Mel spectrogram

On this page 8

A mel spectrogram is a two-dimensional representation of audio in which the horizontal axis is time, the vertical axis is frequency mapped onto the perceptually motivated mel scale, and each cell holds the energy of a frequency band in a short window, usually on a logarithmic scale. In speech & language AI it is the default acoustic feature for speech recognition, speaker analysis and text-to-speech.

The representation compresses a raw waveform of tens of thousands of samples per second into roughly 80 to 128 values per 10 milliseconds, keeping the information that matters for human hearing while discarding fine detail that neural networks rarely need. Its popularity comes from this balance of compactness and fidelity.

How a mel spectrogram is computed

The computation starts with a short-time Fourier transform (STFT). The waveform is cut into overlapping frames, each frame is multiplied by a window function such as Hann, and a fast Fourier transform turns it into a spectrum. Squaring the magnitudes gives a power spectrogram with linearly spaced frequency bins.

Next, a mel filterbank of overlapping triangular filters is applied. Filters are narrow and dense at low frequencies and wide and sparse at high frequencies, so the output mirrors the finer pitch resolution of human hearing below about 1 kHz. Finally, the energies are compressed with a logarithm or a decibel conversion, producing the log-mel spectrogram that models actually consume.

The table below lists the processing steps and the effect each one has on the signal.

StepOperationEffect on the data
FramingSplit audio into short overlapping windowsCreates the time axis
WindowingMultiply each frame by a taperReduces spectral leakage
FFTTransform each frame to frequencyLinear frequency bins
PowerSquare the magnitudeEnergy per bin
Mel filterbankSum energy under triangular filtersPerceptual frequency bands
Log compressionApply log or decibel scalingMatches loudness perception, stabilizes training
Steps from waveform to log-mel spectrogram

The mel scale and why it is used

The mel scale maps physical frequency in hertz to perceived pitch. A widely used formula is m = 2595 · log10(1 + f / 700), which is roughly linear below 1 kHz and logarithmic above it. Equal steps in mels therefore correspond to steps that listeners judge as equally large.

Using this scale concentrates resolution where speech formants and pitch harmonics carry the most information. It also reduces dimensionality: a 1,024-point FFT yields 513 linear bins, while an 80-band mel filterbank keeps the essential structure in far fewer numbers. Two slightly different mel definitions exist, the HTK formula and the Slaney variant, and mixing them between training and inference is a subtle source of errors.

Key parameters and typical values

A mel spectrogram is only reproducible when every extraction parameter is fixed. Models trained with one configuration usually perform poorly when fed features computed with another.

ParameterTypical speech valueWhat it controls
Sample rate16 kHz (recognition), 22.05 to 24 kHz (synthesis)Highest representable frequency
Window length25 ms (400 samples at 16 kHz)Frequency resolution
Hop length10 ms (160 samples at 16 kHz)Time resolution, frame rate
FFT size512 or 1,024 pointsNumber of linear bins
Number of mel bands80 or 128Frequency detail after warping
Frequency range0 to 8 kHz or 20 Hz to 11 kHzWhich part of the spectrum is kept
Log floorSmall epsilon or -80 dB clampPrevents log of zero
Common mel spectrogram settings for speech

The window and hop lengths define the classic time-frequency trade-off: longer windows resolve pitch harmonics more sharply but blur fast transitions such as plosive consonants, while shorter windows do the opposite.

Mel spectrogram compared with other audio features

Several acoustic representations compete with the mel spectrogram, and the choice depends on model type and task. Deep neural networks generally prefer richer, less processed inputs than classical statistical models did.

RepresentationMain ideaTypical use
Raw waveformSamples fed directly to the modelSelf-supervised encoders, some vocoders
Linear spectrogramSTFT power without warpingSource separation, analysis
Mel spectrogramSTFT power on mel bands, log scaledSpeech recognition, TTS, audio tagging
MFCCDiscrete cosine transform of log-mel energiesClassical recognition, small models
Constant-Q transformLogarithmic bins tied to musical notesMusic transcription
Mel spectrogram versus related audio representations

MFCC features are derived from the log-mel spectrogram by a discrete cosine transform that decorrelates the bands. This step helped Gaussian mixture models, but convolutional and transformer networks learn such correlations themselves, so the log-mel spectrogram has largely replaced MFCC in deep learning systems.

Role in speech recognition and speech synthesis

In automatic speech recognition, encoders such as Conformer models take log-mel frames, often stacked or subsampled, and map them to text through CTC, attention or transducer objectives. Augmentation methods like SpecAugment mask random time and frequency stripes directly on the mel spectrogram to improve robustness.

In text-to-speech, the mel spectrogram is the usual intermediate target. An acoustic model such as Tacotron or a non-autoregressive successor predicts mel frames from text, and a neural vocoder such as WaveNet or HiFi-GAN converts them back into a waveform. Because the log-mel representation discards phase, the vocoder must reconstruct phase information plausibly, which is why vocoder quality strongly shapes perceived naturalness.

Limitations and common mistakes

The representation is lossy and sensitive to configuration, so practitioners should watch for several recurring pitfalls.

  • Mismatched parameters between training and deployment, including sample rate and mel formula.
  • Inconsistent normalization, such as per-utterance versus global mean and variance.
  • Missing log floor, which lets silent frames produce extreme negative values.
  • Too narrow a frequency range, which removes fricative energy above 8 kHz in synthesis.
  • Assuming invertibility: without phase, simple inversion with Griffin-Lim sounds metallic.

Explained without jargon

A mel spectrogram is a picture of sound. Time runs from left to right. Pitch runs from bottom to top. Brighter spots mean louder sound at that pitch and moment.

The picture is stretched to match how people hear. Our ears notice small changes in low tones but not in very high ones. So the picture gives more space to low tones and squeezes the high ones together. Computers can then read speech from this picture much like they read photos.

A typical use case

A team building a voice assistant for a hospital ward trains a speech recognition model on recorded dictation. Every clip is resampled to 16 kHz and converted into 80-band log-mel spectrograms with a 25 ms window and a 10 ms hop, then normalized with statistics computed over the full training set.

During testing, accuracy drops sharply on audio from a new microphone. Investigation shows the device pipeline used 44.1 kHz audio and a different mel formula. After the team aligns the feature extraction code across training and deployment, the word error rate returns to its validation level, and the configuration is locked in a shared feature specification for all future models.