Radio from scratch · Chapter 2

Arrows as numbers

Before you startThis chapter uses:

In chapter 1, the game showed that one shadow isn't enough: to know which way an arrow spins, you need both its sideways shadow and its up-down shadow. So whenever a radio measures a signal, it writes down two numbers at a time.

This chapter is about what you can do with those pairs of numbers. Adding them turns out to be easy. Multiplying them turns out to be surprising, and that surprise is the engine of every software radio.

Here's a taste. Below is one arrow, called z. The dots show what you get by multiplying z by itself again and again: z, z×z, z×z×z, and so on. Drag the white ring to change z:

Multiplying makes the dots turn. Shorten z a little and they spiral inward; lengthen it and they spiral outward. Normal numbers don't do this: multiply 2 by itself and you just march off to the right, 2, 4, 8, 16. Why would multiplying arrows produce rotation? By the end of this chapter, it'll feel obvious.

An arrow is just two numbers

Put an arrow's tail at the center of a grid. To tell someone where its tip is, you need two numbers: how far it goes sideways and how far it goes up. Those are exactly chapter 1's two shadows: the green sideways shadow and the yellow up shadow.

Radio engineers call these two numbers I (sideways) and Q (up). When the radio dongle (the USB-stick radio receiver this course uses, an "RTL-SDR", about $30) sends data to your computer, that's literally what comes down the USB cable: I, Q, I, Q, I, Q, millions of times per second. Each pair is one arrow.

Notice there's a second way to describe the same arrow: by its length and its angle. Drag the arrow and watch both descriptions change together. They're two languages for one thing. Sideways-and-up is the language for adding; length-and-angle, as you're about to see, is the language for multiplying.

Adding arrows: tip to tail

You've already added arrows: in chapter 1, we attached a second arrow to the tip of the first. That's all adding is. Take arrow b, slide it (without turning it) so its tail sits on the tip of a, and the sum is the yellow arrow from the center to the new tip.

In numbers it's as simple as it looks: the sideways parts add up and the up parts add up, separately. Nothing surprising yet.

Multiplying arrows: turn and stretch

Adding arrows happens automatically in the air when stations meet on your antenna. Multiplying two arrows is what our receiver code does to almost every pair of numbers from the dongle. Why would you ever multiply two arrows in a radio? As you're about to see, multiplying by an arrow turns things, and that one operation does three jobs that build almost the whole receiver:

What should it mean to "multiply" two arrows? Let's think about what multiplying ordinary numbers does. Multiplying by 2 stretches: everything gets twice as big. Multiplying by −1 flips: 3 becomes −3, pointing the other way. A flip is a half turn.

So multiplication of ordinary numbers is already "stretch and turn", just with very limited turning: no turn, or a half turn. Arrows can point in any direction, so the natural extension is: multiplying by an arrow stretches by its length and turns by its angle. In other words:

Drag both arrows and check it:

The small arcs near the center show the angles stacking up: the product's angle is the blue angle plus the pink one.

Pause and ponderWhich arrow should you multiply by to turn something a quarter turn (90°) counter-clockwise, without stretching it? And what happens if you multiply by that arrow twice?

The answer to the first question is the arrow of length 1 pointing straight up. It's so useful it gets a one-letter name. Mathematicians call it i; radio engineers call it j (because "i" was already taken for electric current), and so will we.

Now the second question. Multiplying by j means "turn a quarter". Doing it twice means "turn a half", and turning a half means flipping: pointing the other way. So j × j = −1.

That's the famous "square root of minus one", and you've just seen there's nothing imaginary about it. In school it's often presented as a strange rule to memorize. It's simply what a quarter turn does when you do it twice.

A game: land on the target

Chapters 4, 5 and 9 use "lengths multiply, angles add" so often that it needs to be automatic, not something you work out. This game is practice until it is. The blue arrow is fixed. Find the pink multiplier that sends the yellow product into the white ring. Think in turns and stretches: how far round does blue need to go, and how much longer or shorter does it need to get?

After a few rounds you'll notice that you stop fiddling and start aiming: point the multiplier at the angle you need to turn by, and make it as long as the stretch you need. That instinct is the whole of multiplying arrows.

An arrow is two numbers, I and Q. Adding arrows adds their parts; multiplying arrows multiplies their lengths and adds their angles, so multiplying by an arrow of length 1 just turns.

Why do lengths multiply and angles add?
  1. Write the multiplier as z = I + j·Q, meaning "I sideways, plus Q up" (Writing it down, below, explains this way of writing it). Multiplying an arrow a by it means a·I + a·(j·Q), and multiplication spreads over the sum as with ordinary numbers.
  2. a·I is a stretched by I, in a's own direction.
  3. a·j·Q is a turned a quarter turn (that's what j does), stretched by Q.
  4. So the product is "I steps along a, then Q steps at right angles to a": the same recipe as drawing z itself, but on a grid tilted to a's direction and scaled by a's length.
  5. On that tilted grid, the recipe (I, Q) lands at z's angle measured from a's direction, at z's length times a's length.
  6. So the product's angle is a's angle plus z's, and its length is a's length times z's.
Where it breaks

On paper an arrow of length exactly 1 stays length 1 forever, however often you multiply by it. In a computer every multiplication is rounded a tiny bit, and the errors add up: chapter 5's script measures the length drifting off by 5.6 millionths after 10 million multiplications with 32-bit numbers (the compact kind signal code often uses, good to about 7 digits). That's why real code keeps an angle and computes each arrow fresh from it, instead of multiplying over and over.

Multiply again and again, and it spins

Job 1 from the list above: the receiver needs an arrow of its own, spinning at a speed we choose (chapter 5 uses it to slide the station you want to 0 Hz). Multiplication makes one, and it's the opening spiral again. Start with the arrow pointing at 1. Multiply it by a small "step" arrow. Then multiply the result by the same step again. And again. Each multiplication turns by the step's angle and stretches by the step's length:

Predict: an arrow of length 1 at 30°, multiplied by itself: z × z × z. At what angle does the result point?

Angles add: 30° + 30° + 30° = 90°, straight up. Lengths multiply: 1 × 1 × 1 = 1.

Look at the dots on the right too: the up part (Q) of each step, one after another. It's chapter 1's sine wave again, but drawn as separate dots instead of a smooth line, because a computer only ever sees the arrow at separate moments. Those moments are called samples, and chapter 3 is about them.

A name for the arrow at angle θ: ejθ

This section adds no new tool: the receiver's code already makes every arrow it needs from cos and sin. It's about a name. From chapter 4 on, formulas have to say "the arrow of length 1 at angle θ" again and again, and every radio book, datasheet and formula writes it the same way: ejθ. If that looks like a power you'd never connect with a circle, this section is why it's written that way, so those formulas read as pictures you already know.

Why would anyone write an angle as an exponent? Because of what this chapter found: multiplying arrows of length 1 adds their angles. Powers do exactly the same with their exponents: 23 × 22 = 25, because 3 + 2 = 5. So write the arrow at angle a as eja and the arrow at angle b as ejb, and the familiar rule for powers says their product is ej(a+b): the arrow at angle a + b. That's "angles add", the rule you already know, wearing the clothes of a power. The angle sits in the exponent, and the j there marks it as a turn rather than growth.

\[ e^{ja} \cdot e^{jb} = e^{j(a+b)} \]

\[ e^{j\theta} = \cos\theta + j \sin\theta \]

That's all chapters 4–6 need. Whatever sits in the exponent after the j is an angle: ej·(some angle) is the arrow at that angle, and a minus sign, e−j·(some angle), means the same angle turned backwards. (Angles here are in radians, as in chapter 1: 2π ≈ 6.28 is a full turn, so π is half a turn and π/2 a quarter turn.)

What is ej·π/2 × ej·π/2? (π/2 is a quarter turn.) Give the angle of the result, in degrees.

Exponents add: ej·π, the arrow at angle π, half a turn: 180°, which is −1. It's j × j = −1 again, written as powers: ej·π/2 is j, the quarter turn.

Optional: why the number e, and does an exponent really land on a circle?

If ejθ still feels like a clever pun rather than real arithmetic, here's a check, built from nothing but this chapter's multiplication. Nothing later depends on it.

The number e ≈ 2.718 comes from growing in many tiny steps: ex is where you end up if you grow by x/n, n times over, (1 + x/n)n, as n gets large (it's the compound-interest limit). The question: what if each tiny step is a turn instead of growth? Put jθ in place of x, and each step multiplies by 1 + j·θ/n: an arrow that's 1 to the right and a tiny bit up. A step at right angles to where you point mostly turns you and barely stretches you. If ejθ really is the arrow at angle θ, then n of those steps should land on the circle at angle θ (the ring below). Do they?

With one big step, you overshoot badly: one big sideways jump makes the arrow much longer than 1. Now raise n. The steps get smaller, each one stretches less, and the path hugs the circle more and more tightly, landing almost exactly on the ring: length 1, angle θ.

They do: in the limit, infinitely many infinitely small sideways steps land exactly at length 1, angle θ. So ejθ isn't just a convenient name: the rule for powers, applied to a turn, really does walk you round the circle.

Pause and ponderSet θ to 3.14 (that's π, half a turn) and n to 60. Where do you land? You've just seen why ejπ = −1, the equation people call the most beautiful in mathematics. It says: turning half way round the circle flips you to the opposite side.

Writing it down

Here is the notation, now that you know what each piece means. The colors match the scenes.

z=I+j·Q (a+jb)·(c+jd) = (ac−bd)+j(ad+bc)
Optional: isn't this a matrix? (Yes.)

If you've met matrices, the formula above may look familiar. It is one. Nothing later in this course needs it, but it's a useful second view.

  1. Fix the multiplier z = c + jd and ask where it sends the grid's two steps: the sideways step 1 and the up step j.
  2. 1 lands on z itself: (c, d). j lands on z × j, which is z turned a quarter turn: (−d, c).
  3. Every arrow a + jb is a recipe, "a sideways steps and b up steps", and multiplying keeps the recipe: a·(c, d) + b·(−d, c) = (ac − bd, ad + bc), the formula above.
  4. A move of the whole plane that's fixed by where the two steps land is what a matrix is, with those landing spots as its columns:

\[ \begin{pmatrix} c & -d \\ d & c \end{pmatrix} \begin{pmatrix} a \\ b \end{pmatrix} = \begin{pmatrix} ac - bd \\ ad + bc \end{pmatrix} \]

Matrices in general can also shear, flip or squash the plane. Multiplying by an arrow only ever turns and stretches it, because its two columns are always the same length and at right angles. Turning is all a radio needs, so two numbers do the job of a matrix's four. To drag the two steps yourself and watch any matrix move the plane, see AI from scratch, chapter 1: a matrix moves the whole plane. This chapter's script checks the match: the same answer for 1000 random arrows.

ejθ=cos⁡θ+j·sin⁡θ ej·2πft

In code

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

From this chapter on, every idea ends as code: the real lines that run the receiver on this site, or, where the receiver doesn't use the idea yet, the chapter's own script. Every name you haven't met is explained just above its box.

The arrows arriving from the dongle. The USB cable delivers bytes, whole numbers from 0 to 255 (chapter 3 explains why), alternating I, Q, I, Q. A byte of 127.5 means zero, so each one is shifted and scaled to a number between −1 and +1:

Names in this code: u is the list of whole numbers (0 to 255) from the USB cable; out is the list of numbers it becomes, in the same order (I, Q, I, Q, …); i counts through them.

for (let i = 0; i < out.length; i++) out[i] = (u[i] - 127.5) / 127.5;
// out = [I0, Q0, I1, Q1, I2, Q2, …]: one arrow per pair

Multiplying arrows. This chapter's whole rule, as code, from the chapter's own checking script. An arrow is its two numbers, and multiplying is the formula above: sideways ac − bd, up ad + bc.

type Arrow = { I: number; Q: number };          // I = sideways, Q = up

const mul = (a: Arrow, b: Arrow): Arrow => ({
  I: a.I * b.I - a.Q * b.Q,                      // sideways: ac − bd
  Q: a.I * b.Q + a.Q * b.I,                      // up:       ad + bc
});

mul({ I: 0, Q: 1 }, { I: 0, Q: 1 })              // j × j  →  { I: -1, Q: 0 }: a half turn, −1

Every multiplication of arrows in the rest of the course is these two lines. Chapter 5 is where the receiver first uses them for real, and explains what for.

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. It builds arrows from scratch in about 30 lines. Its output:

j · j = { I: -1, Q: 0 } → a half turn: −1
(length 2, 30°) × (length 1.5, 45°) = (length 3.000, 75.000°)
(1, 2) + (3, −1) = { I: 4, Q: 1 }
1 × j × j × j × j = { I: 1, Q: 0 }
multiplying by z = c + jd vs the matrix [c −d; d c]: same answer for 1000 random arrows
n =      1: length 1.86210, angle 57.518°
n =      4: length 1.33221, angle 85.760°
n =     30: length 1.04192, angle 89.918°
n =   1000: length 1.00123, angle 90.000°
n = 100000: length 1.00001, angle 90.000°
→ it settles on length 1 at exactly 90°: the arrow at angle θ, which mathematicians write e^(jθ).
all checks passed

The last five lines are the tiny-turns scene in numbers: more, smaller sideways steps land ever closer to length 1 at exactly 90°.

Check yourself

Multiply an arrow of length 2 at 10° by an arrow of length 3 at 80°. What do you get?

Length 2 × 3 = 6, angle 10° + 80° = 90°: an arrow of length 6 pointing straight up, which is 6j.

What does multiplying by −1 do to an arrow, in turning language?

Turns it half way round (180°) without stretching. −1 is the arrow of length 1 at 180°.

Why is j × j = −1, without using any rules?

Multiplying by j turns a quarter. Doing it twice turns a half. Turning a half points the other way, which is what multiplying by −1 does.

You multiply by a step arrow of length 0.99 over and over. What happens, and why can't a radio's spinning arrow do that?

It spirals inward, losing 1% of its length every step, and soon fades to nothing. A radio's reference arrow must keep exactly the same length forever, which is also why our code recomputes it from an angle instead of multiplying repeatedly.

An arrow turns −30° per sample. What's special about it compared with one turning +30°?

It spins clockwise: a negative frequency. With both I and Q you can tell the two apart; with only one shadow you couldn't (chapter 1's game).

Last updated