import numpy, scipy, matplotlib.pyplot as plt, sklearn, librosa, urllib, IPython.display
import essentia, essentia.standard as ess
plt.rcParams['figure.figsize'] = (14,4)
The mel frequency cepstral coefficients (MFCCs) of a signal are a small set of features (usually about 10-20) which concisely describe the overall shape of a spectral envelope. In MIR, it is often used to describe timbre.
Download an audio file:
url = 'http://audio.musicinformationretrieval.com/simple_loop.wav'
urllib.urlretrieve(url, filename='simple_loop.wav')
Plot the audio signal:
x, fs = librosa.load('simple_loop.wav')
librosa.display.waveplot(x, sr=fs)
Play the audio:
IPython.display.Audio(x, rate=fs)
librosa.feature.mfcc
¶librosa.feature.mfcc
computes MFCCs across an audio signal:
mfccs = librosa.feature.mfcc(x, sr=fs)
print mfccs.shape
In this case, mfcc
computed 20 MFCCs over 130 frames.
The very first MFCC, the 0th coefficient, does not convey information relevant to the overall shape of the spectrum. It only conveys a constant offset, i.e. adding a constant value to the entire spectrum. Therefore, many practitioners will discard the first MFCC when performing classification. For now, we will use the MFCCs as is.
Display the MFCCs:
librosa.display.specshow(mfccs, sr=fs, x_axis='time')
Let's scale the MFCCs such that each coefficient dimension has zero mean and unit variance:
mfccs = sklearn.preprocessing.scale(mfccs, axis=1)
print mfccs.mean(axis=1)
print mfccs.var(axis=1)
Display the scaled MFCCs:
librosa.display.specshow(mfccs, sr=fs, x_axis='time')
essentia.standard.MFCC
¶We can also use essentia.standard.MFCC
to compute MFCCs across a signal, and we will display them as a "MFCC-gram":
hamming_window = ess.Windowing(type='hamming')
spectrum = ess.Spectrum() # we just want the magnitude spectrum
mfcc = ess.MFCC(numberCoefficients=13)
frame_sz = 1024
hop_sz = 500
mfccs = numpy.array([mfcc(spectrum(hamming_window(frame)))[1]
for frame in ess.FrameGenerator(x, frameSize=frame_sz, hopSize=hop_sz)])
print mfccs.shape
Scale the MFCCs:
mfccs = sklearn.preprocessing.scale(mfccs)
Plot the MFCCs:
plt.imshow(mfccs.T, origin='lower', aspect='auto', interpolation='nearest')
plt.ylabel('MFCC Coefficient Index')
plt.xlabel('Frame Index')