# import all the library needed
%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
from IPython.display import Audio
Populating the interactive namespace from numpy and matplotlib
sr, signal = wavfile.read('piano.wav')
Audio(signal,rate=44100)
converted_signal=signal[:].astype(double)
length = converted_signal.shape[0]
half_length = int(length/2)
lags, c, lines, line = acorr(converted_signal, maxlags=half_length);
As shown above, the autocorrelation result of piano.wav is has periodically appearing peaks. That means when the orginal signal shifts for a offset which equals the multiple of this periods, the shifted signal and the original signal reaches their max correlation. Only if the max component of the original signal has same period with this period can the phenonmenon above happen. The next step is to compute its period.
margin = 50
window = 700
signal_piece = c[half_length+margin:half_length+margin+window]
plot(signal_piece)
period = margin+argmax(signal_piece)
period
566
Because the value at 0 is greatest, I use margin to exclude this effect. And I assume my window is between the length of 1 period and 2 periods. Therefore, my argmax results indicates the index of the first peak. Adding the margin to this index, we get the period.
44100/period
77.915194346289752
According to sampling rate, 44100 HZ, and period, 468, therefore, the most predominant frequency in the original siganal is 77.92 HZ. It is very close to piano.wav's 78.06 HZ computed in homework 4.
sr, signal = wavfile.read('tom.wav')
Audio(signal,rate=44100)
converted_signal=signal[:].astype(double)
length = converted_signal.shape[0]
half_length = int(length/2)
lags, c, lines, line = acorr(converted_signal, maxlags=half_length);
margin = 50
window = 600
signal_piece = c[half_length+margin:half_length+margin+window]
plot(signal_piece)
period = margin+argmax(signal_piece)
period
468
44100/period
94.230769230769226
This result 94.23 HZ is very close to the result in homework4, which is 92.57 HZ.
sr, signal = wavfile.read('glockenspiel.wav')
Audio(signal,rate=44100)
converted_signal=signal[:].astype(double)
length = converted_signal.shape[0]
half_length = int(length/2)
lags, c, lines, line = acorr(converted_signal, maxlags=half_length);
margin = 20
window = 20
signal_piece = c[half_length+margin:half_length+margin+window]
plot(signal_piece)
period = margin+argmax(signal_piece)
period
33
44100/33
1336.3636363636363
This result 1336.36 HZ is close to the result in homework4, which is 1323.34 HZ. They are not exactly same, because the period is discrete. A more precise peroid may give a closer result.