Radio from scratch · Chapter 15

The network source

Before you startThis chapter uses:

One antenna on a rooftop can serve everyone. This site does exactly that: the dongle is plugged into a computer, a small server runs our driver there, and your browser receives the same bytes over the network that it would get over USB. Everything from chapter 3 on works unchanged.

Almost unchanged. A network is not a USB cable, and three new problems appear: data arrives in pieces of random size, there's far less of it to go around, and many people want the same dongle at once.

Pieces of any size, and the mirror-image ghost

Predict: a piece arrives with an odd number of bytes, and the next piece is decoded as if its first byte were an I. What happens?

Swapping the two shadows flips every arrow's direction: stations appear at mirror-image frequencies. Regrouping the bytes into pairs fixes it.

The server sends neat 256 KiB chunks. The network doesn't care: it delivers whatever has arrived, in pieces of any size, including odd sizes. Measured on this site's server: 48 of 120 pieces had an odd number of bytes. Remember the byte order: I, Q, I, Q… (↩ ch. 3). After an odd-sized piece, the next piece starts with a Q.

If the page decodes each piece on its own, assuming the first byte is always an I, half the stream ends up with I and Q swapped. Untick the box to see what that does:

Swapping the two shadows (↩ ch. 1) mirrors every arrow: counter-clockwise becomes clockwise, so +200 kHz shows up at −200 kHz too. Every station gets a ghost twin on the opposite side of the center, just as strong. A beginner's network receiver often has exactly this bug, and it's baffling until you know to look for it.

The fix is simple: collect pieces into a buffer, and only hand on an even number of bytes, keeping any leftover byte for the start of the next batch.

There's much less bandwidth to go around

Sending the whole band takes 4.8 MB/s per listener. A home connection uploads 40 megabits per second. How many listeners can it serve?

40 Mbit/s ÷ 8 = 5 MB/s: one listener at 4.8 MB/s. That's why the server sends each listener only a slice (the options just below).

Inside a computer, 4.8 MB/s is nothing. Across the internet, it's a lot, and a server has to send it to every listener separately. A typical home connection uploads 10–40 megabits per second (1–5 MB/s). Slide the number of listeners:

Sending the whole band (fine inside your home) runs out after a listener or two. The options below, from biggest to smallest, are everything you've learned, used to save bandwidth:

Over a network, keep I and Q bytes in pairs, and send each listener only the slice they need, mixed, filtered and decimated on the server.

Why does an odd byte make a mirror image?
  1. After an odd-sized piece, every pair is read as (Q, I) instead of (I, Q).
  2. Swapping the two numbers reflects each arrow across the 45° line: an arrow at angle θ ends up at 90° − θ.
  3. An arrow that was turning counter-clockwise by some angle each sample now turns clockwise by the same angle.
  4. So a station at +f now spins at −f: it appears on the opposite side of the center.
  5. Half the stream is swapped and half isn't, so each station shows up at both +f and −f.
Where it breaks

Slices only save bandwidth until the listeners add up. This chapter's script: ten listeners each sent the whole 1.024 MS/s band need 20 MB/s; each sent a 250 kHz slice, still 5 MB/s, too much for a 2.5 MB/s upload; a 48 kHz slice fits (0.96 MB/s), and compressed audio is tiny (0.08 MB/s). Past that, the server has to share one FFT among everyone, or limit how many people can listen.

Public web receivers such as KiwiSDR and OpenWebRX send each listener their own slice plus a low-resolution waterfall of the whole band. This site's server works the same way: the dongle captures a 2.4 MHz window, and each listener gets their own slice of it around their station (240 kHz in Spectrum Lab), mixed, filtered and decimated on the server, with a separate low-resolution picture of the whole window.

One dongle, many listeners

A dongle has one center frequency and sees one slice of the band. Every listener must stay inside it, each with their own window:

For each listener, the server runs its own mixer, filter and decimator: a channelizer. That costs computing power per listener: at 2.4 MS/s with a 70-weight filter, roughly 350 million multiply-adds per second each (↩ ch. 6), divided by the decimation (↩ ch. 7). Servers with many listeners use a trick: one big FFT (↩ ch. 4) already splits the band into thousands of narrow slices at once, and each listener's slice is assembled from the pieces they need.

One more rule from this site's server: if a listener can't keep up, the server skips chunks for them instead of storing them. Storing would make that listener's delay grow forever; skipping costs them a click but keeps everyone else unaffected.

Why does an FFT make a server with many listeners cheaper than one mixer, filter and decimator per listener?

One FFT splits the whole band into thousands of narrow slices at once, for everyone; each listener's channel is then put together from the slices around their station. The big cost is shared instead of repeated.
The math, step by step

\[ \text{listeners} = \left\lfloor \frac{\text{upload}}{\text{rate per listener}} \right\rfloor, \qquad \frac{40\,\text{Mbit/s} \div 8}{4.8\,\text{MB/s}} = 1.04 \to 1 \]

\[ \text{slice} = 2 \times f_\text{slice}\ \text{bytes/s}: \quad 48\,\text{kHz} \to 96\,\text{kB/s}, \quad \frac{4.8\,\text{MB/s}}{96\,\text{kB/s}} = 50\times \text{ less} \]

\[ \text{cost per listener} \approx 2 f_s \times \frac{\text{taps}}{M} = 2 \times 2.4\,\text{M} \times \frac{70}{8} \approx 42\,\text{M multiply-adds/s} \]

Lab: measure the stream

This reads the server's raw stream directly with fetch(), the way Spectrum Lab does, and shows how the pieces arrive. Watch how many have odd lengths. (Only works when the page is served by this site's server.)

In code

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

The server (Node's built-in HTTP): one dongle, many listeners, and skipping instead of storing for slow ones:

function fanOut(chunk) {
  for (const res of clients) {
    if (congested.has(res)) continue;                      // this listener is behind: skip, don't pile up
    if (!res.write(chunk)) { congested.add(res); res.once("drain", () => congested.delete(res)); }
  }
}

The browser side: read pieces, gather at least 64 KiB, hand on an even number of bytes, keep the leftover:

const { value, done } = await reader.read();               // a piece of any size
pending.push(value); size += value.length;
if (size < MIN_CHUNK) continue;
const even = size & ~1;                                   // round down to whole I/Q pairs
// … copy `even` bytes out, hand them on …
pending = size - even ? [lastPiece.subarray(-1)] : [];    // the odd byte starts the next batch

The API (three endpoints, documented in the build guide): GET /api/state, POST /api/tune, GET /api/stream. Browsers can't open raw network sockets, so the standard rtl_tcp protocol many SDR apps use isn't available to a web page, which is why our server speaks plain HTTP.

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:

pairs kept together: station -2.1 dB, mirror at −200 kHz -101.2 dB
pairs split       : station -8.1 dB, mirror at −200 kHz -8.1 dB
whole band 1.024 MS/s  × 10 listeners =  20.48 MB/s does NOT fit in 2.5 MB/s
250 kHz slice, 8-bit   × 10 listeners =   5.00 MB/s does NOT fit in 2.5 MB/s
48 kHz slice, 8-bit    × 10 listeners =   0.96 MB/s fits in 2.5 MB/s
Opus audio             × 10 listeners =   0.08 MB/s fits in 2.5 MB/s
server: 120 reads, average 51.2 KiB, 2.08 MB/s (expected 2.05), 48 reads of odd length
all checks passed

With pairs split, the ghost is exactly as strong as the station: there's no way to tell which one is real.

Check yourself

Why does an odd-sized network piece cause ghost stations?

The next piece starts with a Q byte. Read as an I, it swaps the two shadows, which mirrors every station to the opposite side of the center.

Ten listeners, each sent the whole band at 1.024 MS/s. How much upload does the server need?

2 MB/s each, so about 20 MB/s (160 Mbit/s): far more than most homes have.

Why does the server skip chunks for a slow listener instead of saving them for later?

Saved chunks would make that listener fall further and further behind, with ever-growing delay and memory. Skipping costs them a moment of sound but keeps them live, and doesn't slow anyone else down.

Last updated