-
Notifications
You must be signed in to change notification settings - Fork 37
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
47 additions
and
0 deletions.
There are no files selected for viewing
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |