Skip to content

Commit

Permalink
ZeroCR
Browse files Browse the repository at this point in the history
  • Loading branch information
aishoot authored Jan 9, 2018
1 parent 33b0153 commit 9fee27f
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 0 deletions.
Binary file added ZeroCR/ZeroCR.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
14 changes: 14 additions & 0 deletions ZeroCR/ZeroCR.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import math
import numpy as np

def ZeroCR(waveData,frameSize,overLap):
wlen = len(waveData)
step = frameSize - overLap
frameNum = int(math.ceil(wlen*1.0/step))
zcr = np.zeros((frameNum,1))
for i in range(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
curFrame = curFrame - np.mean(curFrame) # zero-justified
zcr[i] = sum(curFrame[0:-1]*curFrame[1:]<=0)
return zcr
33 changes: 33 additions & 0 deletions ZeroCR/ZeroCRTest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import wave
import numpy as np
import pylab as pl
import ZeroCR

# read wave file and get parameters.
fw = wave.open('../sounds/aeiou.wav','rb')
params = fw.getparams()
print(params)
nchannels, sampwidth, framerate, nframes = params[:4]
strData = fw.readframes(nframes)
waveData = np.fromstring(strData, dtype=np.int16)
waveData = waveData*1.0/max(abs(waveData)) # normalization
fw.close()

# calculate Zero Cross Rate
frameSize = 256
overLap = 0
zcr = ZeroCR.ZeroCR(waveData,frameSize,overLap)

# plot the wave
time = np.arange(0, len(waveData)) * (1.0 / framerate)
time2 = np.arange(0, len(zcr))*(len(waveData)*1.0/len(zcr)/framerate)
pl.subplot(211)
pl.plot(time, waveData)
pl.ylabel("Amplitude")

pl.subplot(212)
pl.plot(time2, zcr)
pl.ylabel("ZCR")
pl.xlabel("time (seconds)")
pl.savefig("ZeroCR.png")
pl.show()

0 comments on commit 9fee27f

Please sign in to comment.