Audio as a signal
Sample rate, Fourier transform, spectrogram: the foundations of all speech and sound processing, with worked numbers.
The idea
A sound is a vibration. A microphone measures it many thousand times a second. From those measurements you can compute which pitches are present, and from that comes an image a model can process.
The basic quantities
| Quantity | Meaning | Usual values |
|---|---|---|
| Sample rate | Measurements per second | 16 kHz speech, 44.1 kHz music |
| Bit depth | Resolution per measurement | 16 bits |
| Channels | Mono or stereo | Mono for speech |
| Window length | Time slice per analysis | 20 to 40 ms |
Computing a spectrogram
import numpy as np
def spectrogram(signal, sr=16000, window_ms=25, hop_ms=10):
n_win = int(sr * window_ms / 1000) # 400 samples
n_hop = int(sr * hop_ms / 1000) # 160 samples
window = np.hanning(n_win) # without a window function
# edge artefacts appear
frames = [signal[i:i + n_win] * window
for i in range(0, len(signal) - n_win, n_hop)]
spec = np.abs(np.fft.rfft(np.array(frames), axis=1))
return 20 * np.log10(spec + 1e-10) # in decibels
sr = 16000
t = np.arange(sr) / sr
# Two superimposed tones, 440 Hz and 880 Hz
signal = np.sin(2*np.pi*440*t) + 0.5*np.sin(2*np.pi*880*t)
S = spectrogram(signal, sr)
print(S.shape) # (98, 201): 98 time frames, 201 frequency bandsThe output shape shows the data reduction: 16,000 samples per second become 98 by 201 values, about an eighth of the volume, in a form far more usable for models.
The sampling theorem
At a 16 kHz sample rate, frequencies up to 8 kHz are representable. Speech carries its intelligibility overwhelmingly below 4 kHz, which is why even telephone quality at 8 kHz works. Sibilants sit higher, and they are what suffers first at a low sample rate.
The trade-off
Worked through: a 25 ms window gives a frequency resolution of about 40 Hz. To separate two tones 10 Hz apart you would need a 100 ms window, and would then no longer resolve events within those 100 ms. For speech, 25 ms is the usual compromise, because phonemes change on that scale.
From hertz to mel
The mel scale reflects that the ear resolves low frequencies more finely. Eighty mel bands between 0 and 8 kHz is the standard for speech models. The effect: about 30 bands lie below 1 kHz and only about 15 between 4 and 8 kHz. That uneven spacing is not imprecision but an adaptation to perception.
Practical preprocessing
- Resample to a uniform rate before anything else happens.
- Remove the DC offset and normalise the level, or recording loudness acts as a feature.
- Trim silence at the start and end but preserve internal pauses.
- With background noise, evaluate spectral subtraction before reaching for a larger model.
Related courses and sources
Hugging Face audio course
From the signal through spectrograms to recognition and synthesis, with code throughout. The practical companion to the speech and audio articles.
For anyone building speech technology themselves; assumes Python and some signal knowledge.
Robust Speech Recognition
Speech recognition that copes with noise, accents and language switching. The benchmark dictation solutions are measured against.
For anyone introducing dictation or transcription who needs a benchmark.