MFCC feature descriptors for audio classification using librosa
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
Mel-Frequency Cepstral Coefficients (MFCCs) are the most widely used features for audio and speech classification tasks. They capture the timbral and spectral characteristics of audio in a compact representation that mimics how the human ear perceives sound. The Python library librosa provides efficient tools to extract MFCCs and related features from audio signals.
What Are MFCCs?
MFCCs are derived through a pipeline that transforms raw audio into a perceptually meaningful representation:
- Frame the signal: Split audio into short overlapping windows (typically 20-40ms)
- Compute the power spectrum: Apply FFT to each frame
- Apply Mel filterbank: Map frequencies to the Mel scale (logarithmic, matching human hearing)
- Take the log: Compute log energies for each Mel band
- Apply DCT: Discrete Cosine Transform decorrelates the features, producing MFCCs
Typically 13 coefficients are used per frame, though 20-40 are sometimes extracted.
Extracting MFCCs with Librosa
Basic Extraction
Customizing Parameters
Aggregating Across Time
For classification, you typically need a fixed-length feature vector per audio clip. Aggregate across time frames:
Delta and Delta-Delta Coefficients
These describe the velocity and acceleration of the MFCCs, providing temporal information akin to derivatives in calculus:
Building an Audio Classifier
Feature Extraction Pipeline
Training and Evaluation
Contrast with Other Features
Comparing MFCCs with features like Chroma or Spectral Contrast can be insightful, depending on the application:
| Feature | Best For | Captures |
| MFCC | Speech, general audio | Timbral/spectral shape |
| Chroma | Music (pitch/harmony) | Pitch class distribution |
| Spectral Contrast | Music genre | Peak-valley differences in spectrum |
| Mel Spectrogram | Deep learning input | Full time-frequency representation |
| Zero Crossing Rate | Speech vs music | Signal noisiness |
Visualizing MFCCs
Common Pitfalls
- Not normalizing features: MFCCs have different scales across coefficients. Always apply
StandardScaleror z-score normalization before feeding into classifiers. - Ignoring deltas: Using only static MFCCs loses temporal dynamics. Adding delta and delta-delta features typically improves classification by 5-15%.
- Fixed duration assumption: Audio clips of different lengths produce different numbers of frames. Either pad/truncate to a fixed duration or use aggregation (mean, std) to get fixed-length vectors.
- Sample rate mismatch: Librosa defaults to resampling at 22050 Hz. If your model was trained on 16000 Hz audio, set
sr=16000inlibrosa.load(). - First coefficient: MFCC coefficient 0 represents overall energy and can dominate other features. Some pipelines drop it or normalize it separately.
Summary
- MFCCs capture perceptually relevant spectral features from audio signals
- Use
librosa.feature.mfcc()to extract coefficients, typically 13 per frame - Add delta and delta-delta features for temporal context (39 features per frame)
- Aggregate with mean/std across time for fixed-length feature vectors
- Normalize features before classification and consider combining with other audio descriptors

