Radio from scratch · Chapter 22

Pictures that survive errors

Before you startThis chapter uses:

Chapter 21 got from the antenna to clean QPSK symbols, two bits each. Clean, but not perfect: at the edges of a pass a few percent of them land in the wrong quarter, and each wrong symbol is one or two wrong bits. A weather picture is compressed (this chapter's last section), so a single wrong bit can ruin a whole line of it. This chapter is about the layer that makes that not matter. The question to keep in mind: how can a picture come through perfectly when some of its bits arrive wrong?

Mixing bits: XOR

If the satellite sent each picture bit only once, a single noisy symbol from chapter 21 would flip that bit forever. To let a bit survive noise, the transmitter mixes each data bit into many sent bits before broadcasting so the survivors can outvote a mistake. That whole protection layer is built from one reversible operation on bits. XOR ("exclusive or", written ⊕) takes two bits and gives 1 if they differ, 0 if they're the same: 0⊕0 = 0, 0⊕1 = 1, 1⊕0 = 1, 1⊕1 = 0. One way to see it: XOR with 1 flips a bit, XOR with 0 leaves it alone. So doing it twice with the same bit undoes it, x ⊕ k ⊕ k = x, which is how the satellite's scrambling of every frame is undone too. (In hardware it's one logic gate: Electronics chapter 12.)

The satellite's encoder keeps the last seven data bits in a row. For every new data bit it sends two bits, each the XOR of a different selection of those seven (marked above). Send a few bits and watch each data bit leave its mark in fourteen sent bits as it slides through.

Fixing errors before they happen: the convolutional code

At the edge of a pass, a few percent of the symbols are on the wrong side of a line. That's why the encoder above sends two bits for every data bit: every data bit leaves traces in 14 transmitted bits, so a few of them can be wrong and the others still agree on what it was. This is a convolutional code. The decoder, the Viterbi algorithm, keeps 64 guesses at once (one for each possible pattern of the six bits before the newest: 2⁶ = 64), scores each by how well it explains what arrived, and at every step throws away the worse of each pair. Flip bits and watch:

Each sent bit is an XOR of several data bits, so every data bit is spread over many sent bits; the Viterbi decoder finds the data whose spread best explains what arrived, and a few wrong bits can't outvote the rest.

Why can the Viterbi decoder keep only 64 guesses?
  1. What the encoder sends next depends only on the next data bit and the six before it.
  2. So two guesses that agree on the last six bits behave the same from now on, whatever came earlier.
  3. Of two such guesses, the one that explains what arrived worse so far can never overtake the better one later.
  4. So at every step, for each of the 2⁶ = 64 patterns of the last six bits, only the best guess needs keeping.
  5. At the end, the best of the 64 traces back the most likely data, without ever trying all 2^(number of bits) messages.
Where it breaks

The code spreads each data bit over 14 sent bits, which works against errors spread out, not bunched up. In this chapter's script, 16 wrong bits scattered over a 2000-bit message gave no wrong data bits in 20 tries; the same 16 wrong bits in a row gave 28 wrong data bits over the 20 tries, and even 6 in a row gave 19. Real links shuffle bits before sending (interleaving), so that a burst of noise becomes scattered errors by the time it reaches the decoder.

The code is "rate ½": two coded bits per data bit. LRPT sends 72,000 QPSK symbols a second. How many thousand data bits per second is that?

72,000 symbols × 2 bits = 144,000 coded bits, half of them data: 72 kbit/s. Half the capacity spent on protection, and worth it: without the code the picture would be unusable for most of a pass.

Frames, sync words and checksums

The decoded bits arrive as one endless stream. To cut it into frames, every frame starts with the same 32 bits, the sync word (chapter 21). Finding it marks where each frame begins, and, as chapter 21 showed, which of the four QPSK rotations is the right one.

Estimate first: LRPT carries 72,000 data bits a second (chapter 21: two coded bits per symbol, half of them data). How many milliseconds does a frame of 488 bytes take?

488 × 8 = 3904 bits; 3904 ÷ 72,000 ≈ 0.054 s: about 54 ms, the number this chapter's script prints.

Then the receiver checks the frame. The satellite computes a number from the frame's bytes and sends it at the end: a checksum, here a 16-bit CRC (cyclic redundancy check, a standard recipe for such a number), the big brother of chapter 17's single parity bit. The receiver computes the same number from what it got. If the two disagree, something in the frame is still wrong after Viterbi, and the frame is dropped rather than painted into the picture as garbage. (The real Meteor link uses Reed-Solomon codes here instead, which can also repair a few bad bytes per frame. Ours just drops.)

Viterbi isn't magic: errors bunched close together defeat it, and below about Es/N0 = 4 dB there are simply too many. Then the picture doesn't fade gradually the way an analog picture fills with snow. It goes from perfect to nothing within a decibel or two: the cliff effect of digital radio. You'll see it in the lab below.

Sync words find the frames, checksums throw away the ones still damaged, and the code keeps the picture perfect right up to the cliff.

Pictures in 8×8 blocks

After Viterbi and the frame checksums, our receiver has a clean stream of 72,000 data bits a second. Why isn't the job finished — why not just send raw pixel brightnesses the way chapter 17's SSTV did? Because a weather satellite strip is 1,500 pixels wide across three camera channels, and sent raw, one line of all three is 1,500 × 3 × 8 = 36,000 bits: half a second per line at 72 kbit/s, so the picture would crawl down the screen at two lines a second. So the satellite compresses the image the JPEG way before sending (which is also why a single unfixed bit error would corrupt a whole block of pixels). Each 8×8 block of pixels is rewritten as 64 numbers saying how much of each of 64 standard patterns it contains, from "flat" through "gentle slope" to "fine checkerboard" (a 2-D version of chapter 4's Fourier transform, the DCT). Clouds and coastlines are mostly smooth, so the first few numbers carry almost everything and the rest can be dropped or rounded coarsely:

Smooth pictures are mostly made of the first few of the 64 patterns, so sending only those numbers saves most of the data.

Meteor sends three such pictures at once, from different wavelengths (visible light and infrared: infrared sees clouds at night by their cold), which decoders combine into false-color images. Ours sends one.

In code

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

The rest of the receiver. The Viterbi decoder's heart, for each received pair of soft values a, b (each between −1 and +1: how sure the receiver is that a bit is 0 or 1):

for (let ns = 0; ns < 64; ns++) {                       // each state: the last 6 data bits
  const u = ns >> 5, p0 = (ns & 31) << 1, p1 = p0 | 1;    // the two states it can come from
  const e0 = exp[(u << 6) | p0], e1 = exp[(u << 6) | p1];  // the bit pairs each path would have sent (±1)
  const m0 = m[p0] + a * e0[0] + b * e0[1], m1 = m[p1] + a * e1[0] + b * e1[1];
  if (m1 > m0) { n[ns] = m1; d[ns] = 1; } else n[ns] = m0;  // keep the better path, remember which
}

The whole receiver, symbol by symbol: matched filter → Gardner timing → AGC → (once) the 4th-power FFT → Costas loop (chapter 21) → four rotations into four Viterbi decoders → sync word search → descramble (XOR) → checksum → picture.

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:

convolutional code: 320 of 8000 coded bits flipped (4%), 0 message bits wrong after Viterbi
XOR: 0⊕0 = 0, 0⊕1 = 1, 1⊕0 = 1, 1⊕1 = 0; and x ⊕ k ⊕ k = x for every x and k
keeping  1 of 64 coefficients:   2% of the numbers, PSNR 23.0 dB
keeping  6 of 64 coefficients:   9% of the numbers, PSNR 36.4 dB
keeping 15 of 64 coefficients:  23% of the numbers, PSNR 40.1 dB
keeping 36 of 64 coefficients:  56% of the numbers, PSNR 41.6 dB
frames of 488 bytes, 54 ms each at 72k symbols/s
Es/N0 12 dB, Doppler     0 Hz: measured     0 Hz, rotation 1, 26/30 frames (0 failed the CRC), picture PSNR 42.4 dB
Es/N0  6 dB, Doppler  2500 Hz: measured  2500 Hz, rotation 1, 25/30 frames (0 failed the CRC), picture PSNR 42.5 dB
Es/N0  4 dB, Doppler -2000 Hz: measured -2000 Hz, rotation 1, 24/30 frames (1 failed the CRC), picture PSNR 42.5 dB
Es/N0  2 dB, Doppler  1000 Hz: measured   998 Hz, rotation -1,  0/30 frames (2 failed the CRC), picture PSNR 0.0 dB
 6 wrong coded bits, 20 tries: scattered → 0 wrong data bits in all, in a row → 19
16 wrong coded bits, 20 tries: scattered → 0 wrong data bits in all, in a row → 28
all checks passed

The first 4 frames of each run go to measuring the frequency and finding the rotation. At 4 dB nearly everything still comes through; at 2 dB the frequency is still found (998 Hz for 1000), but the code gives up: the cliff.

Lab: a pass, decoded live

A whole pass, sped up: the satellite rises, the signal strengthens as it comes closer, the Doppler slides through zero, and it sets. Every frame is a strip of picture, encoded, modulated, Doppler-shifted, buried in noise, and decoded by the receiver of chapters 21 and 22, as it arrives.

For real passes: a V-dipole (two 53 cm rods at 120°, horizontal) outdoors, a dongle at 137.9 MHz recording 288 kS/s or more, and SatDump, which implements the full LRPT stack including the parts simplified here. Pass times for your location: n2yo.com (search "METEOR-M").

Check yourself

What is 1 ⊕ 1 ⊕ 1?

1: 1 ⊕ 1 = 0, then 0 ⊕ 1 = 1. (An odd number of 1s gives 1, which is exactly what a parity bit counts.)

The code sends two bits per data bit. Why is that worth halving the data rate?

Every data bit is spread over 14 sent bits, so the decoder can still find it when a few percent of the bits are wrong; without the code, the picture would be unusable for most of a pass.

What does a sync word do besides marking frames?

It resolves the 90° ambiguity of the Costas loop (chapter 21): only the right rotation decodes to the known pattern.

Why do digital pictures fail suddenly instead of getting gradually snowy?

The error-correcting code fixes everything up to a point, so the picture stays perfect; past it, errors overwhelm the code and whole frames are lost.

Last updated