AI Logic

Audio Features and Front-ends

Mel Scale

Updated:

Diagram of the mel scale. The left side shows 17 points placed at equal intervals on the mel scale and mapped back to a frequency axis (0 to 8000 Hz); the points become denser at lower frequencies. The right side shows curves of the frequency-to-mel conversion: the HTK formula (solid line), the Slaney formula (dashed line, right axis), and a proportional straight line for comparison

Background and Overview

The mel scale is a scale that relabels frequency (Hz) so that perceived pitch is equally spaced along it. Its unit is the mel, and the pitch of a 1000 Hz tone (40 dB above the threshold of hearing) is defined as 1000 mels.

The human ear is sensitive to differences between low tones but insensitive to differences between high tones. For example, 100 Hz and 200 Hz sound clearly different, while the difference between 7100 Hz and 7200 Hz feels much smaller. Treating frequency at equal intervals as-is means handling the low-frequency information that matters to the ear coarsely and the high-frequency information that the ear barely distinguishes finely. Converting to the mel scale corrects this imbalance to match how the ear perceives sound.

The mel scale was built in 1937 by Stevens, Volkmann, and Newman from psychoacoustic experiments based on listeners' judgments. In modern audio processing, it is used to decide how to divide and aggregate the frequency axis in MFCCs and in the mel spectrograms used by models such as Whisper and Tacotron 2.

Conversion Formulas

Because the mel scale is an experimentally measured curve, there are many formulas that approximate it. Two are commonly used.

The HTK formula (from O'Shaughnessy's textbook; the default in the HTK speech recognition toolkit and in torchaudio)

m=2595log⁡10 ⁣(1+f700),f=700(10m/2595−1)m = 2595 \log_{10}\!\left(1 + \frac{f}{700}\right), \qquad f = 700\left(10^{m/2595} - 1\right)

ff is frequency (Hz) and mm is mels. With the natural logarithm it becomes m=1127ln⁡(1+f/700)m = 1127 \ln(1 + f/700). Well below 700 Hz it is nearly proportional to frequency, and above that it approaches a logarithm.

The Slaney formula (from Slaney's Auditory Toolbox; the default in librosa)

m={3f200(f<1000)15+27log⁡6.4 ⁣(f1000)(f≥1000)m = \begin{cases} \dfrac{3f}{200} & (f < 1000) \\[6pt] 15 + 27 \log_{6.4}\!\left(\dfrac{f}{1000}\right) & (f \ge 1000) \end{cases}

This formula joins a straight line below 1000 Hz with a logarithm above it. Since 1000 Hz maps to 15, its values are on a different scale from the HTK formula (1000 Hz is not 1000 mels).

Converting several frequencies with the two formulas gives the following.

FrequencyHTK formulaSlaney formula
100 Hz150.51.5
500 Hz607.47.5
1000 Hz1000.015.0
2000 Hz1521.425.1
4000 Hz2146.135.2
8000 Hz2840.045.2

For the same 1000 Hz span, the HTK formula increases by about 521 mels from 1000 Hz to 2000 Hz but only by about 138 mels from 7000 Hz to 8000 Hz. This reflects that frequency differences feel smaller at higher frequencies (right side of the diagram at the top of the article).

Building a Mel Filterbank

The most common use of the mel scale is the mel filterbank, which aggregates a power spectrum into a small number of bands. The nn filters are built as follows.

  1. Convert both ends to mels: convert the lower limit fmin⁡f_{\min} and the upper limit fmax⁡f_{\max} of the range (often half the sampling rate) to mels.
  2. Place n+2n + 2 points at equal intervals on the mel scale: divide the range evenly, including both ends.
  3. Convert back to Hz: map each point back to frequency with the inverse transform. The points become denser at lower frequencies (left side of the top diagram).
  4. Build triangular filters: the kk-th filter is a triangle that is 0 at point k−1k-1, 1 at point kk, and 0 at point k+1k+1. Neighboring filters overlap by half.
  5. (Optional) Equalize the areas: Slaney normalization multiplies each filter's weights by 2/(fk+1−fk−1)2 / (f_{k+1} - f_{k-1}). This keeps wide high-frequency filters from producing larger values simply because they sum more components.

Multiplying these filters with the per-frame power spectrum from a short-time Fourier transform and summing gives a mel spectrogram. Taking the logarithm gives a log mel spectrogram, and applying a discrete cosine transform on top gives MFCCs.

Equal spacing on the mel scale is easier to picture with a piano keyboard. People perceive pitch roughly as a "ratio" rather than a "difference," so each key covers a wider frequency range in higher registers. The mel scale lies in between: nearly proportional to frequency below 1000 Hz and approaching this "ratio" perception above it.

Why Dividing by the Mel Scale Helps

Laying out filters on the mel scale has three advantages.

  • It tends to keep information that matters to the ear: the cues that distinguish phonemes in speech (spectral peaks called formants) lie mainly between a few hundred Hz and a few kHz. Dividing low frequencies finely makes differences in this range easier to capture.
  • It reduces dimensionality: a 512-point FFT on 16 kHz audio yields 257 frequency values, but a mel filterbank compresses them to about 40 to 128. The model input becomes smaller, and training becomes lighter.
  • It is robust to small fluctuations in voice pitch: the wide filters at high frequencies smooth over differences in the positions of individual harmonics. The coarse shape related to what is being said remains, rather than differences in pitch between speakers.

On the other hand, because it discards fine frequency information, CQT, which divides pitch at equal logarithmic intervals, is better suited to music tasks that require precise handling of pitch and chords.

Practical Considerations

The default formula differs by library. librosa defaults to the Slaney formula with area normalization (htk=False, norm="slaney"), while torchaudio defaults to the HTK formula without normalization (mel_scale="htk", norm=None). Even with the same number of filters, the filter positions and magnitudes change, so the resulting mel spectrogram values change. When using a pretrained model, compute features with the same library and settings used in training.

Keep the following points in mind as well.

  • Do not use too many filters: with too few FFT points and too many filters, the low-frequency filters become too narrow, producing empty filters that contain no FFT frequency bins at all. librosa warns "Empty filters detected" in this case. Reduce the number of filters or increase the number of FFT points.
  • Use the same formula when comparing mel values: the HTK and Slaney formulas have different unit scales, so values expressed as "so many mels" cannot be compared directly.
  • The mel scale itself is uncertain: the original scale was built from listening experiments with few participants, and some researchers point out biases due to the experimental procedure. It is safest to view the differences between formulas not as a matter of which is "correct," but of which experimental curve was approximated and how.

Comparison with Other Frequency Scales

Besides the mel scale, there are several ways to divide the frequency axis of a spectrum.

Frequency axisDivisionStrengthsWeaknessesMain uses
Linear (raw FFT)Equal Hz stepsDiscards no information. Easy to invert back to a waveformHigh dimensionality. Spends many dimensions on high frequenciesSource separation, waveform reconstruction
Mel scaleFine at low frequencies, nearly logarithmic above 1000 HzClose to human perception, low dimensionalityDiscards fine pitch information. Formula differs by librarySpeech recognition, speech synthesis, sound event detection
Logarithmic (CQT)Same number of bins per octavePitches are equally spaced, and transposition becomes a shiftNeeds long windows at low frequencies; computationally heavyMusic transcription, chord recognition
Learned (SincNet, LEAF)Bands learned from dataAutomatically picks a division suited to the taskRequires training data and compute. Usually initialized with the mel scaleSpeaker recognition, audio classification

Rough guidelines for choosing:

  • Classifying or recognizing speech and general sounds: start with a log mel spectrogram on the mel scale (40 to 128 filters).
  • Music tasks where pitch and chords matter: consider CQT.
  • Enough data, and a desire to tailor even the features to the task: try learnable filters initialized with the mel scale (SincNet, LEAF).

PyTorch example

Show code
python
import math
import torch
import torchaudio

'''
Mel-scale conversion, and computing a mel filterbank and a mel spectrogram
'''

# 1. Conversion formulas (HTK and Slaney)
def hz_to_mel_htk(f):
  return 2595.0 * math.log10(1.0 + f / 700.0)

def mel_to_hz_htk(m):
  return 700.0 * (10.0 ** (m / 2595.0) - 1.0)

def hz_to_mel_slaney(f):
  if f < 1000.0:
    return 3.0 * f / 200.0
  return 15.0 + math.log(f / 1000.0) / (math.log(6.4) / 27.0)

print(round(hz_to_mel_htk(1000), 1))     # 1000.0
print(round(hz_to_mel_slaney(1000), 1))  # 15.0 (different unit scale)

# 2. Mel filterbank (triangular filters centered at equal intervals on the mel scale)
sample_rate, n_fft, n_mels = 16000, 512, 40
fb = torchaudio.functional.melscale_fbanks(
  n_freqs=n_fft // 2 + 1,  # number of FFT frequency bins (257)
  f_min=0.0,
  f_max=sample_rate / 2,
  n_mels=n_mels,
  sample_rate=sample_rate,
  norm=None,               # 'slaney' equalizes the area of each filter
  mel_scale='htk',         # 'slaney' gives the same formula as the librosa default
)
print(fb.shape)  # torch.Size([257, 40])  [frequency bins, filters]

# 3. Compute a log mel spectrogram from a waveform
t = torch.arange(sample_rate) / sample_rate
waveform = torch.sin(2 * math.pi * 440 * t).unsqueeze(0)  # [1, 16000]
mel_spec = torchaudio.transforms.MelSpectrogram(
  sample_rate=sample_rate,
  n_fft=n_fft,
  win_length=400,     # 25 ms window
  hop_length=160,     # shift by 10 ms
  n_mels=n_mels,
  mel_scale='htk',
)(waveform)
log_mel = torch.log(mel_spec + 1e-6)  # add a small value to avoid log(0)
print(log_mel.shape)  # torch.Size([1, 40, 101])  [channels, filters, frames]

FAQ

Q. Which should I use, the HTK formula or the Slaney formula?
A.

Neither is correct per se; the rule of thumb is to match the model or existing implementation you use. When using a pretrained model, choose the same formula (and the same normalization setting) as that model's preprocessing. For a new model, the default of your library is fine, but always keep training and inference consistent.

Q. How many mel filters should I use?
A.

Speech recognition commonly uses 40 or 80, and speech synthesis and music commonly use 80 to 128. More filters keep finer frequency information, but too many relative to the number of FFT points produce empty filters at low frequencies.

Q. Why is 1000 Hz equal to 1000 mels?
A.

Because the mel scale measures pitch relatively, a reference point has to be set somewhere. The original paper defined the pitch of a 1000 Hz tone, 40 dB above the threshold of hearing, as 1000 mels. The HTK formula (m=2595log⁡10(1+f/700)m = 2595 \log_{10}(1 + f/700)) is designed to nearly satisfy this reference.

References

  1. Stevens, S. S., Volkmann, J., & Newman, E. B. (1937). A scale for the measurement of the psychological magnitude pitch. The Journal of the Acoustical Society of America, 8(3), 185–190.
  2. librosa.mel_frequencies (librosa 0.11.0 documentation; describes the HTK and Slaney formulas).
  3. librosa.filters.mel (librosa 0.11.0 documentation; mel filterbank and Slaney normalization).
  4. torchaudio.functional.melscale_fbanks (PyTorch documentation).
  5. Mel scale (Wikipedia; various approximation formulas and their origins).