What Is a Mel Spectrogram? Read and Configure Audio Features
A mel spectrogram groups time-varying audio frequency energy into mel filterbank bands. Learn how it differs from a linear spectrogram, which settings change the result, and how to compute one with librosa.
What Is a Mel Spectrogram? Read and Configure Audio Features
A mel spectrogram applies a mel filterbank to a time-frequency spectrogram. It groups linear frequency bins into fewer mel bands, making a compact representation for audio models and visualization, but some frequency detail is lost. No single configuration is best for every task.
A short-time Fourier transform first produces a spectrogram; overlapping triangular filters then combine neighboring bins into mel bands. With power=2, filters operate on squared magnitude. The result is neither a waveform nor a transcript. The usual shape is (mel bands, time). The mel scale approximates perceptual frequency spacing; it does not perfectly model hearing or mark phonemes.
Settings that change the output
Sample rate sr sets samples per second. The highest representable frequency is sr/2 (the Nyquist limit), so fmax must stay within it. Pass the actual rate after resampling.
n_fft and win_length set the frame analysis and frequency bins. Longer windows can distinguish frequency more finely but may blur timing; shorter windows favor timing while reducing frequency detail.
hop_length is the sample step between frames. A smaller hop yields denser time frames and a larger output.
n_mels sets the number of bands. More bands retain more values but increase input size and computation.
fmin/fmax, mel scale, and norm change the covered range and filter layout. Defaults and HTK/Slaney choices differ across libraries; state them for reproducibility.
librosa defaults to power=2. power_to_db converts power to a logarithmic decibel view for display. Do not treat dB values as power.
Compute with librosa
This example follows librosa 0.11. It keeps the file's sample rate and bounds the top frequency.
import librosa
import numpy as np
y, sr = librosa.load("speech.wav", sr=None, mono=True)
S = librosa.feature.melspectrogram(
y=y, sr=sr, n_fft=1024, hop_length=256, n_mels=80,
fmin=0, fmax=min(8000, sr / 2), power=2.0)
S_db = librosa.power_to_db(S, ref=np.max)
print(S.shape) # (mel bands, time frames)An output with 80 rows has 80 mel bands. These are reproducible example values, not universal recommendations.
Troubleshoot and keep settings consistent
If upper bands are empty or the call errors, check that fmax does not exceed sr/2 and that sr matches the resampled audio. Sparse time detail can be addressed by reducing hop_length, with larger input and computation. If frequencies look merged, compare n_fft or n_mels one at a time. If the plot is one color, distinguish raw power from dB display and check silence, clipping, and channel selection; changing display limits does not alter audio.
Match sample rate, channel handling, n_fft, hop_length, n_mels, frequency limits, mel scale, normalization, and power or dB conversion across training, validation, and deployment. Inspect representative waveforms and features, then choose settings using validation data and task requirements. Record the transformation order so results can be reproduced.
References
librosa 0.11, [librosa.feature.melspectrogram](https://librosa.org/doc/0.11.0/generated/librosa.feature.melspectrogram.html)
torchaudio 2.8, [MelSpectrogram](https://docs.pytorch.org/audio/2.8/generated/torchaudio.transforms.MelSpectrogram.html)
torchaudio 2.11, [Audio Feature Extractions](https://docs.pytorch.org/audio/stable/tutorials/audio_feature_extractions_tutorial.html)