MAT 201A Winter 2016 HW 4 Zhenyu Yang
#import libraries
%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 scipy.signal import freqz
from scipy.stats import mode
from IPython.display import Audio
srGlockenspiel,srcGlockenspiel = wavfile.read('glockenspiel.wav') #read piano.wav
srPiano,srcPiano = wavfile.read('piano.wav') #read piano.wav
srTom,srcTom = wavfile.read('tom.wav') #read tom.wav
Populating the interactive namespace from numpy and matplotlib
Audio(srcGlockenspiel, rate=srGlockenspiel) #glockenspiel test play
Audio(srcPiano, rate=srPiano) #piano test play
Audio(srcTom, rate=srTom) #tom test play
plot(srcGlockenspiel) # plot of srcGlockenspiel
title("Data read from glockenspiel.wav")
ylabel("Magnitude")
xlabel("Second")
<matplotlib.text.Text at 0x2561a908>
plot(srcPiano) # plot of srcPiano
title("Data read from piano.wav")
ylabel("Magnitude")
xlabel("Second")
<matplotlib.text.Text at 0x259dec88>
plot(srcTom) # plot of srcTom
title("Data read from tom.wav")
ylabel("Magnitude")
xlabel("Second")
<matplotlib.text.Text at 0x262df470>
# calculate the rfft of these three data sets, abstract value is used
srcGlockenspiel_RFFT = abs(fft.rfft(srcGlockenspiel))
srcPiano_RFFT = abs(fft.rfft(srcPiano))
srcTom_RFFT = abs(fft.rfft(srcTom))
#For the following three plots, the x-axis means number of bins
plot(srcGlockenspiel_RFFT) # plot of RFFT of srcGlockenspiel
title("RFFT of srcGlockenspiel")
ylabel("Magnitude")
xlabel("Frequency")
<matplotlib.text.Text at 0x26184b00>
plot(srcPiano_RFFT) # plot of RFFT of srcPiano
title("RFFT of srcPiano")
ylabel("Magnitude")
xlabel("Frequency")
<matplotlib.text.Text at 0x2562a5f8>
plot(srcTom_RFFT) # plot of RFFT of srcTom
title("RFFT of srcTom")
ylabel("Magnitude")
xlabel("Frequency")
<matplotlib.text.Text at 0x26d17a90>
mainBinGlockenspiel = argmax(srcGlockenspiel_RFFT)#Get the index of the main bin
mainBinPiano = argmax(srcPiano_RFFT)#Get the index of the main bin
mainBinTom = argmax(srcTom_RFFT)#Get the index of the main bin
print(mainBinGlockenspiel,mainPiano,mainTom) #print out 3 main bins
1158 58 81
1158 is the position of the most significant bin of Glockenspiel.wav
58 is the position of the most significant bin of Piano.wav
81 is the position of the most significant bin of Tom.wav
#The following part maps main bins to frequency
freqBinGlockenspiel = (mainBinGlockenspiel/len(srcGlockenspiel_RFFT))*(srGlockenspiel/2)
freqPiano = (mainBinPiano/len(srcPiano_RFFT))*(srPiano/2)
freqTom = (mainBinTom/len(srcTom_RFFT))*(srTom/2)
print(freqBinGlockenspiel,freqPiano,freqTom) #print 3 frequencies
1323.27425373 78.0530973451 92.5606343284
1323 is the most significant frequency of Glockenspiel.wav
78 is the most significant frequency of Piano.wav
92 is the most significant frequency of Tom.wav