Lesson 2 / Unit 1: Audio Foundations
Speech Features
Turn samples into a time-frequency matrix and trace every dimension into the model.
Our recognizer consumes log-power spectra. We use ordinary FFT bins rather than a mel filter bank so the entire feature recipe fits in a few visible lines. This is a teaching choice, not a claim that linear frequency bins are better.
Feature Recipe
The input rate is 8,000 samples per second. The implementation uses a 256-point transform, a 200-sample Hann window and an 80-sample hop. These are respectively 32, 25 and 10 milliseconds at that rate. The real-input spectrum has 129 bins.
power = torch.stft(x, n_fft=256, hop_length=80, win_length=200, window=torch.hann_window(200), center=False, return_complex=True).abs().square()features = power.clamp_min(1e-8).log().TThe matrix is time × frequency. The clamp prevents the logarithm of zero. Consult the PyTorch STFT interface when changing window or centring behaviour; padding changes which input samples contribute to a frame.
Normalisation Scope
The lab subtracts the whole feature matrix’s scalar mean and divides by its scalar standard deviation. It does this independently for each utterance, without using another split’s statistics. That removes one kind of dataset leakage, but it still needs the whole utterance.
This recipe is therefore offline. When a prefix grows, its statistics change. Recomputing features on a longer prefix can change earlier frames even before the encoder sees them. Keep that fact for Causal Context.
Inspect Shapes
from lab import featuresx = features(audio) # audio from the previous lessonprint(x.shape, x.mean().item(), x.std().item())assert x.shape[1] == 129Record the frame count for two clips of different lengths. They share the frequency dimension, not the time dimension. Batch Construction will pad time and retain the true lengths.
Your output is a feature tensor with a documented rate, window, hop, transform size and normalisation scope. A spectrogram picture is useful only if you can reproduce the numbers behind it.
We do this work for teams shipping voice agents: Self-Hosted Voice Agent .
Edit this lesson on GitHub ↗Discussion
Questions, corrections and war stories welcome. Sign in with GitHub to comment.