How to Extract MFCCs Using Librosa in Python

Mel-frequency cepstral coefficients (MFCCs) are essential features in audio processing, speech recognition, and music information retrieval that represent the short-term power spectrum of sound based on human auditory perception. In Python, the Librosa library simplifies the extraction of MFCCs through a multi-step mathematical pipeline consisting of windowing, Fourier transformation, Mel filter bank application, logarithmic scaling, and the Discrete Cosine Transform (DCT). This guide breaks down the underlying process Librosa uses to compute MFCCs and provides a concise implementation to extract them from any standard audio file.


The Mathematical Extraction Pipeline

Librosa executes five core digital signal processing stages when computing MFCCs:

  1. Framing and Windowing (STFT): Audio signals are non-stationary, meaning their statistical properties change over time. Librosa slices the continuous audio signal into short overlapping frames (typically 20 to 40 milliseconds) using a Hann window. It then computes the Short-Time Fourier Transform (STFT) to transition the audio from the time domain to the frequency domain.
  2. Power Spectrum Calculation: The magnitude of the STFT is squared to calculate the power spectrum for each frame, revealing the energy distribution across various frequencies.
  3. Mel Filter Bank Application: Humans perceive pitch non-linearly, hearing differences more acutely at lower frequencies than higher ones. Librosa maps the linear frequency spectrum onto the Mel scale using triangular overlapping band-pass filters: \[m = 2595 \log_{10}(1 + \frac{f}{700})\] Multiplying the power spectrum by these filter banks produces the Mel spectrum.
  4. Logarithmic Energy Compression: Human perception of loudness is also logarithmic. Librosa computes the natural logarithm of the Mel filter bank energies, bringing the signal closer to perceived human hearing dynamics and reducing sensitivity to variations in input volume.
  5. Discrete Cosine Transform (DCT): Because adjacent Mel bands are highly correlated, Librosa applies a Discrete Cosine Transform (specifically, DCT-II) to the log energies. This decorrelates the filter bank energies and yields a compressed representation: the cepstral coefficients. The lower-order coefficients capture the overall spectral envelope (formants and vocal tract shape), while higher-order coefficients represent fine spectral details (pitch).

Python Implementation

To extract MFCCs using Librosa, load the audio file and call the librosa.feature.mfcc function:

import librosa
import numpy as np

# 1. Load the audio file (default resample rate is 22050 Hz)
audio_path = "sample_audio.wav"
y, sr = librosa.load(audio_path, sr=None)

# 2. Extract MFCCs
# n_mfcc specifies the number of coefficients to return
mfccs = librosa.feature.mfcc(y=y, sr=sr, n_mfcc=13, n_fft=2048, hop_length=512)

# Output shape: (n_mfcc, time_frames)
print("MFCC matrix shape:", mfccs.shape)

Critical Parameters Explained


Extracting Dynamic Features (Delta and Delta-Delta)

MFCCs only represent static spectral envelopes per frame. To capture how sound changes dynamically over time, you can compute first-order (velocity) and second-order (acceleration) derivatives:

# Compute first derivative (Delta)
mfcc_delta = librosa.feature.delta(mfccs)

# Compute second derivative (Delta-Delta)
mfcc_delta2 = librosa.feature.delta(mfccs, order=2)

# Stack features into a single composite representation
composite_features = np.vstack([mfccs, mfcc_delta, mfcc_delta2])
print("Composite feature shape:", composite_features.shape)

Stacking static MFCCs with delta and delta-delta features produces a comprehensive 39- or 40-dimensional feature vector per frame, providing optimal input features for machine learning models and audio classifiers.