UnicodeDecodeError: 'utf-8' codec can't decode byte 0xff in position 0: invalid start byte
とエラーが出ました。
#coding:utf-8 import wave import numpy as np #Signal Processing import scipy.signal #Fourier Transforms import scipy.fftpack import scipy.fftpack.realtransforms from pylab import * def wavread(filename): wf = wave.open(filename,"r") #サンプル周波数を返す fs = wf.getframerate() #readframes 音声の最も多いフレームを返す #getnframes 音声のフレームの数を返す x = wf.readframes(wf.getnframes()) x = np.frombuffer(x,dtype="int16")/32768.0 wf.close() return x,float(fs) def hz2mel(f): """Hzをmelに変換""" return 1127.01048 * np.log(f/700.0+1.0) def mel2hz(m): """melをhzに変換""" return 700.0 * (np.exp(m/1127.01048)-1.0) def melFilterBank(fs, nfft, numChannels): """メルフィルタバンクを作成""" #ある周波数〜ある周波数までの領域を通すフィルタ #ナイキスト周波数(Hz) fmax = fs/2 #ナイキスト周波数(mel) melmax = hz2mel(fmax) #周波数インデックスの最大数 #global nmax #nmax = nfft/2 #周波数解像度(周波数インデックス1あたりのHz幅) df=fs/nfft #メル尺度における各フィルタの中心周波数を求める dmel = melmax/(numChannels+1) melcenters = np.arange(1,numChannels+1)*dmel #各フィルタの中心周波数をHzに変換 fcenters = mel2hz(melcenters) #各フィルタの中心周波数を周波数インデックスに変換 indexcenter = np.round(fcenters/df) #各フィルタの開始位置のインデックス indexstart = np.hstack(([0],indexcenter[0:numChannels -1])) #各フィルタの終了位置のインデックス indexstop = np.hstack((indexcenter[1:numChannels],[nmax])) filterbank = np.zeros((numChannels,nmax)) for c in np.arange(0,numChannels): #三角フィルタの左の直線の傾きから点を求める increment= 1.0/(indexcenter[c]-indexstart[c]) for i in np.arange(indexstart[c],indexcenter[c]): i=int(i) filterbank[c,i]=(i - indexstart[c])*increment #三角フィルタの直線の傾きから点を求める decrement = 1.0/(indexstop[c]-indexcenter[c]) for i in np.arange(indexcenter[c],indexstop[c]): i=int(i) filterbank[c,i] = 1.0-((i - indexcenter[c])*decrement) return filterbank,fcenters def preEmphasis(signal,p): return scipy.signal.lfilter([1.0,-p],1,signal) def mfcc(signal,nfft,fs,nceps): global nmax nmax = int(nfft / 2) p = 0.97 signal = preEmphasis(signal,p) hammingWindow = np.hamming(len(signal)) signal = signal * hammingWindow spec = np.abs(np.fft.fft(signal,nfft))[:nmax] fscale =np.fft.fftfreq(nfft,d=1.0/fs)[:nmax] numChannels = 20 df = fs/nfft filterbank,fcenters = melFilterBank(fs,nfft,numChannels) mspec = np.log10(np.dot(spec,filterbank.T)) ceps = scipy.fftpack.realtransforms.dct(mspec,type=2,norm="ortho",axis=-1) return ceps[:nceps] def get_feature(wavfile,nfft,nceps): wav,fs = wavread(wavfile) t = np.arange(0.0,len(wav)/fs,1/fs) center =len(wav)/2 cuttime = 0.8 global wavdata wavdata = wav[int(center-cuttime/2*fs):int(center+cuttime/2*fs)] global time time = t[int(center-cuttime/2*fs):int(center+cuttime/2*fs)] ceps = mfcc(wavdata,nfft,fs,nceps) return ceps.tolist() if __name__ == "__main__": wavfile ="3_7_9.wav" nfft=2048 nceps=12 tmp=get_feature(wavfile,nfft,nceps) print(tmp) #波形をプロット plot(time*1000,wavdata) xlabel("time [ms]") ylabel("amplitude") savefig("waveform.png") show()
Tracebackでは
Traceback (most recent call last): File "mfcc.py", line 154, in <module> show() File "/Users/XXX/anaconda/envs/py36/lib/python3.6/site-packages/matplotlib/pyplot.py", line 253, in show return _show(*args, **kw) File "/Users/XXX/anaconda/envs/py36/lib/python3.6/site-packages/matplotlib/backend_bases.py", line 193, in __call__ self.mainloop() File "/Users/XXX/anaconda/envs/py36/lib/python3.6/site-packages/matplotlib/backends/backend_tkagg.py", line 71, in mainloop Tk.mainloop() File "/Users/XXX/anaconda/envs/py36/lib/python3.6/tkinter/__init__.py", line 557, in mainloop _default_root.tk.mainloop(n) UnicodeDecodeError: 'utf-8' codec can't decode byte 0xff in position 0: invalid start byte
と出ました。
これはグラフのplotがutf-8ではできない、ということでしょうか?
どう直せば良いのでしょうか?
あなたの回答
tips
プレビュー