Radio from scratch · Chapter 16
The whole receiver
- Chapters 3–8: samples, the spectrum, mixing, filtering, decimating, chunks
- Chapters 9–11: demodulating and playing the sound
- Chapters 12–15: the dongle, USB, the driver and the network
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?
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:
src/r820t.ts+src/rtlsdr.ts: the driver (chapter 14). Knows about USB and registers, nothing about signals.src/iq.ts: bytes and recording files → numbers (chapter 3).src/dsp.ts: every signal-processing block from chapters 4–11, as small streaming classes (chapter 8), plus their tests indsp.test.ts.src/waterfall.ts: spectra and the scrolling picture (chapter 4, explained step by step in the build guide).src/main.ts: Spectrum Lab, the app: buttons, plots, audio.server.ts+src/remote.ts: the network source (chapter 15).
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?
- Each block takes numbers in and gives numbers out, and keeps its own small memory (chapter 8).
- None of them knows whether the numbers came from a dongle, a network, a file or a test.
- 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).
- And the driver can be tested against a pretend chip, without signals at all.
- 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?
- The mixer
- The FM demodulator
- The audio filter
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}} \]
- The mixer's cost is fixed per input sample (one cosine, one sine, four multiplications); every block after the first decimation runs 8 or 48 times less often, which is why it's nearly free.
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:
- Made-up signals with known answers (chapters 2–11): a tone at exactly 1000 Hz must come out at 1000 Hz; a station mixed by 250 kHz must land at exactly 0.
- Comparing two ways of doing the same thing: the FFT against the plain winding machine (ch. 4); filtering-then-picking against computing only kept outputs (ch. 7); chunked against whole (ch. 8), bit for bit.
- Fake hardware: the driver against a pretend tuner and a pretend dongle (ch. 12, 14), checking every register it writes.
- Real hardware, checked without ears: the 19 kHz stereo pilot (ch. 9) proves that a real station was tuned, sampled and demodulated correctly.
- Real data, frozen: 4000 samples from this site's dongle are stored in the course (ch. 3), so tests can use real radio even when the dongle is unplugged (or stuck, which happened while writing this course).
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?
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:
- Stereo FM. Lock onto the 19 kHz pilot (a phase-locked loop: chapter 12's idea, in software), double it to 38 kHz, use it to shift the stereo band down (chapter 5), and add and subtract with the mono signal (↩ ch. 9).
- RDS. The station name and song title at 57 kHz: slow digital data (1187.5 bits per second). Your first digital demodulator.
- Aircraft tracking (ADS-B). Planes broadcast their position at 1090 MHz in bursts of on/off pulses. No audio at all: just find the pulses, decode the bits, check the checksum (chapter 22), and put dots on a map.
- APRS. Ham radio position reports at 144.39 MHz: tones that encode bits (AFSK), like an old modem.
- Weather satellites. Meteor-M satellites send pictures at 137 MHz as they pass overhead; you'll need to track the satellite and correct for its motion (Doppler, chapter 21: chapter 5's mixer, with a frequency that changes).
- A public WebSDR. This site's server, sending each listener only their slice (↩ ch. 15).
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