---
title: Array lab
heading: Planar-array directivity by integration, scan loss, beam squint by search and failed elements, in Python
eyebrow: S5 · Lesson 37 · Array lab
xp: 60
---

::: hook
Lessons 32 to 34 gave rules of thumb for arrays: directivity from aperture area, a beam that broadens as $1/\cos\theta$ when scanned, squint from phase shifters, and gain that degrades gracefully when elements fail. This lab checks each one the hard way: it integrates a planar array's pattern over the whole sphere to get its directivity, measures how that falls with scan angle, finds a squinted beam's peak by searching the pattern, and computes what failed elements cost.
:::

::: play
- The curve is an idealized phased array's scan loss: its projected aperture shrinks as $\cos\theta_0$, so its directivity falls by $10\log_{10}\cos\theta_0$ dB.
- At 60° the projected aperture is half: 3 dB lost, before the elements' own patterns take more.
- The lab integrates the exact array factor to check this.
:::

```sim plot
view: graph
x: t = 0..80 °
y: L = -10..0 dB
plot: 10*log(cos(t*pi/180)) | label: scan loss
```

::: predict
## An 8 × 8 array of isotropic elements spaced λ/2 apart radiates equally toward both faces. Its area is 16λ². About what broadside directivity does it have?

- [ ] About 201 (4πA/λ²)
- [x] About 100 (2πA/λ²), a bit less in practice
- [ ] About 64 (the element count)
- [ ] About 16

::: reveal
An aperture radiating into one half-space has $4\pi A/\lambda^2$, but isotropic elements with no ground plane radiate equally behind, splitting the power between two beams: $2\pi A/\lambda^2 = 100$. Integrating the array factor over the sphere gives 94, the shortfall coming from the array's finite size and its edges.
:::
:::

::: nameit
## What the lab computes

- **Planar array factor:** for an $N_x \times N_y$ grid with spacings $d_x$, $d_y$ (in wavelengths) steered to $(\theta_0, \phi_0)$, the pattern is the product of two linear factors in $u_x = \sin\theta\cos\phi - \sin\theta_0\cos\phi_0$ and $u_y = \sin\theta\sin\phi - \sin\theta_0\sin\phi_0$, each $\sin(N\pi du)/\sin(\pi du)$.
- **Directivity by integration:** $D = 4\pi|AF|^2_\text{peak}/\oint|AF|^2\,d\Omega$, with the integral as a sum over a θ–φ grid weighted by sin θ.
- **Squint by search:** with phase shifters set for $\theta_0$ at $f_0$, the pattern at $f = rf_0$ has $\psi = 2\pi d(r\sin\theta - \sin\theta_0)$; search θ for its peak.
- **Failed elements:** if a fraction p of elements fail open, the coherent peak field falls to (1 − p) of its value and the radiated power to (1 − p): the EIRP drops by $20\log_{10}(1 - p)$ dB and the directivity by $10\log_{10}(1 - p)$.
:::

::: check
kind: python

## Write `planar_directivity(Nx, Ny, dx, dy, th0_deg=0, n=200)`: the directivity of a planar array of isotropic elements steered to (θ₀, φ = 0), by summing $|AF|^2\sin\theta$ over n steps in θ and 2n in φ.

```python starter
def planar_directivity(Nx, Ny, dx, dy, th0_deg=0, n=200):
    return 4 * 3.14159 * Nx * dx * Ny * dy
```

```python solution
import math

def planar_directivity(Nx, Ny, dx, dy, th0_deg=0, n=200):
    th0 = math.radians(th0_deg)
    def lin(N, d, u):
        x = math.pi * d * u
        return N if abs(math.sin(x)) < 1e-12 else math.sin(N * x) / math.sin(x)
    def p(th, ph):
        ux = math.sin(th) * math.cos(ph) - math.sin(th0)
        uy = math.sin(th) * math.sin(ph)
        return (lin(Nx, dx, ux) * lin(Ny, dy, uy)) ** 2
    dth, dph = math.pi / n, math.pi / n
    tot = 0.0
    for i in range(n):
        th = (i + 0.5) * dth
        s = math.sin(th)
        for j in range(2 * n):
            tot += p(th, (j + 0.5) * dph) * s * dth * dph
    return 4 * math.pi * (Nx * Ny) ** 2 / tot
```

```python tests
import math
D0 = planar_directivity(8, 8, 0.5, 0.5)
assert abs(D0 / (2 * math.pi * 16) - 1) < 0.1, "Within 10% of 2πA/λ²: about 94."
D30 = planar_directivity(8, 8, 0.5, 0.5, 30)
assert abs(D30 / D0 - math.cos(math.radians(30))) < 0.01, "Scanning to 30° costs about cos 30°."
```

::: right
Right. The cos θ₀ scan loss isn't an assumption here: it comes out of the integral.
:::
:::

::: check
kind: python

## Write `squint_peak(N, d, th0_deg, r)`: the direction (degrees, searched in 0.01° steps from −90° to 90°) of the peak of an N-element linear array spaced d wavelengths, phase-steered to th0 at its design frequency, at r times that frequency.

```python starter
def squint_peak(N, d, th0_deg, r):
    return th0_deg
```

```python solution
import math

def squint_peak(N, d, th0_deg, r):
    s0 = math.sin(math.radians(th0_deg))
    best, bth = -1.0, 0.0
    for i in range(-9000, 9001):
        th = math.radians(i / 100)
        psi = 2 * math.pi * d * (r * math.sin(th) - s0)
        a = N if abs(math.sin(psi / 2)) < 1e-12 else abs(math.sin(N * psi / 2) / math.sin(psi / 2))
        if a > best:
            best, bth = a, th
    return math.degrees(bth)
```

```python tests
import math
assert abs(squint_peak(32, 0.5, 45, 1.1) - math.degrees(math.asin(math.sin(math.radians(45)) / 1.1))) < 0.01, "The search lands on asin(sin θ0 / r): 40.0°."
assert abs(squint_peak(32, 0.5, 0, 1.2)) < 0.01, "Broadside never squints."
```

::: right
Right. Searching the pattern confirms the closed form, and keeps working when element patterns or errors make the closed form inexact.
:::
:::

::: check
kind: python

## Write `failure_loss_db(p)`: return (EIRP loss, directivity loss) in dB when a fraction p of an array's elements fail open; and `simulate_failures(N, p, trials, seed)`: the average EIRP loss in dB when each of N elements fails independently with probability p, computed from the number that survive.

```python starter
def failure_loss_db(p):
    return (0.0, 0.0)

def simulate_failures(N, p, trials, seed):
    return 0.0
```

```python solution
import math, random

def failure_loss_db(p):
    return (-20 * math.log10(1 - p), -10 * math.log10(1 - p))

def simulate_failures(N, p, trials, seed):
    r = random.Random(seed)
    tot = 0.0
    for _ in range(trials):
        k = sum(1 for _ in range(N) if r.random() >= p)
        tot += -20 * math.log10(k / N)
    return tot / trials
```

```python tests
e, d = failure_loss_db(0.1)
assert abs(e - 0.915) < 0.001 and abs(d - 0.458) < 0.001, "10% failed: 0.92 dB of EIRP, 0.46 dB of directivity."
assert abs(simulate_failures(256, 0.1, 400, 3) - e) < 0.03, "Random failures in a large array cost about the same."
```

::: right
Right. Large arrays degrade gracefully: losing a tenth of the elements costs under a decibel.
:::
:::

::: explain
## Why does a phased array's directivity fall as it scans away from broadside?

::: model
An array's directivity depends on the size of its aperture as seen from the direction of the beam. When the beam is steered to an angle θ₀ from broadside, the array is viewed obliquely, and its projected width in that direction shrinks by cos θ₀. A smaller effective aperture means a wider beam and less directivity, in proportion to cos θ₀. On top of that, each element's own pattern usually falls away from broadside and the elements' coupling changes the impedance, so real arrays lose more than the projection alone predicts and are rarely scanned beyond about 60°.
:::
:::
