Junxiang Yao
%pylab inline
from __future__ import print_function
from __future__ import division
Populating the interactive namespace from numpy and matplotlib
from scipy.io import wavfile
sr1,gloc = wavfile.read('glockenspiel.wav')
sr2,piano = wavfile.read('piano.wav')
sr3,tom = wavfile.read('tom.wav')
from IPython.display import Audio
Audio('glockenspiel.wav', rate=sr1)
I imported three audio files, and then checked the waveform of glockenspiel in time domain.
plot(gloc)
title('glockenspiel')
xlabel('time')
ylabel('magnitude')
<matplotlib.text.Text at 0x10c38c350>
Next, I performed rfft on this file.
glocf = abs(fft.rfft(gloc))
plot(glocf)
xlim(0,4000)
title('glockenspiel')
xlabel('freq/bin')
ylabel('magnitude')
<matplotlib.text.Text at 0x10a129950>
I calculated the log and make the frequency of the bin visable and used dB and Hz to present the chart. And then I used argmax() to find the result and printed it.
freqs = np.linspace(0.0,(sr1/2),len(glocf))
figure(figsize=(12,5))
x = argmax(glocf)*(sr1/2+1)/len(glocf)
pylab.axvline(x, ymin = 0, ymax = glocf[argmax(glocf)],color='r')
plot(freqs,20*log10(glocf/sr1))
title('glockenspiel')
xlabel('freq/Hz')
ylabel('magnitude/dB')
print("peak frenquency = %.2f Hz" % x)
peak frenquency = 1323.33 Hz
I enlarged the chart to check the frequency of the most prominent peak.
pylab.axvline(x, ymin = 0, ymax = pianof[argmax(glocf)],color='r')
plot(freqs,20*log10(glocf/sr1))
xlim(1100,1500)
title('glockenspiel')
xlabel('freq/Hz')
ylabel('magnitude/dB')
print("peak frenquency = %.2f Hz" % x)
peak frenquency = 1323.33 Hz
from IPython.display import Audio
Audio('piano.wav', rate=sr2)
After loading the sound file, I checked the waveform of piano in time domain.
plot(piano)
title('piano')
xlabel('time')
ylabel('magnitude')
<matplotlib.text.Text at 0x109f8b9d0>
pianof = abs(fft.rfft(piano))
plot(pianof)
xlim(0,4000)
title('piano')
xlabel('freq/bin')
ylabel('magnitude')
<matplotlib.text.Text at 0x10bec7710>
After preforming the real DFTs on the wave, I changed the x and y value into dB and Hz value in the chart and drew an red line to highlight the peak.
freqs = np.linspace(0.0,sr2/2,len(pianof))
figure(figsize=(12,5))
x=argmax(pianof)*(sr2/2+1)/len(pianof)
pylab.axvline(x, ymin = 0, ymax = pianof[argmax(pianof)],color='r')
plot(freqs,20*log10(pianof/sr2))
title('piano')
xlabel('freq/Hz')
ylabel('magnitude/dB')
print("peak frenquency = %.2f Hz" % x)
peak frenquency = 78.06 Hz
I enlarged the chart to check the frequency of the peak.
pylab.axvline(x, ymin = 0, ymax = pianof[argmax(pianof)],color='r')
plot(freqs,20*log10(pianof/sr2))
title('piano')
xlabel('freq/Hz')
ylabel('magnitude/dB')
xlim(0,150)
print("peak frenquency = %.2f Hz" % x)
peak frenquency = 78.06 Hz
from IPython.display import Audio
Audio('tom.wav', rate=sr3)
I checked the waveform of the audio file here.
plot(tom)
title('tom')
xlabel('time')
ylabel('magnitude')
<matplotlib.text.Text at 0x10bb9ec90>
Next, I performed rfft on the file.
tomf = abs(fft.rfft(tom))
title('tom')
xlabel('freq/bin')
ylabel('magnitude')
plot(tomf)
xlim(0,4000)
(0, 4000)
Again, I used argmax() and found the index of the peak and changed it into Hz.
freqs = np.linspace(0.0,sr3/2,len(tomf))
figure(figsize=(12,5))
x=argmax(tomf)*(sr3/2+1)/len(tomf)
pylab.axvline(x, ymin = 0, ymax = pianof[argmax(tomf)],color='r')
plot(freqs,20*log10(tomf/sr3))
title('tom')
xlabel('freq/Hz')
ylabel('magnitude/dB')
print("peak frenquency = %.2f Hz" % x)
peak frenquency = 92.56 Hz
pylab.axvline(x, ymin = 0, ymax = pianof[argmax(tomf)],color='r')
plot(freqs,20*log10(tomf/sr3))
xlim(0,150)
title('tom')
xlabel('freq/Hz')
ylabel('magnitude/dB')
print("peak frenquency = %.2f Hz" % x)
peak frenquency = 92.56 Hz