---
title: Antenna and link lab
heading: Array patterns, beamwidths, directivity and link budgets in Python
eyebrow: C10 · Lesson 46 · Antenna and link lab
xp: 50
---

::: hook
Lessons 40 to 44 gave antennas and links by formula. Real patterns are often known only as a function to be evaluated, and real links have a dozen terms. In this lab you'll compute an array's pattern element by element, find its beamwidth by searching the pattern, integrate any pattern over the sphere to get its directivity, and turn a link budget into a margin and a maximum range.
:::

::: play
- The curve is a uniform linear array's pattern in dB against the angle from broadside, for N elements d wavelengths apart, steered to $\theta_0$.
- Add elements: the main beam narrows as about 1/N, and the first side lobes stay near −13 dB.
- Widen the spacing past half a wavelength and steer off broadside: grating lobes, full-size copies of the main beam, rise at other angles.
:::

```sim plot
view: graph
x: t = -90..90 °
y: A = -40..1 dB
slider: N = 2..32 step 1 = 8
slider: d = 0.25..1.5 step 0.05 = 0.5
slider: s = -60..60 step 5 = 0 °
plot: 20*log(abs((sin(N*pi*d*(sin(t*pi/180) - sin(s*pi/180))) + N*0.000000000001)/(N*(sin(pi*d*(sin(t*pi/180) - sin(s*pi/180))) + 0.000000000001))) + 0.00001) | samples: 1500 | label: array pattern
readout: approximate beamwidth at broadside, ° = 50.8/(N*d) | digits: 3
```

::: predict
## Numerically, what is the directivity of a half-wave dipole, whose pattern is $\left(\cos(\frac\pi2\cos\theta)/\sin\theta\right)^2$?

- [ ] 1.0
- [ ] 1.5
- [x] 1.64
- [ ] 2.0

::: reveal
1.64, or 2.15 dBi. The short dipole's $\sin^2\theta$ gives 1.5; the half-wave dipole's pattern is a little narrower, so a little more directive. Numerical integration over θ gives these to as many digits as you like, which is how directivities of real antennas are computed from measured patterns.
:::
:::

::: nameit
## Array factor

N equal elements d wavelengths apart, each fed with a phase step α from the last, give

$$AF(\theta) = \frac1N\left\lvert\sum_{n=0}^{N-1}e^{jn\psi}\right\rvert, \qquad \psi = 2\pi d\sin\theta + \alpha$$

with θ from broadside. Steering to $\theta_0$ uses $\alpha = -2\pi d\sin\theta_0$. The sum is computed directly in code, which also handles unequal weights.

## Beamwidth by search

The half-power beamwidth is twice the angle where $AF^2$ falls to ½. Bisection between 0 and 90° finds it to any accuracy. For a broadside array it's about $50.8°/(Nd)$.

## Directivity by integration

For a pattern U(θ) that doesn't depend on azimuth,

$$D = \frac{4\pi U_\text{max}}{\int_0^{2\pi}\!\int_0^\pi U\sin\theta\,d\theta\,d\phi} = \frac{2U_\text{max}}{\int_0^\pi U(\theta)\sin\theta\,d\theta}$$

A midpoint rule with a few thousand points is accurate to many digits for smooth patterns.

## Margin and range

The margin is received power less the noise floor less the required SNR. In free space it falls 20 dB per decade of distance, so the range at zero margin is the distance at which the margin was computed times $10^{\text{margin}/20}$.
:::

::: example
## What is the half-power beamwidth of an 8-element broadside array at half-wavelength spacing?

::: solution
1. **Search the array factor.** Solving $AF^2 = \tfrac12$ gives a half-angle of 6.40°.
2. **Beamwidth.**
   $$2 \times 6.40^\circ = 12.8^\circ$$
3. **Rule of thumb.**
   $$\begin{aligned} \frac{50.8}{Nd} &= \frac{50.8}{4} \\ &= 12.7^\circ \end{aligned}$$
   within 1%.
4. **More elements.** Doubling to 16 elements gives 6.36°.

**Answer:** the half-power beamwidth is 12.8°.
:::
:::

::: example
## A 2.4 GHz link sends 20 dBm through 2 dBi antennas, with a 20 MHz receiver of 6 dB noise figure that needs 25 dB of SNR. What is its margin at 50 m, and its free-space range?

::: solution
1. **Received power.**
   $$P_r = -50.0\ \text{dBm}$$
2. **Noise.**
   $$N = -95.0\ \text{dBm}$$
3. **SNR and margin.** The SNR is 45.0 dB, so the margin over 25 dB is
   $$45.0 - 25 = 20.0\ \text{dB}$$
4. **Free-space range.**
   $$50 \times 10^{20/20}\ \text{m} = 500\ \text{m}$$
   more precisely 498 m.
5. **Indoors.** Walls take that margin quickly.

**Answer:** a 20.0 dB margin at 50 m, and a free-space range of about 500 m (498 m more precisely).
:::
:::

::: check
given: N = 4 | 8 | 16 | 32; d = 0.5 | 0.75
answer: 50.8/(N*d)
unit: °
tolerance: 0.5%

## About what is the broadside beamwidth of {N} elements {d}λ apart, by the rule of thumb?

::: right
Right: about {answer}°.
:::

::: misses
- [any] Not yet. $50.8°/(Nd)$.
:::
:::

::: check
kind: python

## Write `array_factor(N, d, alpha, theta)`: the normalized array factor for N elements d wavelengths apart with phase step `alpha` degrees, at `theta` degrees from broadside, by summing the elements directly.

```python starter
def array_factor(N, d, alpha, theta):
    return 1.0
```

```python solution
import math

def array_factor(N, d, alpha, theta):
    psi = 2 * math.pi * d * math.sin(math.radians(theta)) + math.radians(alpha)
    re = sum(math.cos(n * psi) for n in range(N))
    im = sum(math.sin(n * psi) for n in range(N))
    return math.hypot(re, im) / N
```

```python tests
assert abs(array_factor(8, 0.5, 0, 0) - 1) < 1e-12, "Broadside, at broadside: 1."
assert array_factor(8, 0.5, 0, 30) < 1e-9, "Eight elements λ/2 apart have a null at 30°."
assert abs(array_factor(8, 0.5, -90, 30) - 1) < 1e-12, "A -90° phase step steers the beam to 30°."
assert abs(array_factor(2, 0.5, 0, 90)) < 1e-12, "Two elements λ/2 apart cancel along the line of the array."
```

::: right
Right. A direct sum handles any weights, not just equal ones.
:::
:::

::: check
kind: python

## Write `beamwidth(N, d)`: the half-power beamwidth, in degrees, of a uniform broadside array, found by bisection on $AF^2 = 1/2$. Write `array_factor` again inside your answer, or reuse it.

```python starter
def beamwidth(N, d):
    return 50.8 / (N * d)
```

```python solution
import math

def array_factor(N, d, alpha, theta):
    psi = 2 * math.pi * d * math.sin(math.radians(theta)) + math.radians(alpha)
    re = sum(math.cos(n * psi) for n in range(N))
    im = sum(math.sin(n * psi) for n in range(N))
    return math.hypot(re, im) / N

def beamwidth(N, d):
    lo, hi = 0.0, 90.0
    for _ in range(100):
        mid = (lo + hi) / 2
        if array_factor(N, d, 0, mid) ** 2 > 0.5:
            lo = mid
        else:
            hi = mid
    return 2 * lo
```

```python tests
assert abs(beamwidth(8, 0.5) - 12.8025) < 1e-3, f"8 elements at λ/2: 12.80°, got {beamwidth(8, 0.5):.4f}."
assert abs(beamwidth(16, 0.5) - 6.3587) < 1e-3, f"16 elements: 6.36°, got {beamwidth(16, 0.5):.4f}."
assert abs(beamwidth(4, 0.5) - 26.323) < 1e-3, f"4 elements: 26.32°, got {beamwidth(4, 0.5):.4f}."
```

::: right
Right. The exact search confirms the rule of thumb to about 1%.
:::
:::

::: check
kind: python

## Write `directivity(U)`: the directivity of an azimuthally symmetric pattern `U(theta)` (θ in radians from the axis), by the midpoint rule with 20,000 points.

```python starter
def directivity(U):
    return 1.0
```

```python solution
import math

def directivity(U):
    n = 20000
    th = [math.pi * (i + 0.5) / n for i in range(n)]
    vals = [U(t) for t in th]
    integral = sum(v * math.sin(t) for v, t in zip(vals, th)) * math.pi / n
    return 2 * max(vals) / integral
```

```python tests
import math
assert abs(directivity(lambda t: math.sin(t) ** 2) - 1.5) < 1e-6, "A short dipole: 1.5."
D = directivity(lambda t: (math.cos(math.pi / 2 * math.cos(t)) / math.sin(t)) ** 2)
assert abs(D - 1.6409) < 1e-4, f"A half-wave dipole: 1.641, got {D:.5f}."
assert abs(directivity(lambda t: 1.0) - 1) < 1e-6, "An isotropic pattern: 1."
```

::: right
Right. Any pattern you can evaluate, you can integrate.
:::
:::

::: check
kind: python

## Write `link_margin(pt, gt, gr, f, d, B, nf, snr)` in dB (pt in dBm, gains in dBi, f in Hz, d in m, B in Hz, nf and snr in dB) for free space, and `max_range(pt, gt, gr, f, B, nf, snr)`, the distance in meters where the margin reaches 0.

```python starter
def link_margin(pt, gt, gr, f, d, B, nf, snr):
    return 0.0

def max_range(pt, gt, gr, f, B, nf, snr):
    return 1.0
```

```python solution
import math

def link_margin(pt, gt, gr, f, d, B, nf, snr):
    lam = 2.998e8 / f
    pr = pt + gt + gr - 20 * math.log10(4 * math.pi * d / lam)
    return pr - (-174 + 10 * math.log10(B) + nf) - snr

def max_range(pt, gt, gr, f, B, nf, snr):
    return 10 ** (link_margin(pt, gt, gr, f, 1.0, B, nf, snr) / 20)
```

```python tests
m = link_margin(20, 2, 2, 2.4e9, 50, 20e6, 6, 25)
assert abs(m - 19.9585) < 1e-3, f"The Wi-Fi link has 19.96 dB of margin at 50 m, got {m:.4f}."
r = max_range(20, 2, 2, 2.4e9, 20e6, 6, 25)
assert abs(r - 497.62) < 0.05, f"Its free-space range is 497.6 m, got {r:.2f}."
assert abs(link_margin(20, 2, 2, 2.4e9, r, 20e6, 6, 25)) < 1e-9, "At the range, the margin is zero."
```

::: right
Right. A link budget is a function; its root is the range.
:::
:::

::: explain
## Why do grating lobes appear when array elements are spaced more than half a wavelength apart?

::: model
An array's pattern repeats whenever the phase difference between neighboring elements' contributions, $\psi = 2\pi d\sin\theta + \alpha$, changes by a full $2\pi$: the elements then add in phase again, exactly as at the main beam. As θ sweeps from −90° to 90°, $\sin\theta$ covers a range of 2, so ψ sweeps a range of $4\pi d$. With d below half a wavelength (and no steering) that's less than $2\pi$, so only one in-phase direction exists. With wider spacing, or when steering shifts the range, ψ reaches another multiple of $2\pi$ at some real angle, and a second full-size beam, a grating lobe, appears there.
:::
:::
