音频人工智能指南

梅尔谱图

A mel spectrogram is a picture of sound: it shows how much energy an audio signal has in each of a few dozen frequency bands, frame by frame over time, with the bands spaced on the mel scale so that low frequencies get finer detail, roughly as human hearing does.

听听这个指南AI叙述 · ~8 min

在本页阅读时间 9 分钟
  1. 概述
  2. 主要要点
  3. 深入探讨
  4. Computing a Whisper-style mel spectrogram of a test tone
  5. 现实世界的实施
  6. 梅尔谱图的未来
  7. 付诸实践
  8. 资料来源与延伸阅读
  9. 不断探索
  10. 常见问题

概述

It is the standard input to speech recognizers such as Whisper and the standard intermediate output of text-to-speech systems such as Tacotron 2.

主要要点

  1. A mel spectrogram shows audio energy per frequency band over time, with bands spaced on the perceptual mel scale.
  2. The mel scale comes from a 1937 study by Stevens, Volkmann and Newman in the Journal of the Acoustical Society of America.
  3. The HTK mel formula is 2595 times log10(1 + f/700), which maps 1,000 Hz to about 1,000 mels.
  4. Whisper uses 80-channel log-mel input on 25 ms windows with a 10 ms stride, and Whisper large-v3 uses 128 bins.
  5. Mel spectrograms discard phase, so turning them back into audio requires a vocoder such as WaveNet or HiFi-GAN.
  6. Sample rate, FFT size, hop, band count, frequency range and mel formula must all match what a model was trained on.

深入探讨

A mel spectrogram is a two-dimensional array that describes a sound. One axis is time, divided into short overlapping frames. The other axis is frequency, divided into bands. Each cell holds the energy of the sound in that band during that frame. What makes it a mel spectrogram, rather than an ordinary spectrogram, is how the bands are spaced. They follow the mel scale, a perceptual pitch scale on which equal steps sound roughly equally far apart to a listener. On that scale low frequencies are spread out and high frequencies are squeezed together, so a mel spectrogram spends most of its resolution where speech and music carry the most perceptually important detail. Researchers usually take the logarithm of the energies, producing a log-mel spectrogram, because loudness perception is also roughly logarithmic.

Computing one takes four steps. First, the waveform, a list of air-pressure samples such as 16,000 per second, is cut into frames using a window function, typically a Hann window that tapers each frame's edges. The window length sets frequency resolution and the hop, the gap between frame starts, sets time resolution. Second, each frame goes through a short-time Fourier transform (STFT), which measures how much of each frequency is present, producing a linear spectrogram with n_fft/2 + 1 frequency bins. Third, the magnitudes, or their squares for power, are multiplied by a mel filterbank: a set of overlapping triangular filters, each centered on a point evenly spaced on the mel scale, which pool neighboring frequency bins into one band. Fourth, a logarithm compresses the dynamic range, usually after clipping very small values so the log does not explode.

The scale itself comes from psychology. In 1937 S. S. Stevens and J. Volkmann of Harvard and E. B. Newman of Swarthmore published "A Scale for the Measurement of the Psychological Magnitude Pitch" in the Journal of the Acoustical Society of America. They built a subjective pitch scale by asking listeners to judge when one tone sounded half as high as another, at many reference frequencies. Two engineering formulas descended from that work. The HTK formula, named after the Hidden Markov Model Toolkit and the default in torchaudio, is mel = 2595 times log10(1 + f/700), which maps 1,000 Hz to about 1,000 mels. The Slaney formula from Slaney's MATLAB Auditory Toolbox, the default in librosa, is linear below 1,000 Hz and logarithmic above. They give different band edges, so features computed one way do not match a model trained the other way.

Mel features have been used for speech recognition for decades, as the Tacotron 2 paper notes, but deep learning made them a common interface between models. Tacotron 2, posted by Google researchers in December 2017, split text-to-speech into two networks connected by a mel spectrogram: one predicts 80-channel mel frames from characters, and a modified WaveNet vocoder, the network that turns spectral features back into a waveform, generates audio from them. The authors used 50 ms frames, a 12.5 ms hop, a filterbank from 125 Hz to 7.6 kHz, and clipped magnitudes at 0.01 before taking the log. They reported a mean opinion score of 4.53, against 4.58 for professional recordings. HiFi-GAN in 2020 made the vocoder step fast, and Whisper in 2022 trained a speech recognizer on 680,000 hours of audio using 80-channel log-mel input.

The main alternatives each trade something. A raw waveform keeps everything, including phase, but is long and hard to model directly: 30 seconds at 16 kHz is 480,000 numbers, while Whisper's mel representation of the same audio is 3,000 frames of 80 values, half as many numbers arranged in a far more structured form. A linear-frequency STFT spectrogram keeps more high-frequency detail and can be roughly inverted with the Griffin-Lim algorithm, but uses many more frequency bins. Mel-frequency cepstral coefficients (MFCCs), made by applying a further cosine transform to the log-mel bands, compress further and were the classic input to older recognizers. Learned audio tokenizers produce discrete codes instead of a fixed transform. The Tacotron 2 paper's argument for mel is that it is easy to compute from audio, smoother than raw samples, and indifferent to phase within each frame, which makes squared-error training easier.

Those same properties are the limitations. A mel spectrogram discards phase, and the Tacotron 2 authors note that it discards even more information than a linear spectrogram, so turning it back into audio is a hard inverse problem that needs a trained vocoder or an approximation. The pooling of high frequencies blurs detail that matters for some tasks, such as fricative sounds, music cymbals or non-speech signals; Tacotron 2's authors explicitly accepted this trade, arguing that high-frequency noise bursts rarely need high fidelity for intelligibility. Settings matter: Tacotron 2's authors tried a 5 ms hop and found that the extra time resolution caused significantly more pronunciation problems. A mel spectrogram is also not a transcript, a pitch track or a quality score; it is a signal representation that a model must still interpret.

In practice the transform is cheap and the configuration is the hard part. Every model expects a specific sample rate, FFT size, window length, hop, number of mel bands, frequency range, mel formula, normalization and log scaling, and mismatching any of them silently degrades results. Whisper large-v3, for example, needs 128 bands where earlier Whisper models used 80, and HiFi-GAN's published setup uses an FFT size of 1,024 and a hop of 256 samples at 22.05 kHz. The safest approach is to use the feature extractor shipped with a model rather than reimplementing it.

04工作示例

Computing a Whisper-style mel spectrogram of a test tone

  1. Generate one second of a 440 Hz sine wave at 16,000 samples per second, giving 16,000 samples.

  2. Create torchaudio.transforms.MelSpectrogram with sample_rate 16000, n_fft 400, hop_length 160, n_mels 80 and f_max 8000.

  3. Apply it to the tone; with the default centered frames the output shape is 80 bands by 101 frames.

  4. Take the log of the output after clamping values below a small epsilon such as 1e-10.

  5. Find the band with the most energy: 440 Hz is about 550 mels on the HTK scale, which falls between the centers of filters 14 and 15 when counting from zero.

  6. Plot the result as an image with time on the x-axis and mel band on the y-axis, then repeat with a 4,000 Hz tone and compare how wide its band is.

它显示了什么

An 80 by 101 log-mel array with a bright horizontal line near band 15 for the 440 Hz tone. Check it by confirming the frame count equals 1 + 16000 // 160 and by seeing that the 4,000 Hz tone lands in a much wider band higher up the image.

现实世界的实施

OpenAI's Whisper converts all audio to 16 kHz and computes an 80-channel log-magnitude mel spectrogram on 25 ms windows with a 10 ms stride before the transformer encoder sees it.

Whisper large-v3 changed its input to 128 mel frequency bins instead of 80, according to its Hugging Face model card, while keeping the rest of the architecture the same.

Google's Tacotron 2 predicts 80-channel mel spectrograms spanning 125 Hz to 7.6 kHz from text, and a modified WaveNet turns them into audio, reaching a mean opinion score of 4.53.

HiFi-GAN converts 80-band mel spectrograms into 22.05 kHz speech 167.9 times faster than real time on one V100 GPU, and its training loss compares mel spectrograms of real and generated audio.

Audio libraries expose the transform directly: torchaudio's MelSpectrogram defaults to 128 mel filters and the HTK mel formula, while librosa defaults to the Slaney Auditory Toolbox version.

梅尔谱图的未来

Mel spectrograms remain the default front end for many speech systems, but research keeps testing their limits. Whisper large-v3's move from 80 to 128 mel bins shows that even well-established configurations are still being tuned, and its model card reports 10% to 20% fewer errors than large-v2 across many languages, although that release also changed training data, so the gain cannot be attributed to the input alone. On the generation side, the two-stage text-to-mel then mel-to-waveform pipeline described in the Tacotron 2 and HiFi-GAN papers is now one of several designs. Some speech and music systems instead model discrete codes from neural audio codecs or work on raw waveforms directly, which avoids the phase-loss problem but brings its own costs. Open questions include how much information a fixed mel transform throws away for non-speech audio, whether learned front ends consistently beat it, and how to keep preprocessing reproducible when small configuration differences change model behavior. Published ablations, like Tacotron 2's test of frame hop, remain the reliable way to judge those choices.

付诸实践

为什么这很重要

  • 交通与覆盖范围. 它通过转录、旁白和语音界面提高了可访问性。
  • 成本与预算. 媒体团队可以用更少的预算更快地交付精美的音频。
  • 速度与规模. 面向客户的系统可以处理更大规模的语音交互。

留意

  • 如果未征得同意,语音滥用和冒充风险就会增加。
  • 由于口音、方言或嘈杂的环境,准确性可能会下降。
  • 如果没有明确的标签,合成音频可能会被误认为是真实的语音。

后续步骤

  1. 获得语音捕获、克隆和重用的明确同意。
  2. 测试不同扬声器和背景条件下的质量。
  3. 定义人员必须审查或批准输出的时间。
  4. 标记合成音频并保留来源记录以供问责。

资料来源与延伸阅读

  1. Journal of the Acoustical Society of America (1937)A Scale for the Measurement of the Psychological Magnitude Pitch
  2. arXiv (Google)Natural TTS Synthesis by Conditioning WaveNet on Mel Spectrogram Predictions
  3. arXiv (OpenAI)通过大规模弱监督实现鲁棒语音识别
  4. Hugging Faceopenai/whisper-large-v3 model card
  5. arXiv (NeurIPS 2020)HiFi-GAN: Generative Adversarial Networks for Efficient and High Fidelity Speech Synthesis
  6. PyTorch documentationtorchaudio.transforms.MelSpectrogram
  7. librosa documentationlibrosa.mel_frequencies

不断探索

免费通讯

获取每日AI简报

每个工作日早晨提供三条经过验证的AI故事,使用简明英文撰写。永久免费,无广告。

每个工作日发送一封邮件。一键退订。我们永不出售或分享您的地址。

测试一下自己

Take the Mel Spectrograms quiz

每个答案即时反馈,课程通过后可获得可验证ID的可分享证书。

开始测验

支持免费AI教育。AI Understanding是一家501(c)(3)非营利组织——无广告,无付费墙,永不收费。 捐款

常见问题

什么是梅尔谱图?

A mel spectrogram is a picture of sound: it shows how much energy an audio signal has in each of a few dozen frequency bands, frame by frame over time, with the bands spaced on the mel scale so that low frequencies get finer detail, roughly as human hearing does. It is the standard input to speech recognizers such as Whisper and the standard intermediate output of text-to-speech systems such as Tacotron 2.

梅尔谱图代表什么?

A mel spectrogram maps spectral energy across time into mel-frequency bands.

mel尺度旨在表示什么?

Mel-frequency representations use a nonlinear frequency scale for audio analysis; the classic Davis–Mermelstein study evaluated mel-based features for speech recognition.

在进行mel滤波之前,通常使用哪种变换来获取短帧频率内容?

The librosa mel-spectrogram function computes a spectrogram from audio before mapping it to mel bands.

为什么要将功率谱图转换为分贝单位?

librosa power_to_db converts power values to decibels with a logarithmic ratio to a reference.

librosa中哪个参数控制mel频带的数量?

The librosa API lists n_mels as the number of mel bands to generate.