Speech Representations › The Mel Scale and Filterbanks

Michael BrenndoerferFebruary 15, 202610 min read
Language AI HandbookAll chapters
Part 51 of 67Speech and Audio
Chapter 393Speech Representations › The Mel Scale and Filterbanks
Reading section 3 of 11 · The Mel Scale and Filterbanks · 10 minSection 3 of 11 · 10 min

Book contents

Section 3 of 11 · 10 min read

Speech Representations › The Mel Scale and FilterbanksOne section · Sections in this chapter3 / 11 ↓
Reading tools Definitions & inline links

Choose your expertise level to adjust how many terms are explained. Beginners see more tooltips, experts see fewer to maintain reading flow. Hover over underlined terms for instant definitions.

Article links

Make inline references clickable

The mel scale addresses the mismatch between linear frequency and human perception by warping the frequency axis to approximate human pitch perception. Developed through psychoacoustic experiments in the 1930s and 1940s, where listeners judged subjective pitch distances between pure tones, the mel scale spaces frequencies according to perceived equality rather than physical equality. On the mel scale, equal distances correspond to perceptually equal steps, compressing high frequencies and expanding low frequencies to match the non-linear frequency resolution of the human ear.

The name "mel" comes from "melody," which reflects its origins in musical pitch perception research. The psychoacoustic experiments involved subjects adjusting the frequency of one tone until it sounded exactly halfway between two reference tones in pitch, building up a perceptual scale of equal pitch steps. This differs from the physical frequency scale in important ways: the mel scale is nearly linear below 1000 Hz and approximately logarithmic above, following the pattern of the basilar membrane's mechanical frequency analysis.

Mel Scale Mathematics

The mel scale conversion from frequency ff (in Hz) to mel mm follows:

m=2595⋅log⁡10 ⁣(1+f700)m = 2595 \cdot \log_{10}\!\left(1 + \frac{f}{700}\right)

where:

  • mm: the perceived pitch in mel units, spaced according to human perception
  • ff: the physical frequency in Hertz (Hz)
  • 25952595: the scaling constant that maps 1000 Hz to approximately 1000 mel, anchoring the scale at a convenient reference point
  • log⁡10\log_{10}: the base-10 logarithm that converts frequency ratios to perceived pitch differences, with approximately linear response below 1000 Hz and logarithmic response above

Alternatively, some implementations use the natural logarithm variant:

m=1127⋅ln⁡ ⁣(1+f700)m = 1127 \cdot \ln\!\left(1 + \frac{f}{700}\right)

where:

  • mm: the perceived pitch in mel units
  • ff: the physical frequency in Hertz (Hz)
  • 11271127: the scaling constant for the natural logarithm variant, mathematically equivalent to 2595/ln⁡(10)2595 / \ln(10) to maintain the same mapping
  • ln⁡\ln: the natural logarithm (base ee), giving the same perceptual warping as the base-10 variant

The two formulas are mathematically equivalent and produce the same frequency warping; they differ only in the numerical value of the mel units. The shape remains: approximately linear below 1000 Hz (where human hearing is roughly linear), transitioning to logarithmic above 1000 Hz. This reflects the structure of the cochlea, where the basal end (high frequencies) has logarithmic spacing of hair cells, while the apical end (low frequencies) has more linear organization. The constant 700 Hz represents the corner frequency where the transition occurs, determined empirically from psychoacoustic studies.

The inverse transformation from mel to Hz is also needed when constructing filterbanks:

f=700⋅(10m/2595−1)f = 700 \cdot \left(10^{m/2595} - 1\right)

This inverse allows us to convert equally spaced points in mel space back to their corresponding Hz values for constructing the actual filters.

@fig-mel-scale-mapping illustrates this nonlinear relationship, showing how the mel scale compresses high frequencies and expands low frequencies to match human perceptual resolution.

Out[5]:
Visualization
Line plot of the mel scale frequency warping from 0 to 8000 Hz on the x-axis to mel units on the y-axis, with a vertical dashed line at 1000 Hz marking the linear-to-logarithmic transition and annotated linear and logarithmic regions.
Non-linear mapping from linear frequency (Hz) to mel units according to the psychoacoustic mel scale. The curve shows approximately linear behavior below 1000 Hz, where the human ear has fine pitch resolution, transitioning to a logarithmic relationship above 1000 Hz where perceptual sensitivity decreases. A 100 Hz difference near 200 Hz produces a much larger perceptual step than the same 100 Hz difference near 5000 Hz.

To create a mel spectrogram, we need filters that collect energy according to this perceptual scale rather than the linear bins of the FFT, aggregating fine frequency details where they matter most for phonetic discrimination.

Mel Filterbank Design

A mel filterbank consists of overlapping triangular filters spaced uniformly in the mel domain. The key insight is that "uniformly spaced in mel" means "logarithmically spaced in Hz," giving us dense coverage at low frequencies and sparse coverage at high frequencies, exactly matching the perceptual resolution of the ear.

The construction process involves five steps:

  1. Select frequency range: Typically 0 Hz to the Nyquist frequency (fs/2f_s/2), or a narrower range like 0 to 8000 Hz for speech
  2. Convert to mel scale: Map the frequency bounds to mel values using the mel formula
  3. Create equally spaced points: Divide the mel range into M+2M+2 points (where MM is the number of filters), creating boundaries for the triangular filters
  4. Convert back to Hz: Map these mel points back to the linear frequency scale to determine the center frequencies of each filter in physical frequency units
  5. Construct triangular filters: For each filter mm, create a triangle with its peak at mel point mm and zero crossings at mel points m−1m-1 and m+1m+1

The unequal spacing in Hz means that low-frequency filters are narrow (covering perhaps 50-100 Hz each) while high-frequency filters are wide (covering 500-1000 Hz each). This is not a limitation but a feature: it matches the frequency resolution of the cochlea, which has more hair cells per octave at low frequencies than at high frequencies.

Mathematically, the mm-th filter Hm[k]H_m[k] applied to FFT bin kk (corresponding to frequency fkf_k) is defined as:

Hm[k]={0for fk<fm−1fk−fm−1fm−fm−1for fm−1≤fk≤fmfm+1−fkfm+1−fmfor fm≤fk≤fm+10for fk>fm+1H_m[k] = \begin{cases} 0 & \text{for } f_k < f_{m-1} \\[4pt] \dfrac{f_k - f_{m-1}}{f_m - f_{m-1}} & \text{for } f_{m-1} \leq f_k \leq f_m \\[8pt] \dfrac{f_{m+1} - f_k}{f_{m+1} - f_m} & \text{for } f_m \leq f_k \leq f_{m+1} \\[4pt] 0 & \text{for } f_k > f_{m+1} \end{cases}

where:

  • Hm[k]H_m[k]: the weight of the mm-th mel filter applied to FFT bin kk, ranging from 0 to 1
  • fkf_k: the center frequency of FFT bin kk in the linear frequency domain (Hz)
  • fmf_m: the center frequency of the mm-th mel filter in Hz (the peak of the triangle)
  • fm−1f_{m-1} and fm+1f_{m+1}: the center frequencies of adjacent mel filters, defining the lower and upper bounds of the triangular passband
  • The first case rejects frequencies below the filter's bandwidth
  • The second case linearly interpolates from 0 to 1 as frequency increases from fm−1f_{m-1} to fmf_m
  • The third case linearly decreases from 1 to 0 as frequency increases from fmf_m to fm+1f_{m+1}
  • The fourth case rejects frequencies above the filter's bandwidth

The triangular shape has an elegant property: each pair of adjacent filters partitions their shared frequency band without gaps and without double-counting, since whenever filter mm's weight increases from 0 to 1, filter m+1m+1's weight correspondingly decreases from 1 to 0. This partition of unity property ensures that the total energy is conserved (no frequency component is lost or double-weighted) while giving smooth interpolation between filters.

The filters overlap, with each frequency bin adding to multiple mel bins. This smoothing reduces dimensionality while preserving perceptually relevant information, and provides a form of spectral averaging that makes the representation reliable to small frequency shifts. Typical ASR systems use 40, 80, or 128 mel filters, balancing resolution against computational cost. More filters provide finer frequency discrimination but increase the input dimensionality to the neural network. Whisper, the speech recognition model we will examine in subsequent chapters, uses 80 mel bins, a choice that provides good phonetic resolution while keeping the input compact.

Computing Mel Spectrogram Energies

To compute the mel spectrogram, we apply these filters to the power spectrum of each frame, effectively asking: "How much energy is present in each perceptual frequency band?"

S[m,t]=∑k=0N/2∣X[t,k]∣2⋅Hm[k]S[m, t] = \sum_{k=0}^{N/2} |X[t, k]|^2 \cdot H_m[k]

where:

  • S[m,t]S[m, t]: the mel spectrogram energy at mel filter mm and time frame tt, representing the total power in that mel band
  • ∣X[t,k]∣2|X[t, k]|^2: the power spectrum at time frame tt and FFT bin kk, computed as the squared magnitude of the STFT
  • Hm[k]H_m[k]: the weight of the mm-th mel filter for FFT bin kk, determining how much that frequency bin contributes to the mel band
  • tt: the time frame index, corresponding to discrete time steps (typically every 10 ms)
  • mm: the mel filter index, ranging from 0 to M−1M-1 where MM is the total number of mel filters (typically 80)
  • kk: the FFT bin index, ranging from 0 to N/2N/2 (the number of unique frequency bins for a real signal)
  • NN: the FFT size (number of points in the Fast Fourier Transform, typically 512 or 1024)

This summation aggregates energy across FFT bins according to the mel filter weights, creating a vector of MM values for each time frame. The result is a compressed representation where each mel bin represents the total energy in a perceptual frequency band rather than a single frequency component. The compression ratio is substantial: a 512-point FFT produces 257 unique frequency bins, but we condense these into 80 mel bins, reducing the frequency dimension by more than threefold while preserving the perceptually important structure.

Log Compression

Human loudness perception follows a logarithmic scale known as the Weber-Fechner law, which states that the just-noticeable difference in stimulus intensity is proportional to the current intensity level. A doubling of acoustic energy does not produce a doubling of perceived loudness; instead, we perceive loudness on a roughly logarithmic scale. This property has a practical consequence for speech processing: the raw energy values in mel spectrogram bins can span many orders of magnitude between a quiet fricative and a loud vowel. A softmax vowel might produce 1000 times the energy of a quiet "sh," meaning these two phonemes appear very different in linear scale but comparably different in log scale.

To match this perceptual non-linearity and to compress the dynamic range of the signal, we apply a logarithm to the mel energies:

log-mel[m,t]=log⁡(S[m,t]+ϵ)\text{log-mel}[m, t] = \log(S[m, t] + \epsilon)

where:

  • log-mel[m,t]\text{log-mel}[m, t]: the log-compressed mel spectrogram value at mel filter mm and time tt
  • S[m,t]S[m, t]: the mel spectrogram energy at mel filter mm and time tt (linear scale)
  • ϵ\epsilon: a small constant (typically 10−1010^{-10} or 10−1210^{-12}) added for numerical stability
  • log⁡\log: the logarithm function (natural log or base-10), compressing the dynamic range to match human loudness perception

The small constant ϵ\epsilon prevents numerical instability when S[m,t]S[m, t] approaches zero, avoiding negative infinity values in logarithmic space. The result is often expressed in decibels relative to some reference, though for machine learning purposes, the natural log or base-10 log suffices without explicit dB conversion.

Log compression also aligns with the properties that make mel spectrograms easy for neural networks to process. After log compression, the numerical range of the features is typically -100 to 0 dB (or an equivalent log scale), which is much more tractable for gradient-based optimization than raw energies spanning many orders of magnitude. Gradient updates are more stable when input features have comparable magnitudes across all dimensions, and log compression contributes substantially to this stability.

The log-mel spectrogram has become the standard input representation for neural speech recognition systems, giving a good tradeoff between:

  • Temporal resolution: Capturing rapid phonetic transitions and consonant bursts
  • Frequency resolution: Respecting perceptual frequency spacing where low frequencies require fine discrimination
  • Dimensionality: Typically 80 dimensions per frame versus 256+ for linear spectrograms, reducing computational load
  • Invariance: Robust to minor pitch variations and phase shifts that do not affect phonetic identity