Radio from scratch · Chapter 3

Taking snapshots

Before you startThis chapter uses:

Until now our arrows have spun smoothly, and we could watch them every instant. A computer can't do that. It can only look at the arrow every so often: take a snapshot, write down two numbers (I and Q, from chapter 2), wait, take another. Each snapshot is called a sample, and your dongle takes up to 2.4 million of them every second.

That sounds like plenty. But snapshots can lie. You've seen it in films: a car speeds up, and its wheels seem to slow down, stop, and then spin backwards. Here's the same thing, slowed way down so you can follow it. The left wheel is real; the right one is what a camera taking 4 photos per second sees:

Watch the speed on the left climb steadily, while the camera's version speeds up, slows to a stop at 4 turns per second, then runs backwards. The camera isn't broken. It's doing the only thing it can. This chapter is about exactly what goes wrong, and the simple rule that keeps a radio safe from it.

Photos don't show how the arrow got there

Let's look at the photos one at a time, like a film strip. The slider sets how far the arrow really turns between two photos.

Start at 0.1: the arrow creeps forward a little each photo, and the photos show exactly that. Now try these:

A photo only records where the arrow is, never how it got there. So whoever looks at the photos can only assume the arrow took the short way round: never more than half a turn per photo, forwards or backwards. When the real motion fits that assumption, the photos tell the truth. When it doesn't, a different speed is pretending to be the real one. That pretending has a name: aliasing (an alias is a false name).

The whole story in one graph

Let's map every possible real speed to how it looks in the photos. Horizontally: the real speed, in turns per photo. Vertically: the speed the photos seem to show. Drag the slider along and watch the white dot.

With both shadows, the graph is a sawtooth. In the green zone, between −½ and +½ turn per photo, it's a straight line: what you see is what's real. Outside it, the line jumps back and repeats. Every speed outside the zone looks exactly like one inside it.

Now tick Only one shadow. The graph folds in half: +0.3 and −0.3 now look identical, just like chapter 1's game. The honest zone shrinks to 0 to ½.

This graph is the most famous rule in signal processing, usually stated as a formula. In plain words:

And here's the catch for radio: arrows outside the zone don't politely disappear. A station 1.5 MHz away would land on top of a station 0.9 MHz away in the other direction (the question below works it out). So the dongle has to remove everything outside the zone before it takes its photos. Its chip contains a filter that does exactly that; you'll see its numbers in the code below, and learn how filters work in chapter 6.

Wait: 98 million turns, 2.4 million photos?A station at 98.7 MHz turns 98.7 million times per second, far outside an honest zone of ±1.2 MHz. So how can the dongle photograph it at all? Because, before taking any photos, its tuner chip slows every arrow down by the frequency you tuned to. Tune to 98.0 MHz, and a station at 98.7 MHz now spins at only 0.7 MHz, one at 97.5 MHz at −0.5 MHz, and the photos only ever see these slow, leftover speeds. That's what "around where it's tuned" means. Chapter 5 shows how slowing arrows down works (it's one multiplication), and chapter 12 how the chip does it with electronics.

The dongle takes 2.4 million photos per second and you tune it to 98.0 MHz. A station at 99.5 MHz is 1.5 MHz above the center: that's 0.625 turns per photo. Where would it seem to be if nothing filtered it out? (Give the frequency it would pretend to be, in MHz.)

0.625 − 1 = −0.375 turns per photo, which is −0.9 MHz: it would pretend to be a station at 97.1 MHz, right on top of a real station there.

A game: freeze the wheel

One game to make the rule above something you can feel: a whole turn between photos is invisible. It's the same blindness that lets a station 2.4 MHz away land on top of yours if nothing filters it out first. A wheel is spinning at a secret speed, too fast to follow. You control how often the camera takes a photo. Find a camera speed that makes the wheel stand still in the photos.

Hint: watch the faded trail of the last few photos. If it creeps forward, the wheel turns a bit more than a whole turn between photos, so take photos a little faster. If it creeps backwards, a bit less, so slow down. When the trail collapses to one arrow, the wheel is turning exactly a whole number of times between photos.

This isn't just a trick. A stroboscope is a light that flashes like our camera, and mechanics use one (a "timing light") to make a spinning engine part look frozen so they can read marks on it. Aliasing, used on purpose.

Each photo is measured with a ruler

A digital snapshot has two limits. Sections 1–3 covered the limit in time: how often the camera flashes, which sets how wide a slice of the radio band you can see without aliasing. Now comes the limit in size: how finely each flash writes down the arrow's I and Q coordinates — and why your dongle has a gain knob you must set before you can hear weak stations. Each I and Q number has to be sent over USB, and your dongle packs each one into a single byte: a whole number from 0 to 255. It's like measuring with a ruler that has only 256 marks. The true value is somewhere between two marks, and gets written down as the nearest one.

Below, the blue curve is the true signal and the yellow dots are what gets recorded. The pink line underneath is the error, the difference, magnified five times.

Two words before you play. A bit is one binary digit, 0 or 1. Written side by side, each bit is worth twice the one to its right: 1, 2, 4, 8, …, so n bits count 2n marks, and 8 bits, one byte, give 256. The rightmost bit, worth 1, is the least significant.

Decibels, dB, measure ratios of strength, because radio deals in ratios of millions. Every 10 dB is 10× the power, so 20 dB is 100× and 50 dB is 100 000×. Power grows with the square of size: an arrow twice as long carries 4× the power (+6 dB), one ten times as long 100× (+20 dB). That's why halving the error is "6 dB". In a formula, dB = 10 · log10(power ratio), where the logarithm log10 counts powers of ten: log10 100 = 2, so 100× is 20 dB.

Things to try:

This is what the gain knob on a radio is for. It makes the signal bigger or smaller before it's measured. Too little gain and weak stations are lost between the marks; too much and strong stations clip. Good gain fills most of the ruler without touching the ends.

Noise, and how big a wiggle is

Why can't we just turn the gain knob up until every weak station spans plenty of marks on the ruler? Because the station never arrives alone. Unplug the antenna and the dongle's numbers still don't sit at 127.5: they jitter. That jitter is noise, a small random arrow added to every sample, with a new length and a new direction every time. Some of it comes from the receiver itself (warm electronics never sit perfectly still), some arrives through the antenna: the sky, the city's electrical gadgets, distant transmitters. Every receiver has it, and it decides how weak a station can be and still be heard — and how you set the gain knob in the live lab right below. (Where it comes from, in physics: ham lesson 11.)

Below, a station (one steady arrow, like the FM station whose real samples you'll see at the end of this chapter) arrives with noise on top, and goes through a gain knob and the 8-bit ruler. Left: every sample as a dot, inside the box of the whole ruler. Right: how often each byte value occurs, red at the ends where numbers clip. The readout underneath gives two numbers, the RMS and the SNR, explained just after you've played with it.

Predict first: you turn the gain up by 20 dB. What happens to how far the station stands above the noise?

Gain multiplies everything that arrived, station and noise alike, so their ratio stays put. Only the ruler can spoil it, at either end. Try it below.

Two numbers describe this. The first says how big the wiggle is: the RMS, short for root mean square. Take each number's distance from the middle (x = byte − 127.5, in counts), square it (that's its power, from the dB rule above), average the squares, and take the square root. The RMS is the noise's typical size. Noise sometimes jumps far past it: in this chapter's script, 0.28% of the numbers land beyond 3× the RMS and 0.005% beyond 4×. So noise at 32 counts RMS, a quarter of the way to the ruler's end, almost never clips (0.007% of numbers), while at 64 counts, halfway, 4.5% do.

The second says how much the station stands out: the signal-to-noise ratio, SNR, the station's power divided by the noise's power, in decibels. It decides whether a station can be heard, or a picture decoded, and gain can't raise it: gain only carries station and noise up the ruler together. What raises it is less noise reaching the ruler in the first place: a better antenna, or a quiet amplifier right at the antenna (chapter 12).

Gain moves the station and the noise up the ruler together. It can't improve the signal-to-noise ratio; it can only lose some, to rounding when everything is tiny or to clipping when it's too big.

Why does a whole extra turn vanish from the photos?
  1. A photo records only where the arrow points: its angle.
  2. Angles that differ by a whole turn point the same way.
  3. So an arrow that turns 1.1 turns between photos lands exactly where one turning 0.1 would: the photos can't tell them apart.
  4. Any speed is therefore indistinguishable from one shifted by a whole turn per photo, which is the formula f_seen = f − k·f_s (f_s is photos per second, k a whole number).
  5. Only within ±½ turn per photo is every speed's position sequence different from every other's: the honest zone.
  6. With one shadow, +f and −f also merge (chapter 1), so only half of that zone stays honest.
Where it breaks

"6 dB per bit" assumes a signal that fills the ruler. A signal far smaller than one mark is almost all rounding error, but a little noise rescues it: noise of about one mark makes the rounding jump around at random, so it behaves like more noise instead of wiping the signal out. This chapter's script shows both sides: with the noise at 0.5 counts RMS the station loses only 1.3 dB to rounding; at 0.05 counts it loses 4.8 dB. Designers sometimes add a little noise on purpose for this, and call it dither.

This site's satellite radio managed both mistakes in one day (16 October 2026). In the morning its recorded numbers sat near 1 count RMS, 92% of them exactly 127 or 128. In the afternoon, after an amplifier was added at the antenna, they reached 75 counts RMS with 13% clipped, and a whole satellite pass decoded nothing. Turning the gain down until the noise sat between about 10 and 20 counts RMS fixed it.

What real samples look like

Enough simulations. Below are 4000 real samples recorded from the dongle on this site's server, tuned to 98.45 MHz. Each dot is one snapshot: its I and Q drawn as a point, like chapter 2's arrow tips. The box marks the whole ruler, bytes 0 to 255 in both directions.

On the left, look at the shape: a fuzzy ring. Remember chapter 1: an FM station never changes the length of its arrow, only its speed. Here one strong station dominates everything else in this slice of the band, so its snapshots all land near one circle, at whatever angle the arrow happened to be. The fuzz is everything else (weaker stations and noise) added on top. Even in raw bytes, you can see that FM is "constant length, changing angle". These samples' RMS is 23.1 counts, with nothing clipped: a well-set gain.

On the right, zoomed into the yellow box, the dots sit on a perfect grid. Those are the ruler's marks: every I and Q had to be written as a whole number, so each dot snapped to the nearest crossing. You're looking at quantization, the rounding to the ruler's marks, in real data.

Lab: try it on real radio

Now do it live. Connect to a dongle and watch the cloud and a count of how often each byte value occurs (the histogram on the right). The red bars at the very ends mean clipping.

  1. Connect, and note how many of the 256 marks are in use.
  2. Raise the gain step by step to the maximum. Watch the cloud grow until it slams into the edges of the box and red bars appear at 0 and 255. That's clipping.
  3. Lower it to 0 dB. The cloud shrinks to a small blob that uses only a handful of marks. Weak stations are now lost between them.
  4. Find the gain that fills most of the box with no red bars. That's the best setting for this antenna and this spot. Its RMS will usually be somewhere around 10 to 40 counts.

The server's dongle is shared: if someone is listening to it, they'll hear your frequency and gain changes. "Use my USB dongle" needs Chrome, Edge or Opera on an https:// or localhost page (see Build for your own dongle).

Writing it down

fseen=f−k·fs −fs2<f≤fs2 signal-to-error≈6·bits+1.8 dB RMS=1N∑n=1Nxn2SNR=10·log10PstationPnoise dB

In code

Show the real code (optional: everything it does is explained above)

Choosing the photo rate. The dongle's chip runs its own measuring at 28.8 million samples per second, then thins that out to the rate you ask for. Our driver programs the ratio between the two:

A number starting with 0x is written in hexadecimal, base 16: the digits run 0–9 then a–f, so each hex digit holds exactly 4 bits and 0xff is 255. Code for chips uses it everywhere.

Names in this code: XTAL is the chip's own clock, 28.8 MHz, and rate the snapshots per second you ask for. The chip has no fractions, so the ratio (28.8 ÷ 2.4 = 12) travels as a whole number multiplied by 222 (about 4.2 million), which keeps 22 binary digits after the point. & 0x0ffffffc keeps it within 28 binary digits and sets the last two to 0: a format detail of the chip, done exactly as the open-source librtlsdr driver does it.

const ratio = Math.floor((XTAL * 2 ** 22) / rate) & 0x0ffffffc;   // XTAL = 28.8 MHz
// 2.4 MS/s → ratio = 12 × 2^22 exactly: keep one sample out of every 12

The filter that stops aliasing. Before thinning out, the chip filters away everything outside the new honest zone. These are the filter's weights, written into the chip when it starts up. Chapter 6 explains what the numbers mean:

const FIR = [-54, -36, -41, -40, -32, -14, 14, 53, 101, 156, 215, 273, 327, 372, 404, 421];

Reading the ruler. Each byte becomes a number between −1 and +1. The steps between marks are 1/127.5 ≈ 0.0078 apart, which is exactly the grid you saw in the zoomed real data:

out[i] = (u[i] - 127.5) / 127.5;

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. It also decodes the real samples. Its output:

9 turns/s and −1 turn/s give identical photos at 10 photos/s  (9 − 10 = −1)
+3 vs −3 turns/s: I alone identical; Q differs  → with both shadows, speeds from −5 to +5 are all distinct
 4 bits: signal is 25.7 dB above the measuring error  (rule of thumb: 6 dB per bit)
 8 bits: signal is 50.0 dB above the measuring error  (rule of thumb: 6 dB per bit)
12 bits: signal is 74.0 dB above the measuring error  (rule of thumb: 6 dB per bit)
sample 0: bytes (104, 151) → arrow I = -0.184, Q = 0.184
sample 1: bytes (100, 114) → arrow I = -0.216, Q = -0.106
sample 2: bytes (146, 104) → arrow I = 0.145, Q = -0.184
sample 3: bytes (136, 153) → arrow I = 0.067, Q = 0.200
4000 real samples at 1.024 MS/s: bytes 74..186, using 113 of 256 marks
at 2.4 MS/s the dongle sends 2.4e6 × 2 bytes = 4.8 MB every second
noise at   8 counts RMS: 0.000% of numbers reach the ends of the ruler
noise at  32 counts RMS: 0.007% of numbers reach the ends of the ruler
noise at  64 counts RMS: 4.5% of numbers reach the ends of the ruler
noise at 113 counts RMS: 26.0% of numbers reach the ends of the ruler
noise peaks: 0.28% beyond 3 × RMS, 0.005% beyond 4 × RMS
gain  0 dB: noise   0.05 counts RMS → SNR after the ruler 5.2 dB (10.0 in the air)
gain 20 dB: noise   0.50 counts RMS → SNR after the ruler 8.7 dB (10.0 in the air)
gain 40 dB: noise   5.00 counts RMS → SNR after the ruler 10.0 dB (10.0 in the air)
gain 52 dB: noise  19.91 counts RMS → SNR after the ruler 10.0 dB (10.0 in the air)
gain 60 dB: noise  50.00 counts RMS → SNR after the ruler 9.9 dB (10.0 in the air)
gain 66 dB: noise  99.76 counts RMS → SNR after the ruler 7.6 dB (10.0 in the air)
the real samples: 23.1 counts RMS, 0 of 8000 numbers clipped
all checks passed

Check yourself

A camera takes 24 photos per second of a wheel turning 25 times per second. What does the film show?

25 − 24 = 1: the wheel seems to turn slowly forward, once per second.

At 23 turns per second instead?

23 − 24 = −1: it seems to turn slowly backwards, once per second.

Your dongle takes 1.024 million I/Q samples per second. How wide a slice of the radio band can it see honestly?

1.024 MHz: from −0.512 to +0.512 MHz around the tuned frequency. (With only one shadow it would be half that.)

Why is turning the gain all the way up a bad idea next to a powerful station?

The strong station's signal overruns the ends of the ruler and clips. Clipping distorts it and creates fake signals that can cover the weak station you wanted.

You turn the gain up 10 dB and a weak station is no easier to hear. Why not?

Gain made the station 10 dB bigger and the noise 10 dB bigger too, so the signal-to-noise ratio didn't change. Only less noise (a better antenna, a quiet amplifier at the antenna) or a stronger station raises it.

A recording's samples have an RMS of 1 count. What's wrong, and what's lost?

The gain is far too low: the whole signal fits on two or three marks, so rounding adds error of its own and weak stations fall between the marks. Raise the gain until the RMS is somewhere around 10 to 40 counts.

Last updated