Search

Search papers and lessons

Mel Spectrogram

Level 2 · 11 min read

A mel spectrogram is a spectrogram whose frequency axis is regrouped into bands spaced by the mel scale and log-compressed, a compact picture of sound that suits how hearing works.

On this page

  • Colour = topic mix
  • Discrete math
  • Signal processing
  • Calculus
  • Linear algebra
  • ReadReady to readNot yet

TL;DR

A mel spectrogram is a spectrogram whose frequency axis has been regrouped. Instead of one row per FFT bin, it has a few dozen bands, spaced evenly on the mel scale, which are narrow at low pitches and wide at high ones. Each band's energy is then log-compressed. The result is a compact grid of numbers over time and band that matches how hearing spaces pitch, and most speech and audio models take it as input.

Builds on: Time-Frequency Representation, Mel Scale, Logarithm · Leads to: speech recognition front ends

Where it's used

Speech recognition. Many speech recognisers turn audio into a log-mel spectrogram before any learning happens. Systems often use somewhere between 40 and 128 mel bands, a design choice that trades detail for size.

Music and sound classification. A model that labels a clip as "jazz" or "rain" sees a mel spectrogram as a picture, with time across and bands up. Convolutional networks can then read it the way they read an image, looking for local patterns that move through time.

Data augmentation for speech. SpecAugment trains speech models on mel spectrograms in which random blocks of time steps and of bands are masked. The model must then recognise the word from the parts it can still see.

How it works

Here is one tiny instance traced from start to finish. A single frame of sound has already been turned into a power spectrum, which is the strength at each frequency. There are four frequency points: 0, 700, 1,400 and 2,100 hertz, with powers 0, 4, 2 and 6.

  1. Start with a power spectrum on a linear axis. The values at 0, 700, 1,400 and 2,100 Hz are 0, 4, 2 and 6. Check: there is one number per frequency, and the spacing between them is equal in hertz.
  2. Place the band centres evenly on the mel scale. Convert 0, 700 and 2,100 Hz to mel with m(f)=2595log⁡10(1+f/700)m(f) = 2595 \log_{10}(1 + f/700): 0, 781 and 1,562. Check: the gaps are 781 and 781, equal in mel, although the hertz gaps are 700 and 1,400.
  3. Build two triangular filters. Filter 1 rises from 0 Hz to a peak at 700 Hz and falls back to zero at 2,100 Hz. Filter 2 rises from 700 Hz to a peak at 2,100 Hz and falls to zero at 4,900 Hz. Weights change linearly in mel. Check: each filter is zero at the centres of its neighbours.
  4. Read the filter weights at 1,400 Hz. In mel, 1,400 Hz is 2595log⁡103≈1,2382595 \log_{10} 3 \approx 1{,}238. Filter 1 gives (1562−1238)/781≈0.41(1562 - 1238)/781 \approx 0.41, and filter 2 gives (1238−781)/781≈0.59(1238 - 781)/781 \approx 0.59. Check: the two weights add to 1.
  5. Take the weighted sums. Filter 1 gives 0×0+1×4+0.41×2+0×6=4.820 \times 0 + 1 \times 4 + 0.41 \times 2 + 0 \times 6 = 4.82. Filter 2 gives 0×4+0.59×2+1×6=7.180 \times 4 + 0.59 \times 2 + 1 \times 6 = 7.18. Check: each filter used only its own weights.
  6. Take the logarithm. The natural logs are ln⁡4.82≈1.57\ln 4.82 \approx 1.57 and ln⁡7.18≈1.97\ln 7.18 \approx 1.97. Check: the output for this frame is two numbers, [1.57,1.97][1.57, 1.97], replacing four frequency values.

The diagram shows the whole pipeline for a clip, with each frame passing through the same steps.

flowchart LR
  A[Audio samples] --> B[Frames with a window]
  B --> C[Power spectrum of each frame]
  C --> D[Triangular mel filters]
  D --> E[Weighted sum per band]
  E --> F[Logarithm]
  F --> G[Mel spectrogram: time by band]

The math

Definition: Mel scale and mel spectrogram

The mel value of a frequency ff in hertz is

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

with inverse f(m)=700(10m/2595−1)f(m) = 700\left(10^{m/2595} - 1\right). A bank of JJ triangular filters HjH_j is built on mel centres mjm_j that are evenly spaced. The mel spectrogram is

S[t,j]=ln⁡(∑kHj(fk) Pt[k]).S[t, j] = \ln\left(\sum_k H_j(f_k)\, P_t[k]\right).

Step 1 in symbols: the power spectrum

For frame tt, the power at bin kk is Pt[k]=∣X[t,k]∣2P_t[k] = |X[t, k]|^2. In the walkthrough, the four powers are P=(0,4,2,6)P = (0, 4, 2, 6) at f=(0,700,1400,2100)f = (0, 700, 1400, 2100) hertz.

Step 2 in symbols: centres on the mel axis

The centres are mj=j Δmm_j = j \, \Delta m for j=0,1,2,…j = 0, 1, 2, \ldots, and each has a hertz value f(mj)f(m_j). In the walkthrough, Δm=781\Delta m = 781 mel, which gives the hertz centres 00, 700700, 21002100 and 49004900. Check: m(700)=2595log⁡102≈781m(700) = 2595 \log_{10} 2 \approx 781, and m(2100)=2595log⁡104≈1562=2×781m(2100) = 2595 \log_{10} 4 \approx 1562 = 2 \times 781.

Step 3 in symbols: triangular weights

With m=m(f)m = m(f), the weight of filter jj at frequency ff is a linear interpolation on the mel axis:

Hj(f)={m−mj−1mj−mj−1mj−1≤m≤mjmj+1−mmj+1−mjmj<m<mj+10otherwise.H_j(f) = \begin{cases} \dfrac{m - m_{j-1}}{m_j - m_{j-1}} & m_{j-1} \le m \le m_j \\[2ex] \dfrac{m_{j+1} - m}{m_{j+1} - m_j} & m_j \lt m \lt m_{j+1} \\[2ex] 0 & \text{otherwise.} \end{cases}

The weight is 1 at the centre and falls to 0 at the neighbouring centres. At 1,400 Hz, m=1238m = 1238, so H1=(1562−1238)/781≈0.41H_1 = (1562 - 1238)/781 \approx 0.41 and H2=(1238−781)/781≈0.59H_2 = (1238 - 781)/781 \approx 0.59.

Step 4 in symbols: the band energy

The energy in band jj is the weighted sum Et[j]=∑kHj(fk)Pt[k]E_t[j] = \sum_k H_j(f_k) P_t[k]. In the walkthrough, Et[1]=4.82E_t[1] = 4.82 and Et[2]=7.18E_t[2] = 7.18.

Step 5 in symbols: the logarithm

The output is S[t,j]=ln⁡Et[j]S[t, j] = \ln E_t[j], giving ln⁡4.82≈1.57\ln 4.82 \approx 1.57 and ln⁡7.18≈1.97\ln 7.18 \approx 1.97. Taking the logarithm compresses the range, so that a few loud bands do not drown out the rest. In practice a small floor is added before the logarithm to keep it finite when a band is silent.

Worked examples

Example
1. Tiny: where a frequency lands in mel

We want the mel value of 1,000 Hz, and the hertz value of 500 mel.

  1. Compute m(1000)=2595log⁡10(1+1000/700)m(1000) = 2595 \log_{10}\left(1 + 1000/700\right). The ratio is 1000/700≈1.42861000/700 \approx 1.4286, so the argument is 2.42862.4286.
  2. log⁡102.4286≈0.3854\log_{10} 2.4286 \approx 0.3854, and 2595×0.3854≈10002595 \times 0.3854 \approx 1000. So 1,000 Hz is about 1,000 mel.
  3. Invert 500 mel: f(500)=700(10500/2595−1)f(500) = 700\left(10^{500/2595} - 1\right). The exponent is 500/2595≈0.1927500/2595 \approx 0.1927, and 100.1927≈1.558310^{0.1927} \approx 1.5583.
  4. f(500)=700×0.5583≈391f(500) = 700 \times 0.5583 \approx 391 Hz.
  5. Interpretation: 500 mel is about 391 Hz, so a band centred at 500 mel sits well below the midpoint of the hertz range, as the compression at high pitch predicts.
Example
2. Standard: a speech front end

Speech is sampled at 16 kHz. Each frame is 25 ms long, the frames are 10 ms apart, the FFT length is 512, and there are 80 mel bands. The numbers are typical choices, not those of any one system.

  1. Samples per frame: 0.025×16000=4000.025 \times 16000 = 400.
  2. FFT bins: 512/2+1=257512/2 + 1 = 257, covering 0 to 8,000 Hz.
  3. Frames per second: 1/0.010=1001 / 0.010 = 100. A 10-second clip has about 10001000 frames.
  4. Output size: 80×1000=80,00080 \times 1000 = 80{,}000 numbers, from 257×1000257 \times 1000 FFT values.
  5. Interpretation: each frame shrinks from 257 bins to 80 bands, a ratio of about 3.2, while the time resolution of 10 ms stays the same.

Try it: a 6-second clip uses the same settings. How many numbers does its mel spectrogram hold? Answer: 6/0.010=6006 / 0.010 = 600 frames, and 80×600=48,00080 \times 600 = 48{,}000 numbers.

Example
3. Realistic: how wide the bands are

A front end uses 80 mel bands from 0 to 8,000 Hz. That needs 82 mel points, from the lowest to the highest, which are 81 gaps. The total mel range is m(8000)=2595log⁡10(1+8000/700)≈2840m(8000) = 2595 \log_{10}(1 + 8000/700) \approx 2840, so the gap is 2840/81≈35.12840 / 81 \approx 35.1 mel.

  1. The first centre is at 35.1 mel, which is f(35.1)≈22f(35.1) \approx 22 Hz. The second is at 70.2 mel, which is about 45 Hz. The bands at the bottom are about 23 Hz apart.
  2. The last centre is at 80×35.1≈280580 \times 35.1 \approx 2805 mel, which is about 77337733 Hz. The top edge is 8,000 Hz.
  3. The gap at the top is about 8000−7733=2678000 - 7733 = 267 Hz.
  4. Interpretation: near the top, the bands are more than ten times wider in hertz than at the bottom. The bank spends its resolution on the low pitches, where speech and music carry most of their detail.

Try it: the same range with 40 bands. What is the gap in mel? Answer: 2840/41≈69.32840 / 41 \approx 69.3 mel.

Common mistakes and tradeoffs

It is tempting to think a mel spectrogram is a different kind of transform from a spectrogram. In fact it is a spectrogram with its frequency axis regrouped. The framing, the windowing and the Fourier transform are the same, and only the bands and the logarithm are added.

It is tempting to think the mel scale is exactly what human hearing does. In fact it is an approximation fitted to pitch experiments from the 1930s. Real perception also depends on loudness and context, and other scales are used when those matter.

It is tempting to think that the scale formula is fixed. In fact there are several. The common form 2595log⁡10(1+f/700)2595 \log_{10}(1 + f/700) is one choice, and some toolkits use a version that is linear below 1 kHz and logarithmic above it. Check which one a model was trained with before using its features.

It is tempting to think that more bands are always better. In fact the bands at the bottom become wide in hertz when the count is small, so a low, precise tone smears across one band. A dense bank also produces highly overlapping, redundant features.

It is tempting to think the logarithm can be skipped. In fact a linear mel spectrogram is dominated by its loudest bands, and most learning systems train better with the log, which also matches the way loudness is perceived.

When to use it. Use it for speech, music and general sound, where the content is pitch patterns that change over time, and where a learned model will read the result.

When not to use it. Avoid it when the exact frequency matters, such as measuring a precise hum or tuning an instrument. A linear-frequency spectrogram with a longer window resolves those.

Alternatives. The table compares the common front ends.

Front endFrequency axisTypical use
Linear spectrogramEven bins in hertzPrecise frequency analysis, such as hum or tone detection
Log-mel spectrogramMel bands, log energySpeech and audio models that learn from the picture
Mel-frequency cepstral coefficientsMel bands, then a further transformClassic speech systems that use a compact set of numbers
Learned filter bankFilters learned from the dataModels that take the waveform directly

History

The mel scale comes from listening experiments in the 1930s, in which people judged when pitches were equally spaced. Its name comes from "melody". The scale was a measurement of perceived pitch, and it stayed a research tool for decades.

In 1980 Steven Davis and Paul Mermelstein used mel-spaced filters to build a compact set of features for recognising words, now known as mel-frequency cepstral coefficients. Those features became a standard front end for speech recognition for a long time. As neural networks grew able to learn from images, the log-mel spectrogram itself became the common input, because a convolutional model can learn its own features from the picture. The mel spectrogram is now the usual input to many speech and audio systems.

Recap

  • A spectrogram gives power over time and frequency, one bin per FFT frequency.
  • A mel spectrogram groups those bins into bands whose centres are evenly spaced on the mel scale, using triangular weights.
  • The band energies are log-compressed, which gives a compact grid over time and band.
  • The bands are narrow at low pitches and wide at high ones, which keeps detail where speech and music carry most of their information.
  • Read next: Mel Scale for the scale itself, Automatic Speech Recognition for the main use, and SpecAugment for how the picture is augmented during training.

Check yourself

QuestionRecall: in one sentence, what does the mel scale do to the frequency axis of a spectrogram?Show answer
It re-spaces frequency so that equal steps in mel are equal perceived steps in pitch, which makes the bands narrow at low frequencies and wide at high ones.
QuestionComputation: what is the mel value of a 3,500 Hz tone, using m(f)=2595log⁡10(1+f/700)m(f) = 2595 \log_{10}(1 + f/700)?Show answer
3500/700=53500/700 = 5, so the argument is 1+5=61 + 5 = 6. Then log⁡106≈0.7782\log_{10} 6 \approx 0.7782, and 2595×0.7782≈20192595 \times 0.7782 \approx 2019. The mel value is about 2,019.
QuestionWhy: why are the triangular filters wider in hertz at high frequencies than at low ones?Show answer
The filters are spaced evenly in mel, and mel grows more slowly than hertz at high frequencies. An equal mel gap therefore covers a larger range of hertz up high. The result is fine resolution at low pitches, where listeners are more sensitive, and coarser resolution at high pitches.
QuestionApplied: a team uses 40 mel bands to find the exact frequency of a 50 Hz electrical hum. Why is this a poor choice, and what would work better?Show answer
The lowest mel bands are tens of hertz wide, so the 50 Hz hum falls inside one broad band and its exact frequency is lost. A linear-frequency spectrogram with a longer window, which has bins a few hertz apart, would resolve it.

Sources