In [1]:
import numpy, scipy, matplotlib.pyplot as plt, pandas, librosa

Short-Time Fourier Transform

Let's download and listen to a file:

In [2]:
import urllib
urllib.urlretrieve('http://audio.musicinformationretrieval.com/c_strum.wav')

x, fs = librosa.load('c_strum.wav', sr=44100)

from IPython.display import Audio
Audio(x, rate=fs)
Out[2]:

Spectrogram

Musical signals are highly non-stationary, i.e., their statistics change over time. It would be rather meaningless to compute a spectrum over an entire 10-minute song.

Instead, we compute a spectrum for small frames of the audio signal. The resulting sequence of spectra is called a spectrogram.

matplotlib.specgram

Matplotlib has specgram which computes and displays a spectrogram:

In [7]:
S, freqs, bins, im = plt.specgram(x, NFFT=1024, noverlap=512, Fs=44100)
plt.xlabel('Time (seconds)')
plt.ylabel('Frequency (Hz)')
Out[7]:
<matplotlib.text.Text at 0x1132b68d0>

librosa.feature.melspectrogram

librosa has some outstanding spectral representations, including librosa.feature.melspectrogram:

In [8]:
S = librosa.feature.melspectrogram(x, sr=fs, n_fft=1024)
logS = librosa.logamplitude(S)

To display any type of spectrogram in librosa, use librosa.display.specshow:

In [9]:
librosa.display.specshow(logS, sr=fs, x_axis='time', y_axis='mel')
Out[9]:
<matplotlib.image.AxesImage at 0x113018d90>

librosa.cqt

Unlike the Fourier transform, the constant-Q transform uses a logarithmically spaced frequency axis.

To plot a constant-Q spectrogram, will use librosa.cqt:

In [13]:
fmin = librosa.midi_to_hz(36)
C = librosa.cqt(x, sr=fs, fmin=fmin, n_bins=60)
logC = librosa.logamplitude(C)
librosa.display.specshow(logC, sr=fs, x_axis='time', y_axis='cqt_note', fmin=fmin, cmap='coolwarm')
Out[13]:
<matplotlib.image.AxesImage at 0x114af5dd0>