08.06.2015 Views

Building Machine Learning Systems with Python - Richert, Coelho

Building Machine Learning Systems with Python - Richert, Coelho

Building Machine Learning Systems with Python - Richert, Coelho

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Chapter 9<br />

Matplotlib provides the convenient function specgram() that performs most of the<br />

under-the-hood calculation and plotting for us:<br />

>>> import scipy<br />

>>> from matplotlib.pyplot import specgram<br />

>>> sample_rate, X = scipy.io.wavfile.read(wave_filename)<br />

>>> print sample_rate, X.shape<br />

22050, (661794,)<br />

>>> specgram(X, Fs=sample_rate, xextent=(0,30))<br />

The wave file we just read was sampled at a sample rate of 22,050 Hz and contains<br />

661,794 samples.<br />

If we now plot the spectrogram for these first 30 seconds of diverse wave files, we<br />

can see that there are commonalities between songs of the same genre:<br />

[ 183 ]

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!