importmathimportnumpyasnpdefZeroCR(waveData,frameSize,overLap):wlen=len(waveData)step=frameSize-overLapframeNum=math.ceil(wlen/step)zcr=np.zeros((frameNum,1))foriinrange(frameNum):curFrame=waveData[np.arange(i*step,min(i*step+frameSize,wlen))]#To avoid DC bias, usually we need to perform mean subtraction on each frame#ref: http://neural.cs.nthu.edu.tw/jang/books/audiosignalprocessing/basicFeatureZeroCrossingRate.aspcurFrame=curFrame-np.mean(curFrame)# zero-justifiedzcr[i]=sum(curFrame[0:-1]*curFrame[1::]<=0)returnzcr
importmathimportwaveimportnumpyasnpimportpylabaspl# ============ test the algorithm =============# read wave file and get parameters.fw=wave.open('aeiou.wav','rb')params=fw.getparams()print(params)nchannels,sampwidth,framerate,nframes=params[:4]str_data=fw.readframes(nframes)wave_data=np.fromstring(str_data,dtype=np.short)wave_data.shape=-1,1#wave_data = wave_data.Tfw.close()# calculate Zero Cross RateframeSize=256overLap=0zcr=ZeroCR(wave_data,frameSize,overLap)# plot the wavetime=np.arange(0,len(wave_data))*(1.0/framerate)time2=np.arange(0,len(zcr))*(len(wave_data)/len(zcr)/framerate)pl.subplot(211)pl.plot(time,wave_data)pl.ylabel("Amplitude")pl.subplot(212)pl.plot(time2,zcr)pl.ylabel("ZCR")pl.xlabel("time (seconds)")pl.show()