---
title: What a modem does
heading: The job of a modem, turning bits into a signal a channel can carry and back again, the stages of a modern digital radio from framing to decoding, the audio-frequency FSK modem this course builds, and the first step of all: turning text into bits
eyebrow: D12 · Lesson 1 · What a modem does
xp: 40
---

::: hook
A **modem** (modulator–demodulator) is the bridge between bits and a physical channel. Dial-up modems made that bridge audible: the screech of a connecting modem was two machines negotiating a scheme to push bits through a telephone line built for voice. The same stages are inside every Wi-Fi chip and phone: frame the data, map bits to symbols, shape them into a signal, survive noise and echoes and frequency errors, and undo all of it at the other end. This course builds one, stage by stage, in Python, ending with a modem that sends text through a simulated speaker-to-microphone channel.
:::

::: play
- The sound of an audio frequency-shift-keyed (FSK) modem sending alternating bits at 1200 baud: one tone (space, default 2200 Hz) for a 0 and another (mark, 1200 Hz) for a 1, switching without phase jumps.
- The spectrum shows the energy clustered around the two tones. Listen to it.
- This is the modulation used by caller-ID, early telephone modems (Bell 202) and amateur packet radio, and it's where this course starts.
:::

```sim plot
view: spectrum
x: t = 0..20m s
y: v = -1.2..1.2 V
samples: 2048
freq: f = 0..4k Hz
slider: m = 800..1800 = 1200 Hz
slider: s = 1600..3000 = 2200 Hz
plot: cos(2*pi*(s*t + (m - s)*(floor(1200*t/2) + (1200*t - 2*floor(1200*t/2) - 1 + abs(1200*t - 2*floor(1200*t/2) - 1))/2)/1200)) | label: FSK signal, alternating bits
sound: cos(2*pi*(s*t + (m - s)*(floor(1200*t/2) + (1200*t - 2*floor(1200*t/2) - 1 + abs(1200*t - 2*floor(1200*t/2) - 1))/2)/1200)) | scale: 1
```

::: predict
## How long does a 1200 baud FSK modem take to send "Hello, world", eight bits per character with no framing?

- [ ] 10 ms
- [x] 80 ms
- [ ] 0.8 s
- [ ] 12 ms

::: reveal
12 characters × 8 bits = 96 bits, and 96/1200 = 0.08 s. Real links add framing (lesson 2), error checking (lesson 12) and start-up preambles, so the airtime grows, but the bit rate sets the pace.
:::
:::

::: nameit
## The stages

1. **Source**: text or data as bits (this lesson).
2. **Framing**: a preamble to wake the receiver, a sync word to mark where data begins, a length, the payload and a check (lesson 2, lesson 12).
3. **Modulation**: bits become symbols and symbols become a waveform: FSK (lesson 3), then BPSK, QPSK and QAM (lessons 7 and 9), then OFDM (lesson 13), with pulse shaping to limit bandwidth (lesson 6).
4. **Channel**: noise, echoes and frequency offset (lesson 10).
5. **Demodulation**: filter or correlate (lesson 4), recover the bit clock (lesson 5) and the carrier (lesson 8), equalize echoes (lesson 11), decide symbols.
6. **Decoding**: find the frame, check and correct errors, deliver the bits.

## This course's modem

Audio frequencies, so you can hear it: 9600 samples per second, 1200 baud (8 samples per bit), mark at 1200 Hz and space at 2200 Hz. Text becomes bits as 8-bit ASCII, most significant bit first. The time to send N bits at a symbol rate $R$ with b bits per symbol is $N/(bR)$.
:::

::: example
## What bits represent "Hi"? How long do 1000 characters take at 1200 baud, and at 9600 bit/s?

::: solution
1. **Bits for 'H'.** Its ASCII code is 72:
   $$72 = 01001000$$
2. **Bits for 'i'.** Its ASCII code is 105:
   $$105 = 01101001$$
3. **Count the bits.** Each character is 8 bits:
   $$1000 \times 8 = 8000\ \text{bits}$$
4. **Time at 1200 bit/s.**
   $$\begin{aligned} t &= \frac{8000}{1200}\ \text{s} \\ &= 6.67\ \text{s} \end{aligned}$$
5. **Time at 9600 bit/s.**
   $$\begin{aligned} t &= \frac{8000}{9600}\ \text{s} \\ &= 0.83\ \text{s} \end{aligned}$$

**Answer:** "Hi" is 01001000 01101001, and 1000 characters take 6.67 s at 1200 baud and 0.83 s at 9600 bit/s.
:::
:::

::: example
## The 1000 characters are sent with a start and a stop bit around each byte at 1200 baud. How long do they take now?

::: solution
1. **Bits on the wire.** With a start and a stop bit, each character is 10 bits on the wire:
   $$1000 \times 10 = 10\,000\ \text{bits}$$
2. **Time at 1200 baud.**
   $$\begin{aligned} t &= \frac{10\,000}{1200}\ \text{s} \\ &= 8.33\ \text{s} \end{aligned}$$
   That's a quarter longer than the bare 8000 bits.
3. **What the framing buys.** Framing always costs something; the question is what it buys, and here it buys resynchronization on every byte.

**Answer:** 8.33 s, a quarter longer than the bare 8000 bits would take.
:::
:::

::: example
## A telephone channel passes 3 kHz with a 30 dB SNR. What is the most any modem could send through it?

::: solution
1. **Shannon's limit.** A 30 dB SNR is a power ratio of 1000:
   $$\begin{aligned} C &= 3000\log_2(1 + 1000) \\ &= 29.9\ \text{kb/s} \end{aligned}$$
2. **Compare with real modems.** Real analog modems reached 33.6 kb/s by using a little more bandwidth and better SNR than this textbook figure, and stopped there.

**Answer:** about 29.9 kb/s.
:::
:::

::: check
kind: python

## Turn text into bits and back, and time the transmission.

```python starter
def text_to_bits(text):
    # Each character becomes its 8-bit ASCII code, most significant bit first.
    # Return a flat list of 0s and 1s.
    return []

def bits_to_text(bits):
    # The reverse: group the bits in eights, most significant first, and join the characters.
    # Ignore any leftover bits that don't fill a byte.
    return ""

def airtime(nbits, baud):
    # Seconds to send nbits at baud bits per second.
    return 0.0
```

```python solution
def text_to_bits(text):
    out = []
    for ch in text:
        v = ord(ch)
        out.extend((v >> (7 - i)) & 1 for i in range(8))
    return out

def bits_to_text(bits):
    chars = []
    for i in range(0, len(bits) - 7, 8):
        v = 0
        for b in bits[i:i + 8]:
            v = 2*v + b
        chars.append(chr(v))
    return "".join(chars)

def airtime(nbits, baud):
    return nbits/baud
```

```python tests
assert text_to_bits("A") == [0, 1, 0, 0, 0, 0, 0, 1], f"'A' is 65 = 01000001; got {text_to_bits('A')}."
assert len(text_to_bits("Hello")) == 40, "Five characters make 40 bits."
assert bits_to_text(text_to_bits("Ohmwork modem!")) == "Ohmwork modem!", "Turning text into bits and back should give the same text."
assert bits_to_text(text_to_bits("OK") + [1, 0, 1]) == "OK", "Leftover bits that don't fill a byte are ignored."
assert abs(airtime(len(text_to_bits("Hello, world")), 1200) - 0.08) < 1e-12, "12 characters, 96 bits, take 80 ms at 1200 baud."
```

::: right
Right. Your bits come back as the same text, and 96 bits take 80 ms at 1200 baud.
:::
:::

::: check
given: n = 12 | 100 | 1000; b = 300 | 1200 | 9600
answer: 8*n/b
unit: s
tolerance: 0.5%

## How long does it take to send {n} 8-bit characters at {b} bits per second, with no framing?

::: right
Right. 8 × {n}/{b} = {answer} s.
:::

::: misses
- [n/b] Each character is 8 bits.
- [any] Not yet. Bits over the bit rate.
:::
:::

::: check
given: c = 65 | 72 | 105 | 122
answer: c
base: 2
tolerance: 0

## What 8-bit pattern (most significant bit first) encodes the character whose ASCII code is {c}?

::: right
Right. {c} is {c:bin8} in binary.
:::

::: misses
- [any] Not yet. Write {c} in binary with 8 digits.
:::
:::

::: check
kind: parts
given: n = 100 | 1000 | 5000; b = 300 | 1200 | 9600

## {n} characters are sent at {b} baud with a start and stop bit around each byte.

::: part
answer: n*10/b
unit: s
tolerance: 0.5%

### How long do they take?

::: misses
- [n*8/b] Ten bits per character on the wire.
- [any] $10n/b$.
:::
:::

::: part
answer: 80
unit: %
tolerance: 0.5%

### What fraction of the bits carry data?

::: misses
- [any] 8 of 10.
:::
:::
:::

::: check
kind: parts
given: B = 3 | 4 | 20; S = 20 | 30 | 40

## A channel passes {B} kHz with {S} dB SNR.

::: part
answer: B*log(1 + 10^(S/10))/log(2)
unit: kb/s
tolerance: 0.5%

### What is its Shannon capacity?

::: misses
- [any] $B\log_2(1 + \text{SNR})$.
:::
:::

::: part
answer: log(1 + 10^(S/10))/log(2)
unit: b/s/Hz
tolerance: 0.5%

### How many bits per second per hertz?

::: misses
- [any] $\log_2(1 + \text{SNR})$.
:::
:::
:::

::: check
kind: parts
given: s = 1200 | 2400 | 9600; M = 4 | 16 | 64

## A modem sends {s} symbols per second, each one of {M} possible symbols.

::: part
answer: s*log(M)/log(2)
unit: b/s
tolerance: 0.5%

### What is its bit rate?

::: misses
- [s*M] Each symbol carries $\log_2M$ bits, not M.
- [s] That's the symbol rate. Multiply by the bits per symbol.
- [any] Not yet. $R = s\log_2M$.
:::
:::

::: part
answer: log(M)/log(2)
tolerance: 0.5%

### How many bits does each symbol carry?

::: misses
- [M] {M} symbols encode $\log_2$ {M} bits.
- [any] Not yet. $\log_2M$.
:::
:::

::: right
Right. More symbols per alphabet raise the bit rate without more bandwidth, at the cost of noise margin.
:::
:::

::: explain
## Why does a modem need to turn bits into a waveform at all, instead of sending the bits directly as voltages?

::: model
A channel such as a telephone line, a radio band or the air between a speaker and a microphone only passes a certain range of frequencies, and may block DC and very low frequencies entirely. Raw bits sent as voltage levels would have much of their energy at low frequencies and in sharp edges that the channel can't carry, so they'd arrive distorted or not at all. A modem maps the bits onto signals designed to fit the channel, such as tones within the audio band or a carrier in an assigned radio band, so the information survives the trip, and the receiver then reverses the mapping to recover the bits.
:::
:::

::: practice

::: check
given: n = 1 | 10000; b = 50 | 100000
answer: 8*n/b
unit: s
tolerance: 0.5%

## Airtime: {n} characters at {b} bit/s?

::: misses
- [any] 8n/b.
:::
:::

::: check
given: c = 33 | 48 | 57 | 64 | 90 | 97 | 110 | 126
answer: c
base: 2
tolerance: 0

## ASCII {c} as 8 bits?

::: misses
- [any] Write it in binary.
:::
:::
:::
