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.

Nothing is recorded or sent anywhere: the audio stays in this tab.

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:

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.)

Windows start at 0, 10, 20 … ms, and the last full window starts at 975 ms: \(\lfloor (1000 - 25)/10 \rfloor + 1 = 98\) frames. Chapter 6's convolution size rule, \(\lfloor (n - k)/s \rfloor + 1\), for time instead of pixels.

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
  1. 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).
  2. 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.
  3. 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".
  4. 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.
  5. 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.

0 recorded

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?

A detector checks the sound about 100 times a second, so "right almost all the time" can still mean many false wakes a day. Report false accepts per hour (and per day) next to the miss rate: chapter 12's precision-recall trade, on a clock.

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

5. What to carry forward

All lessons: AI from scratch. Notebook data: Free Spoken Digit Dataset (Zohar Jackson and contributors, CC BY-SA 4.0).

Last updated