Radio from scratch · Chapter 18
Reading the band
Open Spectrum Lab and you see a line wiggling across the top, a curtain of color sliding down underneath, and a frequency in big digits. You can click a bump and hear it, drag an orange line until the hiss goes away, slide the passband off a whistle. Every one of those is a few lines of arithmetic on chapter 4's FFT. This chapter takes the screen apart, piece by piece, until you could build it yourself.
Start with a question you can almost answer already: if two stations sit 1.5 kHz apart, why does one setting of the screen show one bump and another show two?
How sharp can the picture be?
An FFT of N samples gives N bins: its N test speeds (chapter 4), each one standing for a narrow range of frequencies, and together they share the whole band the dongle hears. At 2.4 million samples a second, 1024 bins are 2.3 kHz wide each. Two carriers closer than that fall into the same bin, or into neighboring bins that blur together. Slide them apart, or give the FFT more samples:
More bins cost something: to measure finer frequencies, the FFT has to listen longer. 4096 samples at 2.4 MS/s is 1.7 ms of signal; 16 384 is 6.8 ms. It's the same trade a musician knows: you can't tell a note's exact pitch from a click, only from a held tone.
- Δf: how wide one bin is (Hz). fs: the sample rate. N: the FFT size. T: how much signal one FFT reads.
- Their product is always 1: halve the bin width and you double the listening time.
Spectrum Lab's chart is zoomed in 8× on a 2.4 MS/s band. Does zooming show more detail?
- Yes, 8× finer
- No: each bin just gets wider on screen. Detail only comes from a bigger N.
Calming the grass
Look at a spectrum of pure noise and it isn't flat: it's grass, every bin jumping up and down by several dB. A weak station a few dB above the noise hides in that grass. The fix is the oldest trick in measuring: take several FFTs and average them. Each bin's random ups and downs cancel out; a real carrier is there every time, so it stays.
There is a weak carrier in that picture, 5 dB above the noise. With one FFT, the grass is as tall as the carrier. With 16, it stands out clearly. Two details matter in the code: average the power (not the dB values), and spread the FFTs across the chunk, so each one sees different noise. Spectrum Lab averages 8 FFTs per waterfall row.
- σK: how much the noise bins scatter, after averaging K FFTs. One FFT scatters about ±5.6 dB; averaging 4 halves it, 16 quarters it. (Measured: 5.5, 2.3, 1.1 and 0.55 dB for 1, 4, 16, 64.)
One FFT's grass scatters ±5.6 dB. How many FFTs do you average to bring it down to ±2.8 dB?
Where is the noise?
Averaging calmed the grass so weak stations poke out, but how does the receiver know what color to paint the empty background of the waterfall (section 4), where to set an automatic squelch (section 7), or how many decibels a station stands above the hiss? To do any of those without making you fiddle with sliders every time you change bands, it needs to measure the noise floor — the level of the hiss — automatically. But the spectrum is noise and stations mixed together. Which bins are "just noise"?
Here is a trick that needs no knowledge of where the stations are. Take every bin's level and sort them:
Stations are tall, but they fill only a few bins. Sorted, they all end up at the right. The bin in the exact middle of the sorted list, the median, is noise, even with a dozen stations in the band. It only fails when stations cover more than half the band: in the chapter's script, five wide FM stations covering a third of the bins nudge it up by 1 dB.
With a floor, signal strength becomes a difference. In decibels, a difference is a ratio: a carrier 20 dB above the floor is 100 times stronger than the noise in one bin. That's the signal-to-noise ratio, SNR (chapter 3), which Spectrum Lab's meter shows.
A spectrum display is FFTs: more samples give finer bins, averaging calms the grass, the median bin is the noise floor, and every strength is a number of dB above it.
Why is the median bin the noise floor?
- Sort every bin's level from lowest to highest.
- Stations are tall but few: they all end up at the high end of the list.
- The bin in the exact middle has half the bins below it and half above.
- As long as stations fill less than half the bins, the middle one is a noise bin.
- So the median ignores the stations without knowing where they are; an average would be dragged up by them.
Where it breaks
It works only while noise is the majority. In this chapter's script, five FM stations covering 34% of the bins already lift the median by 1.1 dB (−56.3 to −55.2 dB). In the FM band, where stations sit every 200 kHz, more than half the bins can be stations, and the "floor" becomes a station's level. Spectrum displays then use a lower percentile (the 20th, say) or a floor learned on an empty stretch.
- +3 dB is twice the power, +10 dB ten times, +20 dB a hundred times (chapter 4).
- On a live waterfall the floor is also smoothed over time (90% old, 10% new each row), so the colors don't flicker.
From decibels to colors
The waterfall is the spectrum turned into one row of pixels, many times a second, each new row pushing the older ones down. Every bin's level becomes a color: the floor is black, and as the level climbs it goes blue, magenta, yellow. The contrast says how many dB above the floor count as full yellow:
Low contrast makes weak signals bright but turns every strong one into a yellow blob. High contrast keeps the strong ones apart but hides the faint ones in black. Computing the color for millions of pixels a second is the expensive part, so the colors are worked out once: a table of 256 colors, and each bin just looks up its slot. Changing the contrast repaints the whole history from the stored dB values, which is why Spectrum Lab keeps the last 256 rows of numbers, not just the picture.
A click becomes a frequency
You click a bump. Which frequency was that? The chart shows a slice of the band, from flo to fhi (the whole band, or less when zoomed). Where you clicked, as a fraction of the width, picks a frequency in between. Try it: click anywhere on this chart, then change the step.
Two small things make this pleasant. First, snapping: stations sit on a grid (FM broadcast every 100 kHz in the US, two-way radio every 12.5 or 25 kHz), so the click is rounded to the nearest channel. A sloppy click on a bump lands exactly on the station. Second, bin centers: an FFT bin covers a little range of frequencies, and its color is drawn as a column. If the frequency of bin i is put at the column's left edge instead of its middle, every click lands half a bin off. At 1024 bins over 2.4 MHz that's 1.2 kHz, enough to make a narrow signal sound wrong.
- x: where you clicked, in pixels from the left; W: the chart's width. f is an offset from the dongle's center C.
- Snap the absolute frequency C + f to the step s, not the offset: the dongle's center is rarely on the channel grid.
- Dragging the passband's edge sets the bandwidth: twice the distance from the carrier for AM and FM, the distance itself for one sideband.
Sliding the filter, not the station
You're listening to a voice on single sideband and a carrier whistles 2.6 kHz above the voice's carrier: inside the 2.8 kHz passband. Retuning would move the voice too. What you want is to slide only the filter (its passband, the range it lets through), off the whistle, and leave the voice where it is. Real radios call it passband tuning or filter shift:
Slide it down 500 Hz: the whistle falls outside, and the voice loses only its very top, which barely matters for speech. The trick is three steps, all from earlier chapters. Mix by the tuned frequency plus the shift, so the filter's center moves (chapter 5). Filter as usual (chapter 6). Then mix back by the shift, so the voice's carrier is at 0 Hz again and the demodulator hears it at the right pitch. For SSB, the sideband filter itself moves by the shift too.
- f0: the tuned frequency; s: the shift; h: the low-pass filter; *: convolution (sliding-and-multiplying).
- The script measures it: a +1.2 kHz shift makes an 800 Hz upper-sideband (USB) tone 59 dB quieter.
A squelch you can see
Chapter 11's squelch measured the channel's power, a number on its own scale. Spectrum Lab's squelch is an orange line on the chart, so it has to work in the chart's own decibels. It looks at the bins inside the passband and takes the loudest one. If that rises above the line, a signal is there: sound on.
Why the loudest bin and not the average? A narrow voice or a carrier fills only a few bins of a wide passband: their average barely moves, while their peak jumps right up. Why does the line sit above the floor, not on it? Because noise has peaks too. Of 22 noise bins in a 12.5 kHz passband, the loudest one sits a few dB above the median, every time. So "set it at the noise level" really means "just above the noise's own peaks". Spectrum Lab's Auto button does exactly that: it reads the loudest bin on an empty channel and puts the line 3 dB above it. In the script, that line let none of 100 chunks of pure noise through and opened at once for a carrier 23 dB over the floor.
The rest is chapter 11's two comfort features. Smoothing: the level is averaged with the previous one, so one noise spike doesn't open it. Hysteresis: it closes only 3 dB below the line, the shaded band in the picture, so a signal hovering right at the line doesn't chop.
Lab: read a real band
A live spectrum from a real dongle, with this chapter's dials: the FFT size, how many FFTs are averaged, the noise floor as a median, and the strongest signal's SNR. Click the chart to read a frequency. Try the airband (118–137 MHz) with averaging off and on: the short transmissions pop out of the grass.
In code
Show the real code (optional: everything it does is explained above)
Averaging FFTs (avgSpectrum): spread across the signal, averaged as power, back to dB at the end:
for (let s = 0; s + n <= total && count < frames; s += step, count++) {
const p = powerSpectrum(iq, s, n); // one FFT, in dB (chapter 4)
for (let i = 0; i < n; i++) acc[i] += 10 ** (p[i] / 10); // dB → power, then add
}
for (let i = 0; i < n; i++) acc[i] = 10 * Math.log10(acc[i] / count + 1e-20);
The noise floor and the colors: the median bin, smoothed, and a 256-color lookup table:
const median = (x: Float32Array) => x.slice().sort()[x.length >> 1];
wf.floor = 0.9 * wf.floor + 0.1 * median(db);
const LUT = new Uint32Array(256).map((_, i) => { const [r, g, b] = heat(i / 255); return (255 << 24) | (b << 16) | (g << 8) | r; });
// each bin: how far above the floor, scaled so `contrast` dB is the last color
px[i] = LUT[Math.max(0, Math.min(255, ((db[i] - wf.floor) * 255 / contrast) | 0))];
A click, snapped: position across the chart, then the absolute frequency rounded to the step:
const f = view.lo + fracX(e) * (view.hi - view.lo); // Hz from the dongle's center
const snap = (f: number) => Math.round((centerHz() + f) / stepHz()) * stepHz() - centerHz();
// image column whose middle is frequency f (each bin is drawn as a column from its frequency rightward)
const u = (f: number) => (f / fs) * WF_N + WF_N / 2 + 0.5;
Filter shift (Receiver): mix by tuning + shift, filter, mix back:
this.mixer = new Mixer(fs, offset + sh); // the filter's center moves by the shift
this.chan = new FirDecimator(this.taps, m1, 2);
this.back = sh ? new Mixer(this.chanFs, -sh) : null; // and the carrier comes back to 0 Hz
// SSB: the sideband filter slides too
const shift = (upper ? bw / 2 : -bw / 2) + passShift;
The squelch: the loudest bin in the passband, smoothed, with 3 dB of hysteresis:
for (let i = bin(a); i <= bin(b); i++) peak = Math.max(peak, db[i]);
sqLevel = 0.5 * sqLevel + 0.5 * peak;
if (!sqOpen && sqLevel > line) sqOpen = true;
else if (sqOpen && sqLevel < line - 3) sqOpen = false;
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:
N = 1024: bins 2344 Hz wide, one FFT = 0.43 ms of signal
N = 4096: bins 586 Hz wide, one FFT = 1.71 ms of signal
N = 16384: bins 146 Hz wide, one FFT = 6.83 ms of signal
two carriers 1.5 kHz apart: N=1024 shows 1 peak, N=4096 shows 2 peaks
averaging 1 FFTs: noise bins spread ±5.49 dB
averaging 4 FFTs: noise bins spread ±2.29 dB
averaging 16 FFTs: noise bins spread ±1.13 dB
averaging 64 FFTs: noise bins spread ±0.55 dB
median bin: -56.3 dB with noise alone, -55.2 dB with five FM stations covering 34% of the bins
click at 100.2093 MHz, FM broadcast, 100 kHz step → 100.2000 MHz
click at 146.7213 MHz, 2 m repeater, 5 kHz step → 146.7200 MHz
click at 119.0952 MHz, airband, 25 kHz step → 119.1000 MHz
filter shift +1.2 kHz on an 800 Hz USB tone: 59 dB quieter
empty channel: noise floor -56.3 dB, loudest noise bin in the passband -53.3 dB (3.0 dB above the floor)
squelch at -50.3 dB: 0 of 100 noise chunks opened it; a carrier 23 dB over the floor opens it
all checks passed
Check yourself
At 1.024 MS/s, how wide is one bin of a 4096-point FFT, and how long does that FFT listen?
1 024 000 / 4096 = 250 Hz per bin, and 4096 / 1 024 000 = 4 ms of signal.
Why is the noise floor the median bin and not the average?
Stations are few but very loud: a handful of bins 40 dB up drag the average far above the hiss. The median only looks at the middle of the sorted list, which stays in the noise as long as stations cover less than half the band.
Why snap the absolute frequency and not the offset from the dongle's center?
The dongle is tuned 250 kHz below the station to dodge its own spike, so its center is rarely on a channel. Channels are a grid of absolute frequencies.
Your squelch set right at the noise floor keeps opening on nothing. Why?
It compares the loudest bin in the passband with the line, and in pure noise the loudest of many bins is always a few dB above the median. Set it just above those peaks, which is what Auto does.
Last updated