---
title: Propagation lab
heading: The exact two-ray model, fading margins by simulation and range from a link budget, in Python
eyebrow: S5 · Lesson 48 · Propagation lab
xp: 60
---

::: hook
Lessons 42 and 43 gave link planners' models: Friis, the two-ray ground reflection with its $1/d^4$ law, Fresnel clearance and diffraction. Mobile links add fading: the signal is the sum of many reflections whose phases shift as anything moves, and its strength fluctuates by tens of decibels. This lab computes the exact two-ray field and finds where the $1/d^4$ approximation takes over, simulates Rayleigh fading to measure the margin a link needs, and solves a link budget for its range.
:::

::: play
- The curve is the probability that a Rayleigh-fading signal falls more than M decibels below its mean, $1 - e^{-10^{-M/10}}$: about 10% for 10 dB of margin, 1% for 20 dB, 0.1% for 30 dB.
- Each additional decade of reliability costs another 10 dB of margin, which is why fading channels need diversity rather than ever more power.
- The lab draws complex Gaussian samples and counts the deep fades.
:::

```sim plot
view: graph
x: M = 0..40 dB
y: P = 0.00001..1 log
plot: 1 - exp(-10^(-M/10)) | label: probability of a fade below M
```

::: predict
## A 900 MHz base station 30 m high talks to a phone 2 m high over flat ground. At 7.2 km, ten times the breakpoint distance, how does the exact two-ray received level compare with Friis?

- [ ] Within a decibel of Friis
- [x] About 10 dB below Friis, and within 0.1 dB of the $h_t^2h_r^2/d^4$ approximation
- [ ] About 10 dB above Friis
- [ ] Zero: the rays cancel completely

::: reveal
The exact sum of the direct and reflected rays gives −118.8 dB relative to isotropic antennas, against Friis's −108.7 dB: 10 dB lower. The $1/d^4$ approximation gives −118.7 dB. At 72 km the gap from Friis is 30 dB: beyond the breakpoint the ground reflection costs 20 dB per decade.
:::
:::

::: nameit
## The models

- **Exact two-ray:** with direct and reflected paths $r_1 = \sqrt{d^2 + (h_t - h_r)^2}$ and $r_2 = \sqrt{d^2 + (h_t + h_r)^2}$ and a ground reflection coefficient of −1, the received field is proportional to $\left|\frac{e^{-jkr_1}}{r_1} - \frac{e^{-jkr_2}}{r_2}\right|$; multiplying by $\lambda/4\pi$ gives the path gain (in dB, minus the path loss).
- **Rayleigh fading:** the received amplitude is the magnitude of a complex Gaussian; its power is exponentially distributed, so the chance of falling below a fraction x of the mean is $1 - e^{-x}$.
- **Range from a budget:** with path loss $L_0 + 10n\log_{10}(d/d_0)$ and a budget B in dB for path loss and margin, the range is $d_0\cdot10^{(B - L_0 - M)/(10n)}$.
:::

::: check
kind: python

## Write `two_ray_db(d, ht, hr, f)`: the exact two-ray path gain in dB (negative), and `approx_db(d, ht, hr)`: $20\log_{10}(h_th_r/d^2)$.

```python starter
def two_ray_db(d, ht, hr, f):
    return 0.0

def approx_db(d, ht, hr):
    return 0.0
```

```python solution
import cmath, math

def two_ray_db(d, ht, hr, f):
    lam = 299792458 / f
    k = 2 * math.pi / lam
    r1, r2 = math.hypot(d, ht - hr), math.hypot(d, ht + hr)
    E = cmath.exp(-1j * k * r1) / r1 - cmath.exp(-1j * k * r2) / r2
    return 20 * math.log10(abs(E) * lam / (4 * math.pi))

def approx_db(d, ht, hr):
    return 20 * math.log10(ht * hr / d ** 2)
```

```python tests
import math
assert abs(two_ray_db(7200, 30, 2, 900e6) - approx_db(7200, 30, 2)) < 0.1, "Ten times the breakpoint: within 0.1 dB of the 1/d⁴ law."
friis = lambda d: -20 * math.log10(4 * math.pi * d * 900e6 / 299792458)
assert abs(two_ray_db(7200, 30, 2, 900e6) - friis(7200) + 10.1) < 0.2, "About 10 dB below Friis there."
assert abs(two_ray_db(72000, 30, 2, 900e6) - approx_db(72000, 30, 2)) < 0.01, "Farther still, the approximation is nearly exact."
```

::: right
Right. Beyond the breakpoint the two paths differ by much less than a wavelength, so they cancel more and more nearly.
:::
:::

::: check
kind: python

## Write `outage(M)`: the probability that a Rayleigh-fading signal falls more than M dB below its mean, and `simulate_outage(M, n, seed)`: estimate it from n complex Gaussian samples (each component with variance 1/2, so the mean power is 1).

```python starter
def outage(M):
    return 0.0

def simulate_outage(M, n, seed):
    return 0.0
```

```python solution
import math, random

def outage(M):
    return 1 - math.exp(-10 ** (-M / 10))

def simulate_outage(M, n, seed):
    r = random.Random(seed)
    s = 1 / math.sqrt(2)
    thr = 10 ** (-M / 10)
    cnt = 0
    for _ in range(n):
        x, y = r.gauss(0, s), r.gauss(0, s)
        if x * x + y * y < thr:
            cnt += 1
    return cnt / n
```

```python tests
assert abs(outage(10) - 0.0952) < 1e-4 and abs(outage(20) - 0.00995) < 1e-5, "10 dB: 9.5%; 20 dB: 1%."
assert abs(simulate_outage(10, 200000, 1) / outage(10) - 1) < 0.03, "Simulation agrees at 10 dB."
assert abs(simulate_outage(20, 200000, 1) / outage(20) - 1) < 0.08, "And at 20 dB, with fewer events to count."
```

::: right
Right. Each 10 dB of margin buys a tenfold reduction in outage: the exponential distribution's tail at small values is linear.
:::
:::

::: check
kind: python

## Write `range_km(budget_db, L0_db, n, margin_db, d0_km=1.0)`: the distance at which path loss $L_0 + 10n\log_{10}(d/d_0)$ plus the fade margin uses up the budget.

```python starter
def range_km(budget_db, L0_db, n, margin_db, d0_km=1.0):
    return 1.0
```

```python solution
def range_km(budget_db, L0_db, n, margin_db, d0_km=1.0):
    return d0_km * 10 ** ((budget_db - L0_db - margin_db) / (10 * n))
```

```python tests
assert abs(range_km(150, 120, 3.5, 10) - 10 ** (20 / 35)) < 1e-12, "20 dB of excess at n = 3.5: 3.7 km."
assert abs(range_km(150, 120, 2, 10) / range_km(150, 120, 4, 10) - 10 ** 0.5) < 1e-9, "Exponent 2 against 4: 10 km against 3.2 km."
assert range_km(150, 120, 3.5, 20) < range_km(150, 120, 3.5, 10), "More margin, shorter range."
```

::: right
Right. With steep path-loss exponents, every 10 dB of fade margin costs a large fraction of the range.
:::
:::

::: explain
## Why do fading channels need large margins, and why does diversity help more than extra power?

::: model
In a fading channel the received signal is the sum of many reflections whose phases change as the receiver or its surroundings move, so its power fluctuates randomly about its mean and sometimes falls very low when the reflections nearly cancel. For Rayleigh fading the probability of a fade deeper than a given level falls only in proportion to that level, so each tenfold reduction in outage costs 10 dB of margin, which is very expensive in transmit power. Diversity, receiving through two or more independently fading paths, makes a deep fade require all of them to fade at once, so the outage probability falls as a power of the margin instead, and a few decibels of combined margin achieve what tens of decibels of extra power would.
:::
:::
