C9 · Lesson 53 · Project
Detecting keypad tones with the Goertzel algorithm
Press a key on a phone and it sends two tones at once, one for the key's row and one for its column: dual-tone multi-frequency, or DTMF. Phone exchanges, voicemail systems and alarm panels decode these by asking, for each of eight frequencies, how much of it is in a short burst of samples. A full FFT would compute hundreds of frequencies to use eight; the Goertzel algorithm computes just one DFT bin with a two-line recursion, the second-order resonator of lessons 9 and 36 tuned to the frequency in question. In this project you'll generate DTMF tones, write Goertzel, and build a decoder that survives noise.
- The keypad tone for the row and column you choose: two sinusoids added, with its spectrum below. Press Listen to hear it.
- The four row frequencies are 697, 770, 852 and 941 Hz and the four column frequencies 1209, 1336, 1477 and 1633 Hz. None is a harmonic of another, so speech rarely fakes a key.
- The decoder's job is to find the strongest row line and the strongest column line in a burst of 205 samples at 8 kHz, about 26 ms.
Predict
A decoder takes N = 205 samples at 8 kHz. How far apart are the frequencies a DFT of that length can resolve?
Hz. The closest DTMF tones are 697 and 770 Hz, 73 Hz apart: about two bins, enough to tell them apart. A shorter burst would blur them together; a longer one would take more time per key.
Name it
The keypad
| 1209 Hz | 1336 Hz | 1477 Hz | 1633 Hz | |
|---|---|---|---|---|
| 697 Hz | 1 | 2 | 3 | A |
| 770 Hz | 4 | 5 | 6 | B |
| 852 Hz | 7 | 8 | 9 | C |
| 941 Hz | * | 0 | # | D |
A key's signal is , sampled at 8 kHz.
The Goertzel algorithm
To measure frequency f in N samples, set and run
over the block from rest. Then the power at f is
which equals , the squared DFT bin, when f is the bin frequency . It's a resonator with poles on the unit circle at angle : a tone at f builds up inside it, other frequencies don't. One multiplication per sample per frequency, so eight frequencies cost 8N, against the FFT's for every bin.
Decoding
Measure the four row powers and the four column powers; the key is at the strongest row and the strongest column. Real decoders also check that the strongest tone is well above the rest, that the row and column levels are within a few dB of each other (the twist), and that the tone lasts long enough, to avoid triggering on speech.
Worked example
What are the Goertzel coefficients for the four row frequencies at 8 kHz?
Show the solution
- The coefficient.
- The four rows.
- What a decoder keeps. A decoder stores these eight numbers and nothing else.
Answer 1.7077 for 697 Hz, 1.6453 for 770 Hz, 1.5687 for 852 Hz and 1.4782 for 941 Hz.
Worked example
Which DFT bins (N = 205, 8 kHz) are nearest the row frequencies?
Show the solution
- Bin index.
- The four rows. so bins 18, 20, 22 and 24.
- Off-bin cost. The tones fall a little off-bin, which costs some power in each, but the nearest bins stay well separated, which is why 205 became the usual block length.
Answer bins 18, 20, 22 and 24.
Check
What is the Goertzel coefficient for 941 Hz at 8000 Hz?
Check
Which DFT bin of a 205-point DFT at 8 kHz is nearest 852 Hz?
Check
Two tones 82 Hz apart must land at least one bin apart. What is the shortest block length N, at 8000 Hz? (Give .)
Check
A decoder checks 8 frequencies in each block of 256 samples.
(a)
How many multiplications does Goertzel need per block?
(b)
How many complex multiplications would a radix-2 FFT of N points need? (Use the formula even if N isn't a power of 2.)
Check
A key's row tone has amplitude 1 and its column tone 0.5. What is the twist (column level relative to row), in dB?
Check
Write dtmf(key, fs=8000, n=205, a_row=1.0, a_col=1.0): the list of n samples of the key's two tones, , for keys '0'–'9', '*', '#' and 'A'–'D'.
Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.
Check
Write goertzel_power(x, f, fs): the Goertzel power of the samples x at frequency f.
Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.
Check
Write decode(x, fs=8000): return the key whose row and column frequencies have the largest Goertzel power in x.
Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.
Explain why
Why is Goertzel cheaper than an FFT for a DTMF decoder, even though the FFT is the "fast" algorithm?
The FFT is fast at computing all N bins of a DFT at once, about multiplications for the whole set. A DTMF decoder needs only eight frequencies. Goertzel computes a single bin with one multiplication per sample, so eight bins cost 8N real multiplications, about 1,640 for N = 205, against roughly 4,000 for a 256-point FFT (1,024 complex multiplications of four real ones each). It also needs no buffer of N samples or power-of-two length: it runs as samples arrive and can be tuned to the exact DTMF frequencies instead of the nearest bins.
Practice
Keep practicing
8 kinds of problem on this topic, dealt one at a time with new numbers every time, for as long as you like.