Radio from scratch · Chapter 7
Throwing away snapshots
- Chapter 3: the folding graph (aliasing: too-fast arrows pretend to be slow ones)
- Chapter 6: filters (a weighted average that keeps the middle of the band)
- Chapter 5: centering a station
After chapters 5 and 6, our station sits alone in the middle of the band. Everything else has been filtered away. But look at what we're still carrying: the dongle sends 2.4 million samples per second, enough to describe a 2.4 MHz-wide slice of radio (↩ ch. 3), and our FM station is only 0.2 MHz wide. More than 90% of every sample's information is now empty space.
Every later step, the demodulator (which turns the station into sound, chapter 9) and the audio, would have to grind through all those useless samples. So the next move is to keep only some of them: one in every M. This is called decimation (from the Roman punishment of removing one in ten; we remove far more). It's simple, but it has one trap, and it's exactly the trap from chapter 3.
What keeping every M-th sample does
Keeping one sample in M is like using a camera that takes M times fewer photos. The honest range shrinks to 1/M of what it was (↩ ch. 3: the honest zone). And everything that used to be outside that new, narrower range doesn't vanish: it folds into it.
Here's the picture that makes it obvious. Cut the original spectrum into M slices, each as wide as the new range. Decimation stacks all the slices on top of each other and adds them up. The middle slice (yellow) is the one you want; every other slice gets dropped onto it.
The band here is the demo from chapter 5 with its FM station centered: the AM station now at −350 kHz and a tone at +150 kHz. This demo is sampled at 1.024 MS/s (not the full 2.4), so it spans ±512 kHz, and keeping one in M leaves 1024 ÷ M kS/s.
- With the filter (the default), every slice except the middle one is already empty. Stacking empty slices adds nothing, and the result is just your station, now described by M times fewer numbers.
- Untick the filter: the other slices still hold the AM station and the tone. Stacked on top of the middle, they land right next to your station or on top of it. And once they're mixed in, nothing later can separate them again: they're indistinguishable from real signal.
- Push M to 6 or more: the new range (1024 ÷ 6 ≈ 170 kHz or less) becomes narrower than the station itself (200 kHz). Now even the station's own edges fold over. The filter can't save you here: you've asked for too few samples.
So the rule is: filter first, then decimate, and choose M so the new sample rate stays comfortably above the width of what you're keeping.
Keeping every M-th sample folds everything outside the new, narrower range on top of what you keep, so filter first, and compute only the outputs you keep.
Why does every slice land on the middle one?
- Keeping one sample in M is taking photos M times less often (chapter 3).
- The new photo rate is f_s / M, and two speeds that differ by a whole multiple of it give identical photos (f_seen = f − k · f_new).
- So the band, cut into slices f_new wide, shows every slice at the same place: on top of the middle one.
- Whatever is in any slice adds to what's in the middle, and from then on it's indistinguishable.
- Hence filter first: empty the other slices, then nothing lands on the station.
Where it breaks
Even filtering first isn't perfect: the filter's leftovers fold in too. In this chapter's script, a neighbor at +350 kHz, filtered by 61 weights before decimating to 256 kS/s, still lands on the station at −74 dB instead of vanishing. Usually harmless; with a neighbor 80 dB stronger, it isn't. That's why the receiver leaves a margin (the 1.5× rule, at the end of this chapter) and steepens the filter where the folds would land.
The new rate is 256 kS/s (M = 4, from 1.024 MS/s). An unfiltered neighbor sits at +350 kHz. Where does it land? (Hint: chapter 3's sawtooth subtracts whole sample rates until it fits in −128…+128 kHz.)
Only compute what you keep
Filtering every sample and then throwing most of the results away would be wasteful. A filter's output at one moment doesn't depend on the output at the moment before; each is its own weighted average (↩ ch. 6: the sliding bracket). So just don't compute the outputs you'll throw away: slide the bracket M samples at a time.
Same results, M times less work. This one idea is what makes software radio affordable on a laptop, and it's why the filter and the decimation are written as a single step in our code.
How the receiver chooses M
Our receiver picks the biggest M that keeps the new rate at least 1.5 times the width of the signal it's keeping. The extra half leaves room for the filter's edge (chapter 6: no filter is a perfect rectangle) so that whatever folds in lands in the margin, not on the station. It also never goes below 48 000 samples per second, the rate sound cards like, so the audio step later has an easy job:
Estimate first: the dongle sends 2.4 million samples a second and the receiver keeps every 8th. How many thousand samples a second are left?
- Broadcast FM (200 kHz wide) at 2.4 MS/s: keep every 8th → 300 000 samples per second. Why not go straight down to 48 000 here? Because in the air, the FM arrow's speed swings across 200 kHz; only after chapter 9 turns that speed into a 15 kHz audio wave can we filter and decimate a second time down to speaker speed.
- Narrow FM, AM, single sideband (chapter 10's modes, 3–12.5 kHz wide): keep every 50th → 48 000 per second. In the code below, USB and LSB are upper and lower sideband, nothing to do with the USB cable.
From here on, the receiver works on 8 to 50 times fewer numbers than the dongle delivered.
Lab: zoom in on a real station
Your station, centered, then filtered to ±100 kHz and decimated. The bottom picture covers a range M times narrower, so the station fills it: decimation is also a zoom.
- At M = 3 (1.024 MS/s → 341 kS/s), the station fills most of the picture, with its edges well inside.
- At M = 5 (205 kS/s), the range barely fits the station. At M = 6 or more it doesn't, and the station's own edges fold over.
Writing it down
- Chapter 6's filter, evaluated only at every M-th input position: output number m sits at input position M·m.
- The new rate, and the rule that the filter must remove everything outside the new honest range first.
In code
Show the real code (optional: everything it does is explained above)
The receiver's filter-and-decimate is one class, FirDecimator. Its heart: a loop over only the outputs we keep, stepping M input positions each time, with chapter 6's weighted sum inside:
Names in this code: m is M, the "keep one in M"; o counts the outputs we keep; j is the input position of the output being computed, jumping m each time; this.next is where the first kept output falls in this batch (chapter 8). The inner loop is chapter 6's weighted sum, unchanged.
for (let o = 0, j = this.next; o < count; o++, j += m) { // j jumps m inputs per output
const base = (t - 1 + j) * 2;
let r = 0, i = 0;
for (let k = 0; k < t; k++) { // chapter 6: the weighted sum
r += buf[base - 2 * k] * h[k];
i += buf[base - 2 * k + 1] * h[k];
}
out[2 * o] = r; out[2 * o + 1] = i;
}
And the receiver's choice of M and of the filter (Receiver):
Names in this code: fs is the dongle's sample rate and bw the station's width; m1 is M, so chanFs is the rate after keeping one in M. half is how far the station reaches each side of 0 Hz. firLowpass(cutoff, edge) is chapter 6's filter designer.
const m1 = Math.max(1, Math.floor(fs / Math.max(48e3, 1.5 * bw))); // biggest M keeping ≥ 1.5 × bandwidth
this.chanFs = fs / m1;
// anything between chanFs − half and fs/2 would fold onto the station, so that's where the filter must reject
this.taps = firLowpass(half / fs, Math.max(this.chanFs - 2 * half, this.chanFs * 0.1) / fs);
The filter's edge is made exactly as wide as the margin we left: steep enough that nothing folds onto the station, no steeper (chapter 6: steepness costs weights).
Where this chapter's numbers come from. The numbers this chapter computes come from a short checking script behind this page, which also checks each claim and stops if one is wrong. Its output:
no filter: the +350 kHz neighbor reappears at 94 kHz (350 − 256 = 94)
with a 61-weight filter first: what's left of it is -74 dB
filter-everything-then-pick and compute-only-kept agree on all 16384 outputs, with 4× less work
WFM ( 200 kHz wide): keep every 8 → 300 kS/s
NFM ( 12.5 kHz wide): keep every 50 → 48 kS/s
AM ( 10 kHz wide): keep every 50 → 48 kS/s
USB ( 2.8 kHz wide): keep every 50 → 48 kS/s
all checks passed
Check yourself
Why must you filter before decimating, not after?
Decimating stacks every slice of the spectrum onto the middle one. Anything still in the other slices lands on your station and can never be separated again. Filtering afterwards is too late.
A 300 kHz-wide signal at 2.4 MS/s. Can you keep every 10th sample?
No: that leaves 240 kS/s, narrower than the signal itself, so its edges would fold over. Every 8th (300 kS/s) is the absolute limit. The receiver's 1.5× rule asks for at least 450 kS/s, so every 5th (480 kS/s) is the most it would thin out.
Why compute only every M-th output instead of all of them?
Each output is its own weighted average and doesn't depend on the others, so the ones you'd throw away don't need computing at all: M times less work, identical results.
Last updated