MAT 201A Winter | Jing Yan | theuniqueeye@gmail.com
Step1 : To three audio files, I compute the DFTs of the entire files and then identify the indexs of the most prominent peaks in the magnitude spectrums.
Step2: Take the sample rate as 44100Hz, I Calculate the frequencies (in Hz) correspond to the bins with the most prominent peaks.
# import libraries
%pylab inline
from __future__ import print_function
from __future__ import division
Populating the interactive namespace from numpy and matplotlib
# read three audio files
from scipy.io import wavfile
glockenspiel = wavfile.read('glockenspiel.wav')
piano = wavfile.read('piano.wav')
tom = wavfile.read('tom.wav')
# print the information of three wave files
print("glockenspiel:", glockenspiel)
print("piano:", piano)
print("tom:", tom)
glockenspiel: (44100, array([ 0, 1, -2, ..., -273, -275, -269], dtype=int16)) piano: (44100, array([ 0, 1, -2, ..., -18, 1, -6], dtype=int16)) tom: (44100, array([ -1, 0, -1, ..., 148, 145, 149], dtype=int16))
# diaplay of each audio
from IPython.display import Audio
Audio(glockenspiel[1],rate=44100)
Audio(piano[1],rate=44100)
Audio(tom[1],rate=44100)
# do DFT to each entire file
a = fft.rfft(glockenspiel[1])
b = fft.rfft(piano[1])
c = fft.rfft(tom[1])
# print("a:", a, "b:", b, "c:", c)
# the output is a complex ndarray
# draw the magnitude spectrum of the DFT results
rcParams['figure.figsize'] = (12, 12)
subplot(3,1,1)
plot(abs(a))
xlabel("frequency",size = 8)
ylabel("magnitude",size = 8)
title("the magnitude spectrum of glockenspiel")
subplot(3,1,2)
plot(abs(b))
xlabel("frequency",size = 8)
ylabel("magnitude",size = 8)
title("the magnitude spectrum of piano")
subplot(3,1,3)
plot(abs(c))
xlabel("frequency",size = 8)
ylabel("magnitude",size = 8)
title("the magnitude spectrum of tom")
<matplotlib.text.Text at 0x12b17a850>
# compute the index of the most prominent peak
index_a = argmax(abs(a))
index_b = argmax(abs(b))
index_c = argmax(abs(c))
print(index_a, index_b, index_c)
1158 58 81
max(abs(c))
10148675.819376934
# show the position of maximum magnitude with red dot.
subplot(3,1,1)
xlim(0,2000)
plot(abs(a))
xlabel("frequency",size = 8)
ylabel("magnitude",size = 8)
title("zoom in view of the magnitude spectrum of glockenspiel")
scatter(index_a, max(abs(a)), 50, color ='red')
subplot(3,1,2)
xlim(0,200)
plot(abs(b))
xlabel("frequency",size = 8)
ylabel("magnitude",size = 8)
title("zoom in view of the magnitude spectrum of piano")
scatter(index_b, max(abs(b)), 50, color ='red')
subplot(3,1,3)
xlim(0,200)
plot(abs(c))
xlabel("frequency",size = 8)
ylabel("magnitude",size = 8)
title("zoom in view of the magnitude spectrum of tom")
scatter(index_c, max(abs(c)), 50, color ='red')
<matplotlib.collections.PathCollection at 0x12bc8dc90>
sr = 44100
freq_a = index_a / len(abs(a)) * (sr/2)
freq_b = index_b / len(abs(b)) * (sr/2)
freq_c = index_c / len(abs(c)) * (sr/2)
print(freq_a,freq_b, freq_c)
1323.27425373 78.0530973451 92.5606343284