In [1]:
# learning from http://samcarcagno.altervista.org/blog/basic-sound-processing-python/
In [2]:
import pylab
from scipy.io import wavfile
In [3]:
sampFreq, snd = wavfile.read('440_sine.wav')
In [4]:
sampFreq
Out[4]:
In [5]:
snd
Out[5]:
In [6]:
#the scipy.io.wavfile.read function reads wav files as int16 (for 16-bit wavs) or int32 (for 32-bit wavs), 24-bit wav files are not supported
In [7]:
#this means that the sound pressure values are mapped to integer values that can range from -2^15 to (2^15)-1
In [8]:
snd.shape
Out[8]:
In [9]:
# ie two channels 5292 sample points
In [10]:
snd = snd / (2.**15) #converts our sound array to floating point values ranging from -1 to 1
In [11]:
5292.0 / sampFreq # considering the sampling rate (sampFreq = 44110) this corresponds to a duration of 120 ms
Out[11]:
In [12]:
len(snd)
Out[12]:
In [13]:
snd[4]
Out[13]:
In [14]:
snd[2500]
Out[14]:
In [15]:
# two values for two channels
In [16]:
s1 = snd[:,0] # work with one channel
In [17]:
#if you’re interested in having sound playback from python check out pyalsaaudio (only Linux) or PyAudio
In [18]:
timeArray = pylab.arange(0, 5292, 1)
In [19]:
timeArray
Out[19]:
In [20]:
timeArray = timeArray / float(sampFreq)
In [21]:
timeArray
Out[21]:
In [22]:
timeArray = timeArray * 1000
In [23]:
%matplotlib inline
import matplotlib.pyplot as plt
In [24]:
plt.plot(timeArray, s1, color='k')
plt.ylabel('Amplitude')
plt.xlabel('Time (ms)')
Out[24]:
In [25]:
#Another useful graphical representation is that of the frequency content, or spectrum of the tone
In [26]:
#obtain the frequency spectrum of the sound using the fft function, that implements a Fast Fourier Transform algorithm
In [27]:
# We’ll follow closely the technical document available https://web.archive.org/web/20120615002031/http://www.mathworks.com/support/tech-notes/1700/1702.html to obtain the power spectrum of our sound.
In [28]:
p = pylab.fft(s1) # take the fourier transform
In [29]:
p #array of complex numbers
Out[29]:
In [30]:
n = len(s1)
n
Out[30]:
In [31]:
nUniquePts = int(pylab.ceil((n+1)/2.0)) #ceil give the ceiling of a value i.e the smallest integer larger than that value
In [32]:
nUniquePts #number of unique points
Out[32]:
In [33]:
p = p[0:nUniquePts]
p
Out[33]:
In [34]:
p = abs(p)
p
Out[34]:
In [35]:
len(p)
Out[35]:
In [36]:
p = p / float(n) # scale by the number of points so that
# the magnitude does not depend on the length
# of the signal or on its sampling frequency
p = p**2 # square it to get the power
# multiply by two (see technical document for details)
# odd nfft excludes Nyquist point
if n % 2 > 0: # we've got odd number of points fft
p[1:len(p)] = p[1:len(p)] * 2
else:
p[1:len(p) -1] = p[1:len(p) - 1] * 2 # we've got even number of points fft
freqArray = pylab.arange(0, nUniquePts, 1.0) * (sampFreq / n);
pylab.plot(freqArray/1000, 10*pylab.log10(p), color='k')
pylab.xlabel('Frequency (kHz)')
pylab.ylabel('Power (dB)')
Out[36]:
In [37]:
sampFreq_pianonote, snd_pianonote = wavfile.read('68448__pinkyfinger__piano-g.wav')
In [38]:
sampFreq_pianonote
Out[38]:
In [39]:
snd_pianonote
Out[39]:
In [40]:
snd_pianonote.shape
Out[40]:
In [41]:
p1 = snd_pianonote[:,0]
p1
Out[41]:
In [42]:
p1 = p1 #/ (2.**15) #?
p1
Out[42]:
In [43]:
timeArray = pylab.arange(0, 68608, 1)
timeArray
Out[43]:
In [44]:
timeArray = (timeArray / float(sampFreq)) * 1000
In [45]:
plt.plot(timeArray, p1, color='k')
plt.ylabel('Amplitude')
plt.xlabel('Time (ms)')
Out[45]:
In [46]:
fp1 = pylab.fft(p1)
In [47]:
fp1
Out[47]:
In [48]:
absfp1 = abs(fp1)
absfp1
Out[48]:
In [49]:
absfp1 = absfp1 / float(n)
power = absfp1**2
# if len(power) % 2 > 0: # we've got odd number of points fft
# power[1:len(power)] = power[1:len(power)] * 2
# else:
# p[1:len(power) -1] = p[1:len(power) - 1] * 2 # we've got even number of points fft
freqArray = pylab.arange(0, len(power), 1.0) * (sampFreq_pianonote / float(len(power)));
pylab.plot(freqArray/1000, 10*pylab.log10(power), color='k')
pylab.xlabel('Frequency (kHz)')
pylab.ylabel('Power (dB)')
Out[49]:
In [50]:
power
Out[50]:
In [51]:
#http://stackoverflow.com/questions/604453/analyze-audio-using-fast-fourier-transform
In [52]:
#https://www.quora.com/How-does-the-sampling-rate-influence-the-frequency-resolution-in-frequency-spectrum-in-FFT
In [53]:
#http://web.itu.edu.tr/hulyayalcin/Signal_Processing_Books/RichardLyons_Understanding_DSP__2001.pdf