The air, with everyone talking.

Everyone who uses radio shares the same air, the way everyone at a party shares the same room. This chapter puts several transmitters on the air at once and follows a single receiver as it tries to read them all.

In the last chapter the receiver searched for one known pattern, and finding it was the whole job. A real message has to carry something new, such as a temperature reading or a line of text, and that part is unknown to the receiver in advance. Matching against a copy can therefore find only the part of a message that was agreed on beforehand.

The standard solution is to open every message with a short known pattern, called a preamble, and to put the new information after it. A band does the same thing when the drummer counts in: one, two, three, four. The count carries no music, and its whole job is to tell everyone exactly where the first beat falls. A receiver searches for the preamble with the matched filter from the last chapter. When the spike appears, it knows that a message has begun, and it knows precisely where the first piece of information starts, which is immediately after the preamble ends.

The information itself is sent as bits. A bit is a single yes or no, written as 1 or 0. Here a 1 is sent as an up step and a 0 as a down step, the same two kinds of step the pattern is made of. A handful of bits goes a long way, in the same manner as the game of twenty questions, where every yes or no cuts the remaining choices in half. Eight bits are enough to pick one thing out of 256, which covers every letter, digit and punctuation mark on a keyboard. The capital letter A, for example, is 01000001 in the code that nearly every computer uses.

Below, every transmitter sends the same thirteen-step preamble followed by sixteen bits, which spell two letters. Add transmitters, type letters of your own, drag the messages along the air, and raise the noise. The receiver further down the page lists every preamble it finds and the letters it reads after each one. Then drag two messages until they overlap, and see what the receiver makes of that.

the airdrag a message to move it. the colored bars show where each one sits
lettersstrengthstarts atbits sent

Each message is 116 samples long: the preamble of 13 steps, then 16 bits, with every step and every bit lasting 4 samples. Two messages that overlap are simply added together, sample by sample, which is what happens when two real transmitters send at the same moment.

The receiver

the receiver's search for the preamble
found atsent bybits readletterswrong bits

This is what a preamble is for. The receiver shares no clock with any of the senders and has no idea when a message will begin. The preamble is a known pattern that the matched filter can find in the noise, and the moment the spike appears, the receiver knows where the first bit starts. It starts 26 samples after the top of the spike, which is half the length of the preamble.

From there it reads the bits one at a time. For each bit it adds up the four samples and looks at whether the total came out above zero or below. Adding the four samples is the matched filter again in miniature. The noise in the four samples partly cancels, while the bit itself adds up.

While the receiver is reading one message, its search for the next one is on hold. That is why a message that begins in the middle of another one is lost, because the receiver was busy when its preamble went by. Where two messages overlap, their samples add together and some of the bits come out wrong, and the table marks those in red. The next chapter is about a way around that problem.

A real receiver gets more from a preamble than timing. It also uses it to measure how strong the signal is, and how far the sender's frequency is from its own, which is the slip of the last chapter. But timing is the heart of it. A preamble turns "something is out there" into "the information starts exactly here."

from the spike to the letters

/* After the matched filter finds a preamble at peak, the data starts
   PRE_LEN/2 samples later. Read each bit by adding up its samples and
   looking at whether the total is above or below zero. */
#define PRE_LEN   52
#define CHIP      4
#define DATA_BITS 16

void read_bits(const float *x, int n, int peak, unsigned char *bits)
{
    int start = peak + PRE_LEN / 2;            /* first sample of the first data bit */
    for (int b = 0; b < DATA_BITS; b++) {
        float sum = 0.0f;
        for (int k = 0; k < CHIP; k++) {
            int i = start + b * CHIP + k;
            if (i < n) sum += x[i];
        }
        bits[b] = sum >= 0.0f ? 1 : 0;         /* plus was a 1, minus was a 0 */
    }
}

/* The receiver's whole life, as a loop:
     1. run the matched filter over new samples
     2. if a spike rises above the alarm level: read_bits, pass them on, skip past the message
     3. otherwise keep sliding                                                           */
const PRE_LEN: usize = 52;
const CHIP: usize = 4;
const DATA_BITS: usize = 16;

/// The data starts PRE_LEN/2 samples after the spike. Add up each bit, keep the sign.
fn read_bits(x: &[f32], peak: usize) -> [u8; DATA_BITS] {
    let start = peak + PRE_LEN / 2;
    let mut bits = [0u8; DATA_BITS];
    for (b, bit) in bits.iter_mut().enumerate() {
        let from = start + b * CHIP;
        let sum: f32 = x.get(from..from + CHIP).map_or(0.0, |c| c.iter().sum());
        *bit = (sum >= 0.0) as u8;
    }
    bits
}
import numpy as np

PRE_LEN, CHIP, DATA_BITS = 52, 4, 16

def read_bits(x, peak):
    """The data starts PRE_LEN/2 samples after the spike. Add up each bit, keep the sign."""
    start = peak + PRE_LEN // 2
    chips = x[start:start + DATA_BITS * CHIP].reshape(DATA_BITS, CHIP)
    return (chips.sum(axis=1) >= 0).astype(int)

def to_text(bits):
    return "".join(chr(int("".join(map(str, bits[i:i+8])), 2)) for i in range(0, len(bits), 8))