MAT 201A Winter 2016 HW 4 Qiaodong Cui
%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)
plot(tom) #plot first music
[<matplotlib.lines.Line2D at 0x10bd26590>]
X = abs(fft.rfft(glo)) / (glo.size/2) #rfft, normalize, for the glockenspiel music
plot(X) #plot frequency for glockenspiel
xlim(1000,1200) #the maximum freq bin is within this range
(1000, 1200)
argmax(X) #find maximum bin
1158
$ f $ frequency, $ f_0 $ freq bin, $f_s$ sample rate, $N$ number of samples
def get_Pin(wav, srate):
X = abs(fft.rfft(wav)) / (wav.size/2) #first do a rfft, and normalize
b = argmax(X) #find maximum bin
return b*srate / wav.size #the above equation
get_Pin(tom, rtom) #maximum frequency for tom.wav
92.565431458927179
get_Pin(pia, rpia) #maximum frequency for piano.wav
78.057861328125
get_Pin(glo, rglo) #maximum frequency for glockenspiel.wav
1323.3428349313294