%pylab inline
rcParams['figure.figsize'] = (10, 4) #wide graphs by default
from __future__ import print_function
from __future__ import division
from scipy.io import wavfile
%matplotlib inline
Populating the interactive namespace from numpy and matplotlib
/usr/local/lib/python2.7/site-packages/matplotlib/font_manager.py:273: UserWarning: Matplotlib is building the font cache using fc-list. This may take a moment. warnings.warn('Matplotlib is building the font cache using fc-list. This may take a moment.')
rtom, tom = wavfile.read('tom.wav') #read music file
rpia, pia = wavfile.read('piano.wav')
rglo, glo = wavfile.read('glockenspiel.wav')
from IPython.display import Audio
Audio(tom,rate=rtom)
Audio(pia,rate=rpia)
Audio(glo,rate=rglo)
tom = tom[:].astype(double) #convert to double type
lags, c, lines, line = acorr(tom, maxlags = size(tom) / 2) #auto correlation result
#xlim(0,2000)
plot(c[0: 2000]); #plot down 0:2000 part
#get the lags between two peaks;
t1 = argmax(c[100:500]) + 100;
t2 = argmax(c[500:1000]) + 500;
tomf = 44100 / (t2 - t1) #freq = sample rate / distance between two peaks
tomf #reslult for tom freq
97.56637168141593
pia = pia[:].astype(double);
lags, c, lines, line = acorr(pia, maxlags = size(pia) / 2)
plot(c[0: 2000]); #plot 0:2000
t1 = argmax(c[200:600]) + 200; #find lags between two peaks
t2 = argmax(c[500:1300]) + 500;
piaf = 44100 / (t2 - t1) # the freq equals to sample rate / lags between two peaks.
piaf # the result for piano.wav
77.777777777777771
glo = glo[:].astype(double); #the auto correlation result for glo.wav.
lags, c, lines, line = acorr(glo, maxlags = size(pia) / 2);
plot(c[0:200]); #plot down the first 200 data points.
t1 = argmax(c[0:50]); #find the lags between the two peaks
t2 = argmax(c[50:80]) + 50; #the frequency equals the sample rate / lags between the two peaks
freqg = 44100 / (t2 - t1);
freqg
1297.0588235294117
print ('result tom.wav: ', tomf, ' \nresult for piano.wav: ', piaf,'\nresult for glockenspiel.wav: ', freqg) #the reslut for this assigment
result tom.wav: 97.5663716814 result for piano.wav: 77.7777777778 result for glockenspiel.wav: 1297.05882353
The result get form the previouse assigment is 92.565431458927179 78.057861328125 1323.3428349313294 respectively
So the result is approximately the same with the previous assignment. However, there are some difference between them. The method in this assigment should be more precise for lower frequencies, because for low frequenceis, the lags between two peaks is larger, and the result is sample rate divided by lags between two peaks. So, for bigger lags, the truncation error (between 0-1) has less influence on the result.
However for higher frequency, the lags between two peaks is small, the truncation error(0-1) has bigger impact on the result. Thus, the result for higher frequency is less precise.