Guide to AI and Data Courses · Lecture Notes
Chapter 19: Speech Recognition and Audio Processing
When we say "what's the weather tomorrow" to a phone, the right sentence appears within seconds. Yet all the microphone and the computer receive is a sequence of sixteen thousand numbers per second, with no word, syllable or silence boundaries marked. Speech recognition is the course about the road from that sequence of numbers to text: turning sound into numbers, extracting features that resemble what the ear hears, and choosing the most likely word sequence. These notes introduce the course and work through its three core ideas with small examples you can compute by hand.
Learning outcomes
- Digital sound
- Find the highest representable frequency from the sampling rate; compute the number of frames in a recording.
- Spectrum
- Compute the discrete Fourier transform of a tiny signal by hand and read off which frequency it contains.
- Mel and MFCC
- Convert Hz to the mel scale and follow the MFCC pipeline of triangular filters, logarithm and cosine transform.
- Decoding and evaluation
- Combine acoustic and language model scores to pick the most likely sentence; compute the word error rate of a sentence pair.
- Place of the course
- State what separates speech recognition from signal processing, natural language processing, deep learning and biometrics.
19.1What question does the course ask?
How are words, speakers and events extracted from a sound wave?
Speech recognition and audio processing study how to analyze the sound wave from a microphone and extract words, the speaker's identity or events in the environment. Sound is first converted to time-frequency features; the acoustic model and the language model together find the most likely text.
In written text words are separated by spaces and every letter is the same symbol each time. In speech there is often no silence between words, the same sound produces a very different waveform depending on the speaker, the speed and the surroundings, and neighboring sounds influence one another. That is why "map sound directly to words" does not work. The course asks: how do we get from an ambiguous, noisy and variable waveform to the most likely text?
The answer has two parts. First, sound is converted into features that change slowly and distinguish speech sounds (spectrogram, MFCC). Then the features are interpreted with two sources of knowledge: the acoustic model answers "which sound does this stretch of audio resemble?", and the language model answers "which word sequence is more likely in this language?". The two are multiplied, or their logarithms added, and the highest-scoring sentence is chosen.
19.1.1Beyond speech
The course is not only about recognizing speech. The same features are used to find out whose voice it is (speaker recognition), to tell events in the environment apart (a siren, breaking glass, a cough) and to turn text into natural-sounding speech (speech synthesis). The foundations of signal processing are covered in Digital Signal Processing; here we use only as much of it as we need.
19.1.2At a glance
| Criterion | Speech Recognition and Audio Processing |
|---|---|
| Approach | acoustic signal and language model |
| Input | audio waveform |
| Output | text, speaker, sound event |
| Success measure | word error rate (WER) |
| Math load | medium-high |
| Data needs | many hours of recordings |
19.2A short history
| Year | Milestone |
|---|---|
| 1952 | Bell Labs' Audrey system recognizes digits spoken by a single speaker. |
| 1962 | IBM Shoebox is demonstrated, recognizing sixteen spoken English words, including the ten digits. |
| 1970s | In DARPA-funded projects, Carnegie Mellon's Harpy system recognizes about a thousand words. |
| 1980s | Hidden Markov models (HMMs) become the standard method of speech recognition. |
| 2012 | Deep neural networks begin replacing Gaussian mixtures in acoustic modeling. |
| 2022 | OpenAI's Whisper is trained end to end on about 680,000 hours of multilingual data. |
19.3Core concepts
The rest of the course is built on the concepts below. The following sections work through the most important ones with examples.
| Concept | Meaning |
|---|---|
| Sampling | Measuring the sound wave thousands of times per second. |
| Spectrogram | A time-frequency image from the short-time Fourier transform. |
| MFCC | Features adapted to how the human ear perceives frequency. |
| Acoustic model | Predicts the sound unit (phoneme) from a stretch of audio. |
| Language model | Says which word sequence is more likely. |
| Decoding | Finding the most likely word sequence with beam search. |
| Speaker recognition | Verifying or identifying whose voice it is. |
| Word error rate | WER: the rate of insertion, deletion and substitution errors. |
19.4Turning sound into numbers: sampling, frames and the spectrum
A microphone converts the changes in air pressure into a continuous voltage. The computer measures this voltage at equal intervals; the number of measurements per second is the sampling rate. The most common value in speech recognition is 16 kHz, and 8 kHz on a telephone line. In the resulting sequence no frequency above half the sampling rate can be represented.
If the sampling rate is fs, the highest frequency that can be told apart in the sequence is fs / 2. A higher frequency appears as if it were as far below fs as it really is above it (aliasing).
The properties of speech (which sound is being uttered) can be treated as constant within a window of about 25 milliseconds. So the signal is cut into short, overlapping frames (for example 25 ms long, shifted by 10 ms) and the frequency content of every frame is computed with the discrete Fourier transform (DFT). Putting the spectra of the frames side by side gives the spectrogram: time on the horizontal axis, frequency on the vertical axis, energy as color.
The sampling rate is fs = 8000 Hz and the signal is x = 1, 0, −1, 0, 1, 0, −1, 0 (eight samples; this is the sequence cos(π n / 2)).
- Frequency resolution. In an N = 8 point DFT, bin k corresponds to k · fs / N = 1000 k Hz: the bins are 0, 1000, 2000, … Hz.
- The sequence repeats every four samples. One period takes 4 samples, so the signal has frequency 8000 / 4 = 2000 Hz.
- DFT. Computing it gives magnitudes |X0…X7| = 0, 0, 4, 0, 0, 0, 4, 0. All the energy is in bin k = 2 (and its mirror image k = 6).
- Reading it. Bin k = 2 means 2 · 1000 = 2000 Hz; the DFT has correctly found a 2 kHz tone.
In a real frame, 25 ms at 16 kHz is 400 samples; the DFT is usually computed after padding to 512 points, which gives a bin spacing of 16000 / 512 = 31.25 Hz.
At 16 kHz, 1 second = 16000 samples. The frame is 25 ms (0.025 · 16000 = 400 samples), the shift is 10 ms (160 samples).
- Start of the last frame. A frame must fit completely: it can start at sample 16000 − 400 = 15600 at the latest.
- Number of starts. ⌊15600 / 160⌋ + 1 = 97 + 1 = 98.
So one second of speech becomes 98 frames, each with one spectrum. Neighboring frames overlap by 15 ms (240 samples), so no information is lost at the boundary of a speech sound.
Choosing a low sampling rate just to save file size. In a recording made at 8 kHz, a 7 kHz sound appears as 8000 − 7000 = 1000 Hz: cos(2π · 7000 n / 8000) and cos(2π · 1000 n / 8000) give exactly the same samples. That is why high frequencies are removed with a low-pass filter before sampling; aliasing cannot be repaired afterwards.
19.5The mel scale and MFCC
A spectrogram gives one number per bin, which is 257 numbers for a 512-point DFT. Most of them are unnecessary for telling speech sounds apart, and neighboring bins depend strongly on each other. Moreover, our ears do not perceive frequency linearly: we hear the difference between 1000 and 2000 Hz as much larger than the difference between 7000 and 8000 Hz. The mel scale models this perception.
mel(f) = 2595 · log10(1 + f / 700), with f in Hz. This form sets the scale to about 1000 mel at 1000 Hz. (Versions with other constants also exist.)
MFCCs (mel-frequency cepstral coefficients) are produced in four steps: (1) compute the power spectrum of the frame; (2) pass the spectrum through triangular filters equally spaced on the mel scale and sum the energy in each filter; (3) take the logarithm of the energies (our perception of loudness is also logarithmic); (4) apply the cosine transform (DCT) to the logarithms and keep the first 12–13 coefficients. The DCT removes the dependence between neighboring filters.
We place three triangular filters over 0–4000 Hz. First we find the end points on the mel scale.
- Hz → mel. mel(1000) ≈ 1000, mel(2000) ≈ 1521, mel(4000) ≈ 2146. The 1000–2000 Hz interval is 521 mel; the 7000–8000 Hz interval is only 138 mel: high frequencies are compressed.
- Equal division on the mel axis. The range 0 to 2146 mel is split into four equal parts: 0, 536.5, 1073.0, 1609.5, 2146.1 mel.
- Back to Hz. With f = 700 · (10mel/2595 − 1) these five points become 0, 427, 1114, 2220, 4000 Hz. The filters lie between these points: their centers are 427, 1114 and 2220 Hz, and their widths grow with frequency.
- One tone, two filters. A pure 1000 Hz tone lies on the falling side of filter 1 and the rising side of filter 2. The weights: for filter 2, (1000 − 427) / (1114 − 427) ≈ 0.834; for filter 1, (1114 − 1000) / (1114 − 427) ≈ 0.166; filter 3 starts at 1114 Hz, so its weight is 0. The weights sum to 1.
Suppose the energies of the three filters in one frame are E = 100, 10, 1 (the values are illustrative).
- Logarithm. log10 E = 2, 1, 0.
- DCT. ck = Σj log Ej · cos(π k (j + 0.5) / 3), j = 0, 1, 2.
- c0 = 2 + 1 + 0 = 3 (overall loudness).
- c1 = 2 · cos(π/6) + 1 · cos(π/2) + 0 = 2 · 0.866 ≈ 1.73 (the tilt of the energy toward low frequencies).
- c2 = 2 · cos(π/3) + 1 · cos(π) + 0 · cos(5π/3) = 1 − 1 = 0.
The shape of the spectrum fits into three numbers: c = (3, 1.73, 0). Real MFCCs use 20–40 filters and keep the first 13 coefficients.
Assuming MFCC is the best feature in every situation. MFCC deliberately discards pitch information and concentrates on telling speech sounds apart. For recognizing musical notes or emotion, other features are needed. Deep acoustic models often take the log-mel spectrogram directly and skip the DCT step.
19.6Acoustic model, language model and word error rate
Given the acoustic feature sequence X, we look for the most likely word sequence W. Bayes' rule gives P(W | X) ∝ P(X | W) · P(W): P(X | W) comes from the acoustic model and P(W) from the language model. In practice the logarithms of the probabilities are added and the language model gets a weight λ: score(W) = log P(X | W) + λ · log P(W). Trying every candidate sentence is impossible; the Viterbi algorithm and beam search find the best candidates cheaply.
For the same audio there are two candidates: "recognize speech" and the similar-sounding "wreck a nice beach". The acoustic model finds the second slightly more likely; the language model finds the first much more likely. The log10 values (illustrative):
| Candidate | log P(X | W) | log P(W) | Score, λ = 0 | Score, λ = 1 |
|---|---|---|---|---|
| recognize speech | −42 | −5 | −42 | −47 |
| wreck a nice beach | −41 | −9 | −41 | −50 |
With the acoustic model alone (λ = 0), −41 > −42, so the second sentence wins; with the language model (λ = 1), −47 > −50, so the first sentence wins. For λ = 2 the scores are −52 and −59 and the gap widens. The language model is decisive in separating similar sounds.
19.6.1Word error rate
A system is evaluated by comparing the text it recognizes with a hand-written correct text (the reference). The minimum number of edits between the two sequences is counted in three kinds of error: substitutions (S), deletions (D) and insertions (I).
WER = (S + D + I) / N, where N is the number of words in the reference. Because of insertions, WER can exceed 1.
Reference: tomorrow the weather is nice (N = 5). System output: tomorrow weather was nice please.
- Alignment. tomorrow = tomorrow, the deleted, weather = weather, is replaced by was, nice = nice, an extra please at the end.
- Counting. S = 1, D = 1, I = 1; the minimum edit distance (Levenshtein distance) confirms it is 3.
- Rate. WER = 3 / 5 = 0.6, that is 60%.
Although most words are in place, was and please change the meaning; WER is a harsher measure than word accuracy.
Reading WER as "percent accuracy". WER = 60% does not simply mean 50% of the words are right; WER can also exceed 1. If the reference has five words and the system outputs eight words, none of them matching, the minimum number of edits is 5 substitutions and 3 insertions, that is 8: WER = 8/5 = 160%. Also, in agglutinative languages such as Turkish a single suffix difference makes a whole word wrong, so WER comes out higher than in English; some studies therefore also report the character error rate.
19.7How is the course taught?
19.7.1A typical workflow
A voice assistant records the question "what's the weather tomorrow" at 16 kHz, computes a spectrogram from 25 ms frames, the acoustic model gives phoneme probabilities for each frame, and the language model turns them into the most likely sentence. The text is passed to NLP for meaning.
19.7.2Topics over the semester
- Speech production and perception; phonemes
- Digital audio: sampling, framing, windowing
- Spectrograms, the mel scale and MFCCs
- Hidden Markov models and Gaussian mixtures
- Decoding with Viterbi and beam search
- N-gram and neural language models
- Deep acoustic models
- End-to-end recognition: CTC, attention, transducers
- Evaluation: word error rate
- Speaker recognition and verification
- Noise robustness and speech enhancement
- Speech synthesis (TTS)
This is a typical semester; the order and coverage vary between universities.
19.7.3Skills you will gain
- extracting features with librosa
- transcribing and fine-tuning with Whisper
- training models with torchaudio
- analyzing speech in Praat
- comparing systems by WER
19.7.4Try it yourself
The Try It: A Smart Home Recognizes Commands slide in the presentation lets you solve a typical problem of this course yourself: A smart home assistant must recognize four voice commands: yes, no, stop, start. Pick a command, change the speaker, raise the noise; the system compares the sound with stored templates.
19.8Are you ready for this course?
19.8.1Prerequisites
| Course | Why it is needed |
|---|---|
| Digital Signal Processing | spectrogram, MFCC, filters |
| Calculus | complex numbers, integrals and an introduction to Fourier series |
| Programming | array operations and reading audio from files in Python |
19.8.2Ask yourself
For every "no", the right-hand column shows where to start. If you can say "yes" to most of them, you are ready.
| Question | If not, start with |
|---|---|
| Can you explain how the Fourier transform splits a signal into frequencies? | Digital Signal Processing |
| Can you use conditional probability and Bayes' rule? | Probability and Statistics |
| Can you explain how a simple neural network is trained? | Artificial Neural Networks |
| Can you read an audio file in Python and plot its waveform? | An intro programming course, then a short NumPy and librosa tutorial |
19.9Neighboring courses
19.9.1Where it overlaps
| Relation | Course | Shared ground |
|---|---|---|
| builds on | This course builds on Digital Signal Processing | spectrogram, MFCC, filters |
| shares methods | Shares methods with Deep Learning | end-to-end speech recognition |
| shares methods | Shares methods with Natural Language Processing and Text Mining | language model, speech to text |
19.9.2Where it differs
- Digital Signal Processing: Signal processing filters and transforms any kind of signal; this course focuses on features and models specific to audio and speech.
- Natural Language Processing and Text Mining: NLP assumes it already has text; this course obtains that text from the sound wave and also handles audio events.
- Deep Learning: Deep learning offers general network architectures; this course adapts them to the time-frequency structure of sound and the alignment problem.
- Biometric Systems: Biometrics uses the voice as proof of identity, like a face or fingerprint; this course also analyzes what is said.
19.10In practice
19.10.1Applications
| Area | What is done |
|---|---|
| Voice assistants | Understanding commands to control a phone or home. |
| Captioning | Turning video, meeting and lecture recordings into text. |
| Call centers | Analyzing and routing calls. |
| Speaker verification | Voice-based banking and access control. |
| Sound event detection | Detecting breaking glass, sirens, coughs. |
| Speech synthesis | Turning text into natural-sounding speech. |
19.10.2Risks and ethics
| Risk | Explanation |
|---|---|
| Voice cloning | A voice imitated from a few seconds of recording is used in fraud and to defeat voice authentication. |
| Always-on listening | Devices waiting for a wake word can record by mistake, and a voice also carries health and emotion information. |
| Accent and dialect gaps | Error rates are clearly higher for some accents and dialects, so these speakers gain less from captions and assistants. |
| Hallucinated transcripts | End-to-end models can write sentences nobody said during silence or noise, which is dangerous in medical transcription. |
These issues are discussed in depth in Explainable AI and AI Ethics.
19.10.3Tools and datasets
Tools: librosa, Kaldi, Praat, torchaudio, ESPnet, SpeechBrain, Whisper.
Well-known datasets: TIMIT, LibriSpeech, Mozilla Common Voice, Switchboard, VoxCeleb, AudioSet.
19.11Common misconceptions
Speech recognition maps sound directly to words.
The same word sounds very different across accents, speeds and noise; the language model resolves the ambiguity.
The louder the recording, the better.
Clipping and reverberation hurt recognition; a clean, balanced recording is what matters.
A model that works well in English works in Turkish too.
Each language needs enough data; agglutinative Turkish also has a vocabulary problem to solve.
Audio processing is only about speech.
Music, environmental sounds and medical sounds (heart, cough) are also part of the course.
19.12Summary
- Sound is converted to a sequence of numbers by sampling; frequencies above half the sampling rate cannot be represented (the Nyquist limit).
- The signal is cut into 25 ms frames with a 10 ms shift; the DFT of each frame gives a spectrum, and the spectra together form the spectrogram (at 16 kHz, 1 second = 98 frames).
- MFCC passes the power spectrum through triangular filters on the mel scale, takes the logarithm and applies the DCT, matching the ear's perception of frequency and loudness.
- Recognition searches for the most likely sentence by combining the acoustic and language model scores: log P(X | W) + λ log P(W).
- Success is measured by the word error rate, (S + D + I) / N; it can exceed 1 and is not simply one minus accuracy.
19.13Test Yourself
A telephone line is sampled at 8 kHz. What is the highest frequency that can be represented in the sequence? At what frequency does a 5 kHz tone appear in the recording?
Answer
The highest frequency is 8000 / 2 = 4000 Hz. 5 kHz is above this limit, so it aliases: it appears at 8000 − 5000 = 3000 Hz (cos(2π · 5000 n / 8000) = cos(2π · 3000 n / 8000)).
A recording lasts 2 seconds at 16 kHz; the frame is 25 ms and the shift is 10 ms. How many frames are obtained?
Answer
There are 32000 samples, the frame is 400 and the shift is 160. ⌊(32000 − 400) / 160⌋ + 1 = ⌊197.5⌋ + 1 = 198 frames.
A 512-point DFT is used at 16 kHz. What is the bin spacing in Hz? Which bin does a 1000 Hz tone fall into?
Answer
The spacing is 16000 / 512 = 31.25 Hz. 1000 / 31.25 = 32, so bin 32 (counting bins from 0).
The sequence x = 0, 1, 0, −1 completes one period in 4 samples and the sampling rate is 8000 Hz. What is the frequency of the signal?
Answer
One period takes 4 samples; the frequency is 8000 / 4 = 2000 Hz. (The magnitudes of the 4-point DFT come out as 0, 2, 0, 2; the energy is in bin k = 1, and 1 · 8000 / 4 = 2000 Hz agrees.)
Reference: please open the door. System: open the doors. What are S, D, I and the WER?
Answer
Alignment: please deleted (D = 1), door replaced by doors (S = 1), no insertions (I = 0). N = 4, WER = (1 + 1 + 0) / 4 = 0.5.
For the same audio, candidate A has acoustic score −30 and language model score −4; candidate B has −28 and −8 (log values). Which wins for λ = 1? At which λ do they tie? What does this say about the role of the language model?
Answer
λ = 1: A = −30 − 4 = −34, B = −28 − 8 = −36; A wins. Tie: −30 − 4λ = −28 − 8λ, so 4λ = 2 and λ = 0.5. For λ < 0.5 B wins, for λ > 0.5 A wins. B is slightly better acoustically but much less likely as language; as the weight of the language model grows, such candidates are eliminated.
19.14After this course
After this course you can move on to natural language processing for the meaning of the text, to embedded AI for on-device voice assistants, or to biometric systems for voice authentication. Digital signal processing deepens the mathematics behind the subject.
Related careers: Speech recognition engineer, Audio processing engineer, Researcher, Embedded audio developer.
19.15Further reading
- D. Jurafsky and J. H. Martin, Speech and Language Processing: the standard textbook for speech recognition, acoustic and language models and WER.
- L. R. Rabiner and B.-H. Juang, Fundamentals of Speech Recognition (1993): a detailed account of sampling, feature extraction, hidden Markov models and decoding.
- X. Huang, A. Acero and H.-W. Hon, Spoken Language Processing (2001): speech production, perception, recognition and synthesis in one book.
19.16References
- Rabiner and Juang, Fundamentals of Speech Recognition, Prentice Hall, 1993.
- Huang, Acero and Hon, Spoken Language Processing, Prentice Hall, 2001.
- Jurafsky and Martin, Speech and Language Processing, 2nd ed., Prentice Hall, 2009.
- Rabiner, "A Tutorial on Hidden Markov Models and Selected Applications in Speech Recognition", Proceedings of the IEEE 77(2), 1989.
Related pages: Slides · Slides (PDF) · Course page