Radio from scratch · Chapter 16

The whole receiver

Before you startThis chapter puts together everything so far:

Fifteen chapters ago, a radio station was a spinning arrow. Since then you've seen how to sample it, find it among hundreds of others, move it, isolate it, thin it out, keep it flowing, turn its speed or length into sound, and play that sound steadily. You've seen the chips that capture it and the USB messages that bring it in. This chapter puts everything on one page.

The map

2.4 million samples a second arrive and 50,000 reach the speakers. By what factor did the chain reduce the rate overall?

2,400,000 / 50,000 = 48 (in two steps on the map: 8, then 6). Everything after the first decimation is that much cheaper.

The complete receiver, from antenna to speaker. Each block shows how fast numbers flow through it, the chapter that explains it, and the file it lives in. Click any block to go back to its chapter.

Notice how the numbers shrink as they travel: 2.4 million pairs per second arrive, 300 thousand survive the first filter, and 50 thousand reach the speakers. The early, wide steps are simple (one multiplication per sample in the mixer); the expensive filtering happens once, and decimation makes everything after it cheap (↩ ch. 7).

How the code is organized

About 1 500 lines of TypeScript, no libraries, in a handful of files with one job each:

The key design decision is the layering. The driver doesn't know what a radio station is; the signal blocks don't know where their numbers come from (USB, network, a file, or a test). That's why every chapter's script can feed the real code with made-up signals, and why the same blocks run in the browser, in Node, and on the server. The build guide's source viewers walk through the most important files line by line.

The receiver is a chain of small streaming blocks that don't know where their numbers come from, so the same code runs on USB, the network, files and tests.

Why does layering make the receiver testable?
  1. Each block takes numbers in and gives numbers out, and keeps its own small memory (chapter 8).
  2. None of them knows whether the numbers came from a dongle, a network, a file or a test.
  3. So a test can feed any block a made-up signal whose right answer is known (a 1000 Hz tone must come out at 1000 Hz).
  4. And the driver can be tested against a pretend chip, without signals at all.
  5. Each layer is checked on its own; real hardware is needed only for the last check.
Where it breaks

Made-up signals only test what you thought of. Every bug in chapter 14's list (a string the dongle can't serve, a stalled chip after a fast reopen, a stuck tuner) passed every test with fake signals and fake chips, and showed up only on real hardware. That's why the test suite ends with a real station's 19 kHz pilot, and why abuse tests (open and close six times fast) exist.

How fast is it? Measured on your computer

Predict: which block uses the most computing time?

It's the only block that touches all 2.4 million samples a second, with a cosine and a sine for each. Measure it below.

A receiver has to process one second of radio in less than one second, every second, forever. How much room is there? Press the button to time each block on one second of radio at 2.4 MS/s, right here in your browser:

On a typical laptop, the whole FM receiver uses well under a tenth of one processor core. The mixer is the biggest cost, simply because it touches every one of the 2.4 million samples (computing a cosine and a sine for each); everything after the decimation is nearly free. If it ever weren't fast enough, the standard moves are: run the signal processing in a Web Worker (a second thread, so drawing never waits for math), use a cheaper NCO, or let the dongle send fewer samples per second.

The math, step by step

\[ \frac{2\,400\,000}{8} = 300\,000 \;\xrightarrow{\text{FM, filter}}\; \frac{300\,000}{6} = 50\,000 \ \text{samples/s} \qquad (8 \times 6 = 48) \]

\[ \text{headroom} = \frac{1\,\text{s of radio}}{\text{time to process it}} \]

How the receiver is tested

Every chapter ended with a script that checks its own claims; together they're the course's test suite (npm run course-check runs them all). The strategy, collected:

How can you test that a filter processing a live stream in chunks is right, when you don't know what the correct output is?

Run it once on the whole signal and once in chunks, and compare: they must agree bit for bit (chapter 8). You don't need to know the right answer to know two ways of computing it must match.

Where to go next

You now understand every part of a working software radio. Some projects that build directly on it, in rough order of difficulty:

For more depth: PySDR (free, the same ideas in Python), Richard Lyons' Understanding Digital Signal Processing (the intuition book), and Johnson, Sethares and Klein's Software Receiver Design (builds a full digital receiver). The build guide is the reference for writing your own app for your own dongle; Hello, dongle is an 80-line starting point.

Lab: the full app

Everything in this course, in one app: Spectrum Lab. Load a recording or the demo signals, or connect a dongle, click a station in the waterfall, and watch it go through every stage you've learned, numbered just like the course. Each stage has a link back to a short explanation.

Check yourself: the whole course

Why does a radio need both I and Q?

With one shadow you can't tell a frequency above the tuned one from one below it (ch. 1); they'd land on top of each other. With both, you can, so the full sample rate is usable (ch. 3), and mixing moves things in one direction only (ch. 5).

In one sentence each: mixing, filtering, decimating.

Mixing multiplies by a spinning arrow to slide the whole band (ch. 5). Filtering is a weighted average that keeps the middle and removes the rest (ch. 6). Decimating keeps every M-th sample, which is only safe after filtering (ch. 7).

FM and AM: where is the sound, and how is it recovered?

FM: in the arrow's speed; measure the angle between consecutive samples (ch. 9). AM: in the arrow's length; measure each sample's length and remove the average (ch. 10).

Why can't each chunk of live radio be processed on its own?

Every block needs a little memory from the previous chunk (the mixer's angle, the filter's last samples, the demodulator's last sample); without it there's a click at every chunk edge (ch. 8).

Your waterfall shows every station twice, mirrored around the center. What's the likely cause?

I and Q swapped somewhere: for example, network pieces of odd length read without keeping I/Q pairs together (ch. 15).

Last updated