import numpy, scipy, matplotlib.pyplot as plt, pandas, librosa
Let's download and listen to a file:
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)
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:
S, freqs, bins, im = plt.specgram(x, NFFT=1024, noverlap=512, Fs=44100)
plt.xlabel('Time (seconds)')
plt.ylabel('Frequency (Hz)')
librosa.feature.melspectrogram
¶librosa
has some outstanding spectral representations, including librosa.feature.melspectrogram
:
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
:
librosa.display.specshow(logS, sr=fs, x_axis='time', y_axis='mel')
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
:
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')