C953 of 55
0 XP
  1. Hook
  2. Play
  3. Predict
  4. Name it
  5. Check
  6. Explain

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?

Name it

The keypad

1209 Hz1336 Hz1477 Hz1633 Hz
697 Hz123A
770 Hz456B
852 Hz789C
941 Hz*0#D

A key's signal is sin⁡(2πfrowt)+sin⁡(2πfcolt), sampled at 8 kHz.

The Goertzel algorithm

To measure frequency f in N samples, set c=2cos⁡(2πf/fs) and run

s[n]=x[n]+cs[n−1]−s[n−2]

over the block from rest. Then the power at f is

P=s[N−1]2+s[N−2]2−cs[N−1]s[N−2]

which equals |X[k]|2, the squared DFT bin, when f is the bin frequency kfs/N. It's a resonator with poles on the unit circle at angle 2πf/fs: 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 N2log2⁡N 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
  1. The coefficient. c=2cos⁡(2πf/8000)
  2. The four rows. c697=1.7077c770=1.6453c852=1.5687c941=1.4782
  3. 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
  1. Bin index. k=fN/fs
  2. The four rows. k697=17.86k770=19.73k852=21.83k941=24.11 so bins 18, 20, 22 and 24.
  3. 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 fs/Δf.)

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, arowsin⁡(2πfrowi/fs)+acolsin⁡(2πfcoli/fs), 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?

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.