---
title: An FSK transmitter
heading: Sending bits as two tones, frequency-shift keying with mark and space frequencies, why the phase must carry on smoothly from one bit to the next, the spectrum of continuous-phase FSK, and writing a modulator that turns any bit stream into samples you can play
eyebrow: D12 · Lesson 3 · An FSK transmitter
xp: 40
---

::: hook
The simplest way to send bits as sound is to whistle two notes: a low one for 1 and a high one for 0. That's **frequency-shift keying**, and with tones at 1200 and 2200 Hz it's the Bell 202 standard that carried caller ID and early computer links over phone lines. The subtle part is the switch between notes. Jump to the new tone's own phase and the waveform glitches, splattering energy across the spectrum; carry the phase on smoothly and the signal stays compact. A modulator that does it right is a few lines of code that keep a running phase.
:::

::: play
- One byte, B, sent as continuous-phase FSK at 1200 baud: 1 bits at 1200 Hz (one cycle per bit), 0 bits at 2200 Hz.
- The waveform never jumps: each bit starts where the last one left off. Change the byte and watch the tones rearrange.
- Listen to the byte repeating.
:::

```sim plot
view: graph
x: t = 0..6.6667
y: v = -1.2..1.2
slider: B = 0..255 step 1 = 72
plot: cos(2*pi*(2.2*t + (1.2 - 2.2)*((floor(B/2^(7)) - 2*floor(B/2^(8)))*step(floor((1.2*t)) - 0 - 0.5) + (floor(B/2^(6)) - 2*floor(B/2^(7)))*step(floor((1.2*t)) - 1 - 0.5) + (floor(B/2^(5)) - 2*floor(B/2^(6)))*step(floor((1.2*t)) - 2 - 0.5) + (floor(B/2^(4)) - 2*floor(B/2^(5)))*step(floor((1.2*t)) - 3 - 0.5) + (floor(B/2^(3)) - 2*floor(B/2^(4)))*step(floor((1.2*t)) - 4 - 0.5) + (floor(B/2^(2)) - 2*floor(B/2^(3)))*step(floor((1.2*t)) - 5 - 0.5) + (floor(B/2^(1)) - 2*floor(B/2^(2)))*step(floor((1.2*t)) - 6 - 0.5) + (floor(B/2^(0)) - 2*floor(B/2^(1)))*step(floor((1.2*t)) - 7 - 0.5) + (floor(B/2^(7 - floor((1.2*t)))) - 2*floor(B/2^(8 - floor((1.2*t)))))*((1.2*t) - floor((1.2*t))))/1.2)) | samples: 1200 | label: FSK signal (t in ms)
readout: byte = B | format: bin8
sound: cos(2*pi*(2.2*(1000*(t - floor(150*t)/150)) + (1.2 - 2.2)*((floor(B/2^(7)) - 2*floor(B/2^(8)))*step(floor((1.2*(1000*(t - floor(150*t)/150)))) - 0 - 0.5) + (floor(B/2^(6)) - 2*floor(B/2^(7)))*step(floor((1.2*(1000*(t - floor(150*t)/150)))) - 1 - 0.5) + (floor(B/2^(5)) - 2*floor(B/2^(6)))*step(floor((1.2*(1000*(t - floor(150*t)/150)))) - 2 - 0.5) + (floor(B/2^(4)) - 2*floor(B/2^(5)))*step(floor((1.2*(1000*(t - floor(150*t)/150)))) - 3 - 0.5) + (floor(B/2^(3)) - 2*floor(B/2^(4)))*step(floor((1.2*(1000*(t - floor(150*t)/150)))) - 4 - 0.5) + (floor(B/2^(2)) - 2*floor(B/2^(3)))*step(floor((1.2*(1000*(t - floor(150*t)/150)))) - 5 - 0.5) + (floor(B/2^(1)) - 2*floor(B/2^(2)))*step(floor((1.2*(1000*(t - floor(150*t)/150)))) - 6 - 0.5) + (floor(B/2^(0)) - 2*floor(B/2^(1)))*step(floor((1.2*(1000*(t - floor(150*t)/150)))) - 7 - 0.5) + (floor(B/2^(7 - floor((1.2*(1000*(t - floor(150*t)/150)))))) - 2*floor(B/2^(8 - floor((1.2*(1000*(t - floor(150*t)/150)))))))*((1.2*(1000*(t - floor(150*t)/150))) - floor((1.2*(1000*(t - floor(150*t)/150))))))/1.2)) | scale: 1
```

::: predict
## At 1200 baud and 9600 samples per second, how many samples does each bit get, and how many cycles of each tone fit in a bit?

- [ ] 1 sample; 1 and 1.83 cycles
- [x] 8 samples; 1 cycle of mark and 1.83 cycles of space
- [ ] 8 samples; 1 cycle of each
- [ ] 1200 samples; 1200 and 2200 cycles

::: reveal
9600/1200 = 8 samples per bit. A bit lasts 1/1200 s, during which 1200 Hz completes exactly 1 cycle and 2200 Hz completes 2200/1200 = 1.83 cycles. Because the space tone ends a bit partway through a cycle, the next bit must start from that phase to avoid a jump.
:::
:::

::: nameit
## Frequency-shift keying

Each bit selects one of two frequencies, **mark** $f_1$ for 1 and **space** $f_0$ for 0, for one bit time T = 1/baud. With the modulator keeping a running phase φ,

$$x[n] = \cos\varphi[n], \qquad \varphi[n + 1] = \varphi[n] + 2\pi f_{b(n)}/f_s$$

the waveform is **continuous-phase FSK**: no jumps, so its spectrum stays concentrated around the two tones.

## Spacing and bandwidth

The frequency deviation is half the tone spacing, here (2200 − 1200)/2 = 500 Hz, and the **modulation index** is $h = |f_1 - f_0|/R$ = 1000/1200 = 0.83. Carson's rule (D4) puts the bandwidth near $|f_1 - f_0| + 2R$. When $h$ = 1/2 the scheme is **MSK**, the narrowest FSK whose tones can still be told apart coherently.

## Samples per bit

At $f_s$ samples per second and R baud, each bit gets $f_s/R$ samples, which must be a whole number for this simple modulator: 9600/1200 = 8.
:::

::: example
## What's the modulation index and approximate bandwidth of 1200 baud FSK with tones at 1200 and 2200 Hz?

::: solution
1. **Modulation index.** The tones are 1000 Hz apart:
   $$\begin{aligned} h &= \frac{1000}{1200} \\ &= 0.833 \end{aligned}$$
2. **Carson's rule.** The tone spacing plus twice the baud rate:
   $$\begin{aligned} B &\approx 1000 + 2 \times 1200 \\ &= 3400\ \text{Hz} \end{aligned}$$
3. **Fit the telephone band.** That's why Bell 202 fits a telephone line's 300 to 3400 Hz band, just.

**Answer:** h = 0.833, and the bandwidth is about 3400 Hz.
:::
:::

::: example
## At 1200 baud, how many cycles of each Bell 202 tone fit in one bit, and why must the phase be continuous?

::: solution
1. **The mark tone.** 1200 Hz fits exactly one cycle per bit:
   $$\frac{1200}{1200} = 1\ \text{cycle}$$
2. **The space tone.** 2200 Hz fits
   $$\frac{2200}{1200} = 1.833\ \text{cycles}$$
   so it ends each bit at a different phase.
3. **Why the phase must carry on.** Restarting the tone at zero phase each bit would make a jump, which spreads energy across the band; a phase accumulator just keeps going.

**Answer:** one cycle of the mark tone and 1.833 cycles of the space tone fit in a bit, so the phase has to run on from bit to bit to avoid jumps that spread energy across the band.
:::
:::

::: example
## At a 9600 Hz sample rate, how many samples are there per bit, and per cycle of the 2200 Hz tone?

::: solution
1. **Samples per bit.**
   $$\frac{9600}{1200} = 8$$
2. **Samples per cycle of the space tone.**
   $$\frac{9600}{2200} = 4.36$$
3. **Compare with the sampling limit.** That's comfortably above the 2 per cycle that sampling requires, so the tone is well represented.

**Answer:** 8 samples per bit and 4.36 per cycle of the 2200 Hz tone.
:::
:::

::: check
kind: python

## Write a continuous-phase FSK modulator.

```python starter
import numpy as np

FS, BAUD, F_MARK, F_SPACE = 9600, 1200, 1200.0, 2200.0

def fsk_modulate(bits):
    # Continuous-phase FSK: 8 samples per bit. Sample n is cos(phase), and after each sample the
    # phase advances by 2*pi*f/FS, with f = F_MARK for a 1 bit and F_SPACE for a 0. The phase
    # starts at 0 and carries on smoothly from bit to bit. Return a numpy array.
    return np.zeros(len(bits)*FS//BAUD)
```

```python solution
import numpy as np

FS, BAUD, F_MARK, F_SPACE = 9600, 1200, 1200.0, 2200.0

def fsk_modulate(bits):
    sps = FS//BAUD
    freqs = np.repeat([F_MARK if b else F_SPACE for b in bits], sps)
    steps = 2*np.pi*freqs/FS
    phase = np.concatenate(([0.0], np.cumsum(steps)[:-1]))
    return np.cos(phase)
```

```python tests
_x = fsk_modulate([1, 0, 1, 1, 0])
assert len(_x) == 40, f"Five bits at 8 samples each make 40 samples; got {len(_x)}."
assert abs(_x[0] - 1) < 1e-12 and abs(_x[8] - 1) < 1e-12, "The phase starts at 0, and a full 1200 Hz bit (8 samples) returns it to a whole cycle."
assert abs(_x[9] - np.cos(2*np.pi*(8*1200 + 2200)/9600)) < 1e-9, "The phase must carry on from the last bit: sample 9 is one 2200 Hz step past a whole cycle."
_ones = fsk_modulate([1]*120)
_k = np.argmax(np.abs(np.fft.rfft(_ones)))
assert abs(_k*9600/len(_ones) - 1200) < 1e-9, f"All ones should be a pure 1200 Hz tone; its peak is at {_k*9600/len(_ones):.0f} Hz."
_zeros = fsk_modulate([0]*120)
assert abs(np.argmax(np.abs(np.fft.rfft(_zeros)))*9600/len(_zeros) - 2200) < 1e-9, "All zeros should be 2200 Hz."
_r = fsk_modulate(list(np.random.default_rng(3).integers(0, 2, 400)))
assert abs(np.mean(_r**2) - 0.5) < 0.02, "A constant-amplitude tone has an average power of 1/2."
```

::: right
Right. Your modulator keeps its phase continuous, puts all-ones at exactly 1200 Hz and all-zeros at 2200 Hz.
:::
:::

::: check
given: s = 8000 | 9600 | 48000; r = 300 | 1200 | 2400
answer: s/r
tolerance: 0.5%

## A modem samples at {s} Hz and sends {r} baud. How many samples does each bit get?

::: right
Right. $f_s/R$ = {answer} samples.
:::

::: misses
- [r/s] Samples per bit is the sample rate over the bit rate.
- [any] Not yet. Divide the rates.
:::
:::

::: check
given: a = 1200 | 1070 | 2025; b = 2200 | 1270 | 2225; r = 300 | 1200
answer: abs(b - a)/r
tolerance: 0.5%

## FSK tones at {a} and {b} Hz carry {r} baud. What's the modulation index?

::: right
Right. $|f_1 - f_0|/R$ = {answer}.
:::

::: misses
- [abs(b - a)/(2*r)] The index uses the full tone spacing over the bit rate.
- [any] Not yet. Spacing over baud.
:::
:::

::: check
given: f = 1200 | 2200 | 1700; r = 1200 | 300
answer: f/r
tolerance: 0.5%

## How many cycles of a {f} Hz tone fit into one bit at {r} baud?

::: right
Right. f/R = {answer} cycles.
:::

::: misses
- [any] Not yet. Tone frequency times bit time.
:::
:::

::: check
kind: parts
given: m = 1200 | 1300; s = 2100 | 2200; R = 300 | 1200

## FSK uses tones at {m} and {s} Hz at {R} baud.

::: part
answer: (s - m)/R
tolerance: 0.5%

### What is the modulation index?

::: misses
- [any] Spacing ÷ baud.
:::
:::

::: part
answer: s - m + 2*R
unit: Hz
tolerance: 0.5%

### About what bandwidth does Carson's rule give?

::: misses
- [any] Spacing + 2 × baud.
:::
:::
:::

::: check
kind: parts
given: f = 1200 | 2200 | 1800; R = 600 | 1200; fs = 8000 | 9600 | 48000

## A tone of {f} Hz is sent at {R} baud and sampled at {fs} Hz.

::: part
answer: f/R
tolerance: 0.5%

### How many cycles fit in one bit?

::: misses
- [any] Frequency ÷ baud.
:::
:::

::: part
answer: fs/R
tolerance: 0.5%

### How many samples per bit?

::: misses
- [any] Sample rate ÷ baud.
:::
:::
:::

::: explain
## Why should an FSK modulator keep its phase continuous from one bit to the next?

::: model
If the modulator switched abruptly to a separate oscillator at the new frequency, the waveform would jump from wherever the old tone was in its cycle to wherever the new one happened to be, and a sudden jump contains a broad spread of frequencies. That splatter widens the signal's spectrum, interferes with neighboring channels and makes the receiver's filters work harder. Keeping a single running phase and only changing how fast it advances means each bit starts exactly where the last one ended, so the waveform stays smooth and its energy stays near the two tones.
:::
:::

::: practice

::: check
given: s = 4000 | 48000 | 96000; r = 50 | 300 | 1200 | 2400 | 4800
answer: s/r
tolerance: 0.5%

## Samples per bit: {s} Hz, {r} baud?

::: misses
- [any] $f_s/R$.
:::
:::

::: check
given: a = 500 | 1500; b = 1600 | 3000; r = 100 | 2400
answer: abs(b - a)/r
tolerance: 0.5%

## Modulation index: {a} and {b} Hz at {r} baud?

::: misses
- [any] $|f_1 - f_0|/R$.
:::
:::
:::
