Looking at music

A very convenient way to get a quick impression of what the songs of diverse genres "look" like is to draw a spectrogram for a set of songs in a genre. A spectrogram is a visual representation of the frequencies that occur in a song. It shows the intensity for the frequencies on the y axis in the specified time intervals on the x axis. In the following spectrogram, that would mean the brighter the color, the stronger the frequency in the particular time window of the song.

Matplotlib provides the convenient specgram() function, which performs most of the under-the-hood calculation and plotting for us:

>>> import scipy.io.wavfile
>>> from matplotlib.pyplot import specgram
>>> sample_rate, X = scipy.io.wavfile.read(wave_filename)
>>> print(sample_rate, X.shape)
22050, (661794,)
>>> specgram(X, Fs=sample_rate, xextent=(0,30), cmap='hot')

The WAV file we just read in was sampled at a rate of 22050 Hz and contains 661794 samples.

If we now plot the spectrogram for these first 30 seconds for diverse WAV files, we can see that there are commonalities between songs of the same genre, as shown in the following diagram:

Just by glancing at the image, we immediately see the difference in the spectrum between, for example, metal and classical songs. While metal songs have high intensity over most of the frequency spectrum all the time (they're energetic!), classical songs show a more diverse pattern.

It should be possible to train a classifier that discriminates at least between metal and classical songs with high enough accuracy. Other genre pairs, such as country and rock, could pose a bigger challenge, though. This looks like a real challenge for us since we need to discern not only two classes, but six. We need to be able to distinguish between all of them reasonably well.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset