Skip to content
Tutorials Center
My learning

Topics

Lesson 34 of 36 ยท Projects and Practice

Project: A Touch-Tone Detector with the Goertzel Algorithm

Build a DTMF (touch-tone) decoder from scratch: synthesize dual tones, implement the Goertzel algorithm, compare with the FFT, decode noisy digits, and test robustness.

Early telephones signaled the digits you dialed with pairs of tones, called DTMF (dual-tone multi-frequency). Each key plays one low tone (its row) and one high tone (its column) at the same time. A receiver has to decide, within a fraction of a second and with noise on the line, which two of eight frequencies are present. It is the perfect small project for everything in this chapter: sampling, the DFT, leakage and windows.

By the end you will have a decoder that turns an audio-like array of samples back into the digits. You will build it in four steps.

flowchart LR
    A["Digits"] --> B["Synthesize<br/>dual tones"]
    B --> C["Add noise"]
    C --> D["Goertzel at<br/>8 frequencies"]
    D --> E["Pick strongest<br/>row and column"]
    E --> F["Digits"]

Pipeline of the project.

The key layout

1209 Hz 1336 Hz 1477 Hz
697 Hz 1 2 3
770 Hz 4 5 6
852 Hz 7 8 9
941 Hz * 0 #

We sample at fs = 8000 Hz, as telephones do. Highest frequency 1477 Hz is far below the Nyquist frequency of 4000 Hz, so there is no aliasing concern.

Step 1: synthesize a dialed number

Each digit lasts 50 ms (400 samples) followed by 50 ms of silence. The helper below builds the whole signal and keeps the layout for the decoder.

Step 2: the Goertzel algorithm

A full FFT computes all N bins, but a decoder needs only 8 of them. The Goertzel algorithm evaluates a single DFT bin with a second-order recursion that costs about N multiplications per frequency and needs no complex arithmetic until the very end. For a target angular frequency w = 2ฯ€k/N:

s[n] = x[n] + 2 cos(w) s[n-1] - s[n-2], and the power is s[N-1]ยฒ + s[N-2]ยฒ - 2cos(w) s[N-1] s[N-2].

That recursion is a resonator, a filter with poles on the unit circle at e^(ยฑjw), tuned to the target frequency; the final formula reads off the magnitude squared of the DFT at that frequency. Because k does not have to be an integer, you can place the "bin" exactly on each tone.

Python Goertzel versus the DFT

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The two columns match to rounding error for each frequency; only 770 Hz and 1336 Hz are large. With N = 205 samples, the classic telephony block length, the bin width fs/N is 39 Hz, which keeps the tones from interfering with the neighbor tones.

Step 3: decode a noisy dialed number

The decoder takes one block of N samples per digit, runs Goertzel at the four row and three column frequencies, and picks the strongest of each group. A Hann window reduces leakage from the nearby tones. Zero-mean Gaussian noise with a standard deviation of 0.5 is added to a signal whose tone amplitudes are 1; the detector still works because the correct bins hold far more energy than the noise in any one bin.

Python Dial and decode

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Step 4: test the limits

How much noise can the decoder take? Repeat the experiment with growing noise and count how many of 200 random digits are decoded correctly. The signal-to-noise ratio per block improves with N, so longer blocks tolerate more noise. In practice, real decoders also check that the two tones have roughly equal strength, that no third tone is strong (to reject speech) and that the tone lasts at least 40 ms.

Python Accuracy versus noise level

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

You should see near-perfect decoding at low noise and a steady decline once the noise standard deviation passes the tone amplitude.

Ideas to extend the project

  • Add a second pass that rejects signals where the strongest row or column is less than twice the runner-up.
  • Replace the fixed 100 ms timing with an energy detector that finds where tones start and stop.
  • Generate a WAV file with scipy.io.wavfile and decode it from disk when you run the code locally.
  • Compare the cost of 8 Goertzel filters with a 256-point FFT.

The pattern here, "detect a few known frequencies cheaply," also powers tuners for musical instruments, vibration monitors that watch a machine's rotation harmonics, and radio-signaling systems.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What advantage does the Goertzel algorithm have over a full FFT in a DTMF decoder?

    A DTMF receiver needs 8 specific frequencies, so computing 8 single bins is cheaper than all N bins of the FFT.
  2. 2. Which pair of frequencies does the key 5 produce?

    Key 5 is in the second row (770 Hz) and second column (1336 Hz).
  3. 3. Why is a window applied to each block before Goertzel?

    Tapering the block lowers side lobes so that the tones in the other rows or columns leak less into each measurement.
  4. 4. Why is a sampling rate of 8000 Hz safe for DTMF?

    All eight tones lie below fs/2 = 4000 Hz, so none aliases.

Summary

  • DTMF encodes each key as one row tone plus one column tone.
  • Goertzel is a second-order resonator that measures one DFT bin cheaply.
  • A window, a block length near 205 samples at 8 kHz and "strongest row, strongest column" are enough for a working decoder.
  • Noise tolerance can be measured by simulation and improves with longer blocks.

Next: Project: Echo, Reverb and Inverse Filtering