Definitionintro
Mel scale
Also called mel frequency scale, mel.
A pitch scale from listener tests. Speech tools use m = 2595 log10(1 + f/700); other mel formulas exist, such as Slaney's and the 1937 table of Stevens, Volkmann and Newman.
The mel scale maps a frequency in Hz to a perceived pitch in mels, so that equal steps in mels sound like equal steps in pitch. Speech toolkits use the formula , but it is a fit, and other mel scales are in use: the scale of Slaney's Auditory Toolbox and librosa, and the original table of Stevens, Volkmann and Newman (1937) from listener tests. The mel scale is only a frequency axis; the mel filter bank places triangular filters on it, and MFCC and the other speech front ends start from those filters.
Definition
The mel scale used on this site and in most speech toolkits is
with in Hz and in mels. Its inverse is . The function is strictly increasing and concave, , and , so 1000 Hz is very close to 1000 mels.
The constant is close to , so the form , which Kaldi uses, is the same scale to about 4 significant digits: . At 4000 Hz the two forms give 2146.06 and 2146.08.
The slope is , which is 1.61 mel per Hz at 0 Hz and 0.66 mel per Hz at 1000 Hz. For much smaller than 700 Hz, grows nearly in proportion to ; for much larger, grows nearly in proportion to , so each doubling of the frequency adds nearly the same number of mels. The usual phrase "linear below 1 kHz and logarithmic above" describes this shape only loosely: at 500 Hz the formula is already 25 percent below its tangent line at 0.
The Slaney scale, from Slaney's Auditory Toolbox and the default of librosa, is a different function with a different unit:
It is exactly linear below 1 kHz, where 1 kHz is 15 units, and exactly logarithmic above.
The original mel scale is the table of Stevens, Volkmann and Newman (1937). Listeners adjusted one pure tone until it sounded half as high, or twice as high, as another, and the scale was built from these judgments, with a 1000 Hz tone set at 1000 mels. The formulas above are fits to this shape and its later revisions, not the table itself.
Examples and non-examples
| (Hz) | 100 | 250 | 500 | 700 | 1000 | 2000 | 4000 | 8000 |
|---|---|---|---|---|---|---|---|---|
| 150.49 | 344.16 | 607.45 | 781.17 | 999.99 | 1521.36 | 2146.06 | 2840.02 | |
| Slaney | 1.5 | 3.75 | 7.5 | 10.5 | 15 | 25.08 | 35.16 | 45.25 |
| Slaney, scaled so 1000 Hz is 1000 | 100 | 250 | 500 | 700 | 1000 | 1672.1 | 2344.3 | 3016.4 |
- Equal steps. A mel filter bank with 26 filters from 0 to 8000 Hz has its 28 edges every mels, which gives edges 68.5 Hz apart at the bottom and 775 Hz apart at the top.
- Two scales, different numbers. Even after scaling the Slaney scale so that 1000 Hz is 1000, the two scales differ: 4000 Hz is 2146.06 on one and 2344.3 on the other. Filters built on the two scales are not the same.
- Non-example: the octave scale. A musical scale of semitones, , is logarithmic at all frequencies. On the mel scale, the octave from 100 to 200 Hz spans 133 mels, and the octave from 2000 to 4000 Hz spans 625 mels.
- Non-example: fundamental frequency. The mel scale describes the pitch of pure tones as a function of their frequency. It does not say what pitch a complex sound such as a voiced vowel has; that is the task of pitch detection.
Related definitions
- Mel filter bank. Triangular filters whose edges are equally spaced in mels; see mel filter bank.
- Mel-frequency cepstral coefficients. The cosine transform of the log energies of those filters; see MFCC.
- Bark scale. A scale of critical bands, about 24 Bark over the range of hearing. Perceptual linear prediction uses the form . It has a shape similar to the mel scale, with one Bark near 100 Hz wide at low frequencies.
- ERB rate. A scale based on the equivalent rectangular bandwidth of the auditory filters, used by gammatone filter banks. It also grows nearly linearly at low frequencies and logarithmically at high ones.
- Other mel formulas. Older work used , which is exactly 1000 at 1000 Hz, and Davis and Mermelstein took the scale as linear below 1000 Hz and logarithmic above.
Proof: The mel conversions are inverse to each other and have the stated shapefull
We prove the facts that the page and the implementation rely on. Let for and for .
Lemma 1 (inverse). for and for .
Proof. , so . Also , whose is .
Lemma 2 (shape). is strictly increasing and strictly concave on , with . As , . For , , which increases with and tends to .
Proof. Differentiate: and . The limit of is . The ratio increases with and tends to 2.
So an octave adds fewer mels at low frequencies than at high ones, never more than 781.17, which is the sense in which the scale is logarithmic at high frequencies.
Lemma 3 (the natural log form). with , so the two forms differ by the factor , a relative difference below at every frequency.
Proof. .
Lemma 4 (the Slaney scale). is continuous and strictly increasing, and its inverse is for and for .
Proof. Both pieces are strictly increasing, the linear piece tends to at , and the log piece is there, so is continuous and strictly increasing. Each branch of the inverse solves its piece for , and the branch is chosen by exactly when .
The tests check the values of the table on the page, Lemma 1 and Lemma 4 on a grid of frequencies, Lemma 3 to 4 significant digits, and the monotone shape of both scales.
Code
Each implementation follows the pseudocode above. A comment @line links a line of code to a line of the pseudocode. Point at a line of the pseudocode to see the matching code. The build runs the native tests of each language.
# SPDX-License-Identifier: MIT
# Part of https://algorithm.tamnd.com/mel-scale/. The authors are listed in prov/AJSQPY.jsonl.
"""Two mel scales and their inverses.
The O'Shaughnessy form, used by HTK, Kaldi and most speech toolkits:
m(f) = 2595 log_10(1 + f / 700).
The Slaney form, used by the Auditory Toolbox and by librosa (its default) and
torchaudio when the "slaney" scale is asked for: linear below 1 kHz with a step
of f_sp = 200 / 3 Hz per mel, so 1 kHz is 15 mel; above 1 kHz a log step of
ln(6.4) / 27 per mel. The two scales use different units and agree only in shape.
"""
import math
F_SP = 200.0 / 3.0
MIN_LOG_HZ = 1000.0
MIN_LOG_MEL = MIN_LOG_HZ / F_SP # 15
LOG_STEP = math.log(6.4) / 27.0
def hz_to_mel(f: float) -> float:
"""m = 2595 log_10(1 + f / 700), f in Hz and f >= 0."""
if f < 0:
raise ValueError("the frequency must be at least 0")
return 2595.0 * math.log10(1.0 + f / 700.0)
def mel_to_hz(m: float) -> float:
"""f = 700 (10^(m / 2595) - 1), the inverse of hz_to_mel."""
return 700.0 * (10.0 ** (m / 2595.0) - 1.0)
def hz_to_mel_slaney(f: float) -> float:
"""f / f_sp below 1 kHz, 15 + ln(f / 1000) / (ln(6.4) / 27) from 1 kHz up."""
if f < 0:
raise ValueError("the frequency must be at least 0")
if f < MIN_LOG_HZ:
return f / F_SP
return MIN_LOG_MEL + math.log(f / MIN_LOG_HZ) / LOG_STEP
def mel_to_hz_slaney(m: float) -> float:
"""The inverse of hz_to_mel_slaney."""
if m < MIN_LOG_MEL:
return m * F_SP
return MIN_LOG_HZ * math.exp(LOG_STEP * (m - MIN_LOG_MEL))
test_mel_scale.py 61 lines
# SPDX-License-Identifier: MIT
# Part of https://algorithm.tamnd.com/mel-scale/. The authors are listed in prov/AJSQPY.jsonl.
import math
from mel_scale import hz_to_mel, hz_to_mel_slaney, mel_to_hz, mel_to_hz_slaney
def test_check_values():
assert round(hz_to_mel(700.0), 2) == 781.17
assert round(hz_to_mel(1000.0), 2) == 999.99
assert round(hz_to_mel(4000.0), 2) == 2146.06
assert round(hz_to_mel(8000.0), 2) == 2840.02
assert hz_to_mel(0.0) == 0.0
def test_inverse():
for f in (0.0, 50.0, 999.0, 1000.0, 1001.0, 4000.0, 11025.0):
assert abs(mel_to_hz(hz_to_mel(f)) - f) < 1e-9
assert abs(mel_to_hz_slaney(hz_to_mel_slaney(f)) - f) < 1e-9
def test_natural_log_form_agrees_to_four_digits():
# 2595 / ln 10 = 1126.99..., so 1127 ln(1 + f / 700) is the same scale rounded
assert abs(2595.0 / math.log(10.0) - 1127.0) < 0.01
for f in range(0, 8001, 250):
assert abs(hz_to_mel(f) - 1127.0 * math.log(1.0 + f / 700.0)) < 0.02
def test_shape_linear_then_log():
# equal ratios of f give nearly equal mel steps far above 700 Hz
a = hz_to_mel(8000.0) - hz_to_mel(4000.0)
b = hz_to_mel(16000.0) - hz_to_mel(8000.0)
assert abs(a - b) / a < 0.1
# near 0 Hz the slope is 2595 / (700 ln 10), about 1.61 mel per Hz
slope = (hz_to_mel(1.0) - hz_to_mel(0.0)) / 1.0
assert abs(slope - 2595.0 / (700.0 * math.log(10.0))) < 2e-3
def test_slaney_scale():
assert abs(hz_to_mel_slaney(1000.0) - 15.0) < 1e-12
assert abs(hz_to_mel_slaney(500.0) - 7.5) < 1e-12
# 27 mel above 1 kHz is a factor 6.4 in frequency
assert abs(mel_to_hz_slaney(15.0 + 27.0) - 6400.0) < 1e-9
# continuous at 1 kHz
assert abs(hz_to_mel_slaney(999.999999) - hz_to_mel_slaney(1000.0)) < 1e-6
# the two scales differ: in Slaney units scaled so 1 kHz is 1000, 4 kHz is about 2344, not 2146
scaled = hz_to_mel_slaney(4000.0) * 1000.0 / 15.0
assert abs(scaled - 2344.2) < 0.1
def test_monotone_and_bad_input():
fs = [i * 37.0 for i in range(300)]
for g in (hz_to_mel, hz_to_mel_slaney):
vals = [g(f) for f in fs]
assert all(u < v for u, v in zip(vals, vals[1:]))
try:
g(-1.0)
except ValueError:
continue
raise AssertionError(g)
References
- S. S. Stevens, J. Volkmann and E. B. Newman, A scale for the measurement of the psychological magnitude pitch, Journal of the Acoustical Society of America 8(3), 1937, 185 to 190, the listener experiments and the table of pitch in mels against frequency
- S. B. Davis and P. Mermelstein, Comparison of parametric representations for monosyllabic word recognition in continuously spoken sentences, IEEE Transactions on Acoustics, Speech, and Signal Processing 28(4), 1980, 357 to 366, Section II.C, footnote 1: the mel scale taken as linear below 1000 Hz and logarithmic above
- Kaldi speech recognition toolkit, src/feat/mel-computations.h, the functions MelScale, 1127 ln(1 + f/700), and InverseMelScale
- librosa, librosa/core/convert.py, the function hz_to_mel: the Slaney scale by default, and 2595 log10(1 + f/700) when htk is true
Where this leads
About this page
The build computes these levels from the evidence in the repository. Each row says what the next level needs. Read what the levels mean.
| Ladder | Level | What the next level needs |
|---|---|---|
| Human proof | H2 full proof |
|
| Lean | L0 no Lean part |
|
| Code (python) | I0 not checked yet |
|
| Visual | V0 none |
|
| Writing | W4 complete |
|
Tag AJSQPY Edit this page History Report an error Markdown