AI from scratch · Elective · Listening: speech and wake words
Listening: speech and wake words
"Hey Siri", live captions, voicemail transcripts, the radio page on this site that writes down what a ham operator says: all of them turn sound into something a model can read. The trick is the same every time. Sound becomes a picture, a spectrogram, and then it's chapter 6 or chapter 7 again: a CNN or a transformer reading an image. This lesson builds the picture live from your microphone (or a test sound), shows why its axes are bent the way they are, and lets you train a tiny wake-word detector on your own voice.
1. Sound is a list of numbers
A microphone measures air pressure thousands of times a second: 8,000 for a phone call, 16,000 for most speech models, 44,100 for music. One second of speech at 16 kHz is 16,000 numbers. You can't hear much in that list directly, because what matters is which frequencies are present and how they change, so the next step is to measure those.
Sound arrives as thousands of pressure measurements a second; speech models start from those numbers.
2. The spectrogram: frequencies over time
Cut the sound into short overlapping windows (25 ms, every 10 ms), and in each window measure the strength of every frequency with the Fourier transform. Stack the windows side by side: time across, frequency up, color for strength. Say a long "eeee", then "oooo", then whistle:
Vowels show as stacks of horizontal bands (the voice's pitch and its harmonics, shaped by the mouth into formants), consonants like "s" as noisy smears high up, silence as dark. Two adjustments make this picture the standard input of speech models:
- Mel scale. Ears tell 100 Hz from 200 Hz easily and 7,100 from 7,200 hardly at all. The mel scale spaces frequency bands like hearing does, narrow at the bottom and wide at the top, typically 40 to 80 bands.
- Log of the energy. Loudness is perceived (and best modeled) logarithmically: a whisper and a shout differ by a factor of thousands in power but by a few steps in log.
The result, the log-mel spectrogram, is a small image: one second of speech becomes about 100 frames × 64 bands.
One second of audio, 25 ms windows every 10 ms. About how many frames? (A whole number.)
A log-mel spectrogram turns speech into an image of frequency over time, with frequency and loudness bent the way hearing bends them.
Why the Fourier transform finds the frequencies
- The Fourier coefficient for frequency \(f\) is a dot product (chapter 1) of the window with a sine wave at \(f\) (and with a cosine, for the phase).
- If the window contains that frequency, the signal and the sine rise and fall together and the products add up; for other frequencies they drift in and out of step and cancel.
- Sines of different frequencies (that fit the window a whole number of times) are perpendicular to each other: their dot product is zero. So each coefficient picks up its own frequency and nothing else: a change of basis (linear algebra 2) from "time" to "frequency".
- The fast Fourier transform computes all of them in \(n \log n\) steps instead of \(n^2\), which is what makes a live spectrogram possible.
- Short windows give sharp timing but blurry frequency; long windows the reverse. 25 ms is the classic compromise for speech, short enough to follow syllables, long enough to resolve the voice's pitch.
3. Models that listen
Once speech is an image, the rest of the path applies. In the practice notebook a small CNN (chapter 6) reads log-mel spectrograms of the Free Spoken Digit Dataset (3,000 recordings of "zero" to "nine" by six speakers): 97% right on a random split, but 79% on a speaker it never heard. The random split had the test speaker's own voice in training: features and leakage's group leak, in sound. For full transcription, Whisper (OpenAI, 2022) is chapter 7's transformer: an encoder reads 30 seconds of 80-band log-mel, a decoder writes the text token by token (the seq2seq elective). This site's radio page runs Whisper on its own receiver to caption what it hears.
Speech models read log-mel images: CNNs for short commands, encoder-decoder transformers like Whisper for transcription, and they must be tested on voices they never heard.
4. A wake word, trained on your voice
A wake-word detector listens all the time on a tiny chip, so it must be small and must almost never fire by mistake. Here's the simplest kind, template matching: record your word a few times; the detector keeps their log-mel pictures, and compares every new second of sound with them (cosine similarity, chapter 7). Pick a short phrase like "hey blip". Start the microphone (section 1) first.
Say your word, then say other things: similar-sounding words, your word in a different voice, music. Lower the threshold and it fires more on everything (false accepts); raise it and it misses you (false rejects). Real detectors are small CNNs trained on thousands of speakers and hours of non-wake audio, and they're judged by false accepts per hour at a fixed miss rate.
A smart speaker wakes by mistake once every 2 hours of TV in the room. Is that good?
- Yes: it's right almost all the time
- No: false accepts are judged per hour of background audio, and products aim for well under one a day
- Only if it also misses the wake word
A wake-word detector trades misses against false wakes, measured per hour, because it judges the sound a hundred times a second, all day.
Where it breaks: other voices, other rooms, other people
- Voices it never heard. The notebook's 97% → 79% is the gentle version. Accents, children's voices and speech impairments are under-represented in most training data, and accuracy for them is often far lower. Test by speaker group (chapter 12's slices).
- Noise and distance. A word spoken across a room over a TV is a different picture from one spoken into a phone. Training with added noise and room echoes (augmentation, the deep CNN lesson) is standard.
- Your template detector. It knows one voice and one way of saying the word; another person, or you with a cold, may not match.
- Privacy. An always-listening device decides locally whether to start sending audio. False wakes send audio nobody meant to share, which is why the false-accept rate is a privacy number, not just a quality number.
5. What to carry forward
- Audio: samples per second; speech models usually take 16 kHz.
- Short-time Fourier transform → mel bands → log: the log-mel spectrogram, an image.
- CNNs for commands and wake words; encoder-decoder transformers (Whisper) for transcription.
- Test on new speakers; measure wake words in false accepts per hour.
All lessons: AI from scratch. Notebook data: Free Spoken Digit Dataset (Zohar Jackson and contributors, CC BY-SA 4.0).
Last updated