---
title: "Project: simulating a noisy link"
heading: Build a digital link in Python and measure its error rate
eyebrow: A11 · Lesson 53 · Project
xp: 80
---

::: hook
Communication engineers judge every modulation and code by one graph: bit error rate against $E_b/N_0$, the energy per bit relative to the noise density. In this project you'll produce that graph yourself. You'll write a noise channel calibrated in $E_b/N_0$, send random bits through it, measure the error rate at several noise levels, and check the result against the Q-function formula. Then you'll test an obvious idea for doing better, sending every bit three times and taking a vote, and find out why it doesn't work the way you'd expect.
:::

::: play
- The solid curve is the bit error rate of plain antipodal signaling, $Q\left(\sqrt{2E_b/N_0}\right)$. The dashed curve is the same link with every bit sent three times at a third of the energy each, decided by majority vote.
- Move the marker. At every $E_b/N_0$, which is better?
- How many dB more would the voting link need to match the plain one at a BER of $10^{-3}$?
:::
:::

```sim plot
view: graph
x: g = 0..10 dB
y: BER = 1e-6..0.5 log
slider: g0 = 0..10 step 0.5 = 6 dB
plot: Q(sqrt(2*10^(g/10))) | label: plain
plot: 3*Q(sqrt(2*10^(g/10)/3))^2*(1 - Q(sqrt(2*10^(g/10)/3))) + Q(sqrt(2*10^(g/10)/3))^3 | label: 3 copies, majority vote | dashed | color: ch2
marker: x = g0 | label: Eb/N0
readout: plain BER = Q(sqrt(2*10^(g0/10))) | digits: 3
readout: voting BER = 3*Q(sqrt(2*10^(g0/10)/3))^2*(1 - Q(sqrt(2*10^(g0/10)/3))) + Q(sqrt(2*10^(g0/10)/3))^3 | digits: 3
```

::: predict
## Each bit is sent three times, each copy with a third of the bit's energy, and the receiver takes a majority vote of three hard decisions. Compared with sending each bit once at full energy, the error rate is:

- [ ] Much lower, because a single error is corrected
- [x] Higher
- [ ] The same
- [ ] Lower at high noise, higher at low noise

::: reveal
Higher, at every noise level. Splitting the energy three ways makes each copy much noisier, and the vote can't recover what the hard decisions threw away. At 6 dB the plain link has a BER of 0.24% and the voting link 0.77%. Adding the three received voltages before deciding would exactly match the plain link, and real codes do much better than either by spreading each bit's information across many symbols cleverly.
:::
:::

::: nameit
## Calibrating the noise

Antipodal signaling sends $\pm\sqrt{E_b}$ per bit. White noise of one-sided density $N_0$, sampled by a matched receiver, adds Gaussian noise with variance $\frac{N_0}{2}$ to each symbol. With symbols scaled to $\pm 1$ ($E_b = 1$), the noise standard deviation is

$$\sigma = \sqrt{\frac{1}{2E_b/N_0}}$$

and the error rate is $Q\left(\frac{1}{\sigma}\right) = Q\left(\sqrt{2E_b/N_0}\right)$. $E_b/N_0$ is quoted in dB: $E_b/N_0 = 10^{\text{dB}/10}$.

## Measuring a BER curve

At each $E_b/N_0$, send $n$ random bits, count the errors $k$, and report $\hat p = k/n$ with its error bar $\sqrt{\hat p(1 - \hat p)/n}$ (lesson 31). Plot on a log scale: the curve drops like a cliff, because the Gaussian tail does.

## Repetition and voting

Sending a bit as three copies, each with energy $E_b/3$, gives each copy an error probability $p = Q\left(\sqrt{2E_b/3N_0}\right)$. A majority vote fails when two or three copies are wrong:

$$P_b = 3p^2(1 - p) + p^3$$

Fair comparisons hold the energy per bit fixed, and on that basis this code loses. Coding gain comes from codes that add redundancy more efficiently, and from **soft decisions** that use the received voltages rather than only their signs.
:::

::: example
## What noise standard deviation corresponds to $E_b/N_0 = 3$ dB, for symbols of ±1?

::: solution
1. **Convert from decibels.**
   $$10^{0.3} = 2.0$$
2. **Noise standard deviation.**
   $$\begin{aligned} \sigma &= \sqrt{\frac{1}{2 \times 2.0}} \\ &= 0.50 \end{aligned}$$
   a noise variance of 0.25.
3. **Expected BER.**
   $$Q(2.0) = 0.023$$

**Answer:** $\sigma = 0.50$, a noise variance of 0.25, and the expected BER is 0.023.
:::
:::

::: example
## At $E_b/N_0 = 6$ dB, how do the plain and voting links compare?

::: solution
1. **Convert from decibels.** $E_b/N_0 = 3.98$.
2. **Plain link.**
   $$\begin{aligned} P_b &= Q(\sqrt{7.96}) \\ &= Q(2.82) \\ &= 0.0024 \end{aligned}$$
3. **One copy in the voting link.**
   $$\begin{aligned} p &= Q(\sqrt{7.96/3}) \\ &= Q(1.63) \\ &= 0.052 \end{aligned}$$
4. **Voting.**
   $$\begin{aligned} P_b &= 3(0.052)^2(0.948) + 0.052^3 \\ &= 0.0077 \end{aligned}$$
   about three times worse.
5. **Simulation.** A simulation of 400,000 bits gives 0.0023 and 0.0077.

**Answer:** the plain link has a BER of 0.0024 and the voting link 0.0077, about three times worse.
:::
:::

::: check
kind: python

## Step 1. Write `awgn(x, ebn0_db, rng)` that adds Gaussian noise with standard deviation $\sqrt{1/(2E_b/N_0)}$ to the array `x`, with $E_b/N_0$ given in dB.

```python starter
import numpy as np

def awgn(x, ebn0_db, rng):
    return x

y = awgn(np.ones(100_000), 3, np.random.default_rng(1))
print(np.var(y - 1))
```

```python solution
import numpy as np

def awgn(x, ebn0_db, rng):
    ebn0 = 10 ** (ebn0_db / 10)
    return x + rng.normal(0, np.sqrt(1 / (2 * ebn0)), np.shape(x))

y = awgn(np.ones(100_000), 3, np.random.default_rng(1))
print(np.var(y - 1))
```

```python tests
import numpy as np
for db in (0, 3, 10):
    y = awgn(np.zeros(200_000), db, np.random.default_rng(5))
    v = 1 / (2 * 10 ** (db / 10))
    assert abs(np.var(y) - v) / v < 0.02, f"At {db} dB the noise variance should be {v:.4f}; got {np.var(y):.4f}."
    assert abs(np.mean(y)) < 0.01, "The noise should have zero mean."
assert awgn(np.ones((4, 5)), 3, np.random.default_rng(0)).shape == (4, 5), "Keep the input's shape."
```

::: right
Right. The noise is calibrated: at 3 dB the variance is 0.25.
:::
:::

::: check
kind: python

## Step 2. Write `ber(ebn0_db, n, rng)`: send `n` random bits as ±1, pass them through `awgn`, decide by the sign, and return the fraction wrong. `awgn` is included.

```python starter
import numpy as np

def awgn(x, ebn0_db, rng):
    return x + rng.normal(0, np.sqrt(1 / (2 * 10 ** (ebn0_db / 10))), np.shape(x))

def ber(ebn0_db, n, rng):
    return 0.0

for db in (0, 2, 4, 6):
    print(db, ber(db, 200_000, np.random.default_rng(db)))
```

```python solution
import numpy as np

def awgn(x, ebn0_db, rng):
    return x + rng.normal(0, np.sqrt(1 / (2 * 10 ** (ebn0_db / 10))), np.shape(x))

def ber(ebn0_db, n, rng):
    bits = rng.integers(0, 2, n)
    y = awgn(2.0 * bits - 1, ebn0_db, rng)
    return np.mean((y > 0) != (bits == 1))

for db in (0, 2, 4, 6):
    print(db, ber(db, 200_000, np.random.default_rng(db)))
```

```python tests
import numpy as np
from math import erfc, sqrt
for db in (0, 2, 4, 6):
    q = 0.5 * erfc(sqrt(10 ** (db / 10)))
    p = ber(db, 400_000, np.random.default_rng(20 + db))
    sd = sqrt(q * (1 - q) / 400_000)
    assert abs(p - q) < 4 * sd, f"At {db} dB the BER should be near Q(√(2Eb/N0)) = {q:.5f}; got {p:.5f}."
```

::: right
Right. Your measured curve matches $Q\left(\sqrt{2E_b/N_0}\right)$ within its error bars at every point.
:::
:::

::: check
kind: python

## Step 3. Write `rep3(ebn0_db, n, rng)`: send each of `n` bits as three copies of $\pm\frac{1}{\sqrt3}$ (so the energy per bit is still 1), add noise with `awgn` at the same $E_b/N_0$, take a majority vote of the three signs, and return the BER. `awgn` is included.

```python starter
import numpy as np

def awgn(x, ebn0_db, rng):
    return x + rng.normal(0, np.sqrt(1 / (2 * 10 ** (ebn0_db / 10))), np.shape(x))

def rep3(ebn0_db, n, rng):
    return 0.0

print(rep3(6, 200_000, np.random.default_rng(1)))
```

```python solution
import numpy as np

def awgn(x, ebn0_db, rng):
    return x + rng.normal(0, np.sqrt(1 / (2 * 10 ** (ebn0_db / 10))), np.shape(x))

def rep3(ebn0_db, n, rng):
    bits = rng.integers(0, 2, n)
    x = np.repeat(2.0 * bits - 1, 3) / np.sqrt(3)
    y = awgn(x, ebn0_db, rng).reshape(n, 3)
    votes = np.sum(y > 0, axis=1) >= 2
    return np.mean(votes != (bits == 1))

print(rep3(6, 200_000, np.random.default_rng(1)))
```

```python tests
import numpy as np
from math import erfc, sqrt
for db in (2, 6):
    p1 = 0.5 * erfc(sqrt(10 ** (db / 10) / 3))
    q = 3 * p1**2 * (1 - p1) + p1**3
    p = rep3(db, 400_000, np.random.default_rng(30 + db))
    sd = sqrt(q * (1 - q) / 400_000)
    assert abs(p - q) < 4 * sd, f"At {db} dB the voting BER should be near {q:.5f}; got {p:.5f}. Did you split the energy three ways?"
    plain = 0.5 * erfc(sqrt(10 ** (db / 10)))
    assert p > plain, "With the energy split three ways, voting should be worse than the plain link."
```

::: right
Right. Voting on hard decisions loses to the plain link. Try summing the three received values and deciding on the sign of the sum: it matches the plain link exactly.
:::
:::

::: check
given: g = 4 | 6 | 8
answer: 3*Q(sqrt(2*10^(g/10)/3))^2*(1 - Q(sqrt(2*10^(g/10)/3))) + Q(sqrt(2*10^(g/10)/3))^3
tolerance: 1%

## Step 4. Using the formula (or your simulation), what BER does the three-copy voting link have at $E_b/N_0$ = {g} dB?

::: right
Right. {answer}, compared with $Q\left(\sqrt{2E_b/N_0}\right)$ for the plain link.
:::

::: misses
- [Q(sqrt(2*10^(g/10)))] That's the plain link.
- [3*Q(sqrt(2*10^(g/10)))^2] Each copy has only a third of the energy.
- [any] Not yet. Find each copy's error rate $p$, then $3p^2(1 - p) + p^3$.
:::
:::

::: explain
## Why does voting on three hard decisions lose to sending one bit at full energy?

::: model
The total energy per bit is fixed, so each copy gets a third of it, and its own error rate is much higher than the single full-energy symbol's. Each hard decision then throws away how confident it was: a copy that barely crossed zero counts as much as one far from it. The vote combines three unreliable yes-or-no answers, and two of the three being wrong is more likely than one full-energy symbol being wrong. Adding the voltages before deciding keeps the confidence information and recovers the plain link's performance, which is the idea behind soft-decision decoding.
:::
:::

::: practice

::: check
given: g = 0 | 3 | 10
answer: sqrt(1/(2*10^(g/10)))
tolerance: 0.5%

## For symbols of ±1, what noise standard deviation corresponds to $E_b/N_0$ = {g} dB?

::: misses
- [1/(2*10^(g/10))] That's the variance.
- [any] $\sigma = \sqrt{1/(2E_b/N_0)}$.
:::
:::

::: check
given: g = 2..10 int
answer: Q(sqrt(2*10^(g/10)))
tolerance: 1%

## What is the BER of antipodal signaling at $E_b/N_0$ = {g} dB?

::: misses
- [Q(sqrt(10^(g/10)))] $Q\left(\sqrt{2E_b/N_0}\right)$: don't drop the 2.
- [any] Convert dB to a ratio first.
:::
:::

::: check
given: p = 0.01 | 0.02 | 0.03 | 0.05 | 0.08 | 0.1
answer: 3*p^2*(1 - p) + p^3
tolerance: 0.5%

## Each of three copies is wrong with probability {p}, independently. What is the chance a majority vote is wrong?

::: misses
- [3*p^2] Include the chance all three are wrong, and that the third is right in the two-wrong case.
- [any] $3p^2(1 - p) + p^3$.
:::
:::
:::
