Radio from scratch · Chapter 8
Chunks of a live stream
So far, every picture processed a recording: a fixed list of samples, start to finish. Live radio isn't like that. It never starts and never ends. The dongle hands over a chunk of samples about every 55 ms, and each chunk has to be processed and played while the next one is already arriving.
The obvious approach is to treat each chunk as its own little recording. It almost works, and the way it fails is something you can hear.
Hear the problem
Below is a pretend FM station playing a steady tone, run through the receiver's real steps (mixer, filter + decimator, then two steps from later chapters: the FM demodulator, which turns the station into sound, chapter 9, and a last filter on that sound) chunk by chunk. Choose which step forgets what it was doing at the end of each chunk, and look at, then listen to, the result:
With "nothing", the tone is a smooth wave straight through every pink chunk boundary, and it sounds like a clean whistle. Every other choice puts a glitch at every boundary, and you hear it as a buzz or a crackle repeating at the chunk rate (with 8 ms chunks, 125 times per second). Real radio uses ~55 ms chunks, so the clicks would come about 18 times a second, on top of the music. Unbearable.
What each step must remember
Each step needs a different, small piece of memory from the previous chunk:
- The mixer needs its running angle (↩ ch. 5: the NCO). If it restarts at angle 0, its spinning arrow jumps to a new position at every boundary, and so does everything multiplied by it.
- The filter needs the last few dozen samples of the previous chunk (↩ ch. 6: the sliding bracket). Its bracket reaches back in time: the first outputs of a new chunk depend on samples from the old one. Without them, the bracket is mostly averaging empty space (zeros), so the output sags towards zero until the bracket has filled up again.
- The decimator needs to know where the next kept sample falls (↩ ch. 7). Chunks aren't a multiple of M long, so if a chunk ends 3 samples after the last kept one, the next chunk must start keeping at its own sample M−3, not at 0. Otherwise the spacing stutters.
- The FM demodulator needs the previous sample, because it measures how far the arrow turned since then (chapter 9). For the first sample of a chunk, "then" is in the previous chunk.
The solution is unglamorous and complete: each step is a small object that keeps its memory between calls. Processing a chunk updates the memory; the next call carries on from it. Processing a whole file is then just processing one very big chunk, so a single implementation serves both.
Each step of a live receiver keeps a little memory from the previous chunk, so processing in chunks gives exactly the same numbers as processing everything at once.
Why must chunked and whole agree exactly, not just closely?
- Every output sample is computed from the same input samples by the same arithmetic.
- With the memory handled right, the chunked run carries exactly the values the whole run had at each boundary.
- So each output is the same operations on the same numbers, in the same order.
- Computer arithmetic, though rounded, rounds the same way every time for the same operations in the same order.
- So the results are identical bit for bit, and any difference at all proves something at a boundary is wrong.
Where it breaks
"The same order" matters. Chapter 4's FFT and plain winding machine compute exactly the same thing in a different order, and their results differ by 3 millionths (rounding). A receiver that filtered with FFTs in chunks would pass a "close enough" test but not a bit-for-bit one, so its test would have to allow for rounding, and could then miss a small boundary bug.
How would you prove that the memory is handled perfectly, without listening for clicks?
The price of chunks: delay
Chunks also cost time. A chunk can't be processed until it has fully arrived, the filters add a little delay of their own (their output reflects the middle of their bracket, which is in the past), and the audio player keeps a small cushion of sound queued so that a late chunk doesn't cause a gap.
Smaller chunks mean less delay, but more overhead: every chunk costs a USB request, a function call per step, and a bit of bookkeeping. ~55 ms is a comfortable middle for listening. Two-way radio and live monitoring care about delay much more, and use smaller chunks.
Lab: hear the clicks on live radio
This plays the real station through the full receiver (you'll see its insides in chapter 9). Tick the box to make it forget everything at every chunk edge, and listen to what happens.
In code
Show the real code (optional: everything it does is explained above)
Here is where each step keeps its memory. The mixer saves its angle at the end of every call:
let ph = this.ph; // pick up where the last chunk left off
// … process this chunk …
this.ph = ph; // remember for the next chunk
The filter-and-decimator keeps the tail of the input (hist) and where the next kept output falls (next). It glues the remembered tail in front of each new chunk, so the bracket can reach back across the boundary:
const buf = new Float32Array(this.hist.length + x.length);
buf.set(this.hist); buf.set(x, this.hist.length); // old tail + new chunk
// … compute outputs at positions next, next + m, next + 2m, … …
this.next += count * m - n; // where the next kept sample falls in the next chunk
this.hist = buf.slice(buf.length - this.hist.length); // keep the new tail
The FM demodulator (chapter 9) remembers the previous sample (pi, pq). The test that proves it all works, from the receiver's test suite:
const whole = new Receiver(fs, 200e3, mode, bw).process(iq).audio;
const r = new Receiver(fs, 200e3, mode, bw);
for (let s = 0; s < iq.length; s += 2 * 12_345) parts.push(...r.process(iq.subarray(s, s + 2 * 12_345)).audio);
assert.ok(parts.every((v, i) => v === whole[i])); // identical, not just close
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:
WFM: 9753 audio samples, chunked = whole, bit for bit
NFM: 9753 audio samples, chunked = whole, bit for bit
AM : 9753 audio samples, chunked = whole, bit for bit
USB: 9753 audio samples, chunked = whole, bit for bit
LSB: 9753 audio samples, chunked = whole, bit for bit
a mixer that forgets its angle: output jumps by up to 1.01 at chunk edges (the signal itself is 0.5 long)
a filter that forgets: its first output is 3% of the right size, and it takes 69 samples to recover
FM demodulator at a chunk edge: with memory 0.853 rad, forgetting 0.000 rad
waiting for one 256 KiB chunk at 2.4 MS/s: 54.6 ms; plus filter delay and a 150 ms audio cushion
all checks passed
Check yourself
Why does a filter that forgets its history produce a dip at each chunk edge?
Its bracket reaches back into the previous chunk. Without that data it averages zeros for those positions, so the first outputs are too small until the bracket is filled with new samples.
Your chunks are 55 ms long and something forgets at every edge. How often do you hear a click?
About 18 times per second (1000 / 55).
Why compare chunked and whole processing bit for bit, instead of "close enough"?
If the memory is handled exactly right, the arithmetic is literally the same operations in the same order, so the results must be identical. Any difference at all means something at a boundary is wrong, even if it's too small to hear yet.
Last updated