---
title: "Radiation and localization lab: pistons, arrays, delays and positions"
heading: A baffled piston's exact on-axis pressure with its near-field maxima, the sidelobes of a steered line array found by scanning, a fractional delay recovered by cross-correlation and parabolic interpolation, and a source located from time differences by Gauss–Newton
eyebrow: S16 · Lesson 20 · Radiation and localization lab
xp: 60
---

::: hook
Lessons 15 to 19 gave radiation and reception in their far-field, ideal forms. Real measurements happen near sources, arrays have sidelobes that matter, delays fall between samples, and a position comes from several noisy delays at once. In this lab you'll compute a piston's pressure right down to its face, an array's worst sidelobe, a sub-sample delay between two recorded signals, and a source's position from four microphones.
:::

::: play
- The curve is the on-axis pressure amplitude of a baffled piston of radius a, relative to $2\rho cU$, against distance in units of $a^2/\lambda$, for the ka on the slider.
- Close in, the pressure oscillates between zero and its maximum as contributions from the center and edge interfere; the last maximum lies near $a^2/\lambda$, and beyond it the pressure falls as 1/r.
- Measurements meant to represent the far field must be taken well beyond that last maximum.
:::

```sim plot
view: graph
x: x = 0.05..10 log
y: p = 0..1.1
slider: k = 3..30 = 10
plot: abs(sin(k/2*(sqrt((x*k/(2*pi))^2 + 1) - x*k/(2*pi)))) | samples: 800 | label: on-axis pressure, relative to 2ρcU
```

::: predict
## A 10 cm-radius piston radiates 3430 Hz (λ = 10 cm). Where along the axis is the last pressure maximum?

- [ ] At the piston's face
- [x] About 7.5 cm out
- [ ] About 1 m out
- [ ] There's no maximum on the axis

::: reveal
On axis the pressure is $2\rho cU|\sin[\frac k2(\sqrt{r^2 + a^2} - r)]|$, largest where the path difference to the rim is half a wavelength: $r = (a^2 - (\lambda/2)^2)/\lambda$ = (0.01 − 0.0025)/0.1 = 0.075 m, close to $a^2/\lambda$ = 0.1 m. Task 1 confirms it and checks the far-field limit.
:::
:::

::: nameit
## What the lab computes

- **Piston on axis**: $|p| = 2\rho cU\left|\sin\left[\frac k2\left(\sqrt{r^2 + a^2} - r\right)\right]\right|$, exact at every distance; far away it tends to $\rho cUka^2/2r$.
- **Arrays**: the normalized array factor of N elements with spacing d steered to $\theta_s$, $|\sin(Nu/2)/(N\sin(u/2))|$ with $u = kd(\sin\theta - \sin\theta_s)$; the highest sidelobe is found by scanning angles outside the main lobe ($|u| \ge 2\pi/N$) and outside any grating lobes.
- **Delays**: the integer lag that maximizes the cross-correlation $\sum_ix_iy_{i+l}$, refined by a parabola through the peak and its two neighbors.
- **Positions**: given microphones at known positions and the measured delays of each relative to the first, find (x, y) minimizing the squared mismatch between predicted and measured range differences by Gauss–Newton iterations from a starting guess.
:::

::: check
kind: python

## Write `piston_axis(a, f, U, r)`, the exact on-axis pressure amplitude in air (ρ = 1.21 kg/m³, c = 343 m/s).

```python starter
import math

def piston_axis(a, f, U, r):
    k = 2 * math.pi * f / 343
    return 1.21 * 343 * U * k * a * a / (2 * r)
```

```python solution
import math

def piston_axis(a, f, U, r):
    k = 2 * math.pi * f / 343
    return 2 * 1.21 * 343 * U * abs(math.sin(k / 2 * (math.sqrt(r * r + a * a) - r)))
```

```python tests
import math
a, f, U = 0.1, 3430, 0.01
k = 2 * math.pi * f / 343
assert abs(piston_axis(a, f, U, 50) / (1.21 * 343 * U * k * a * a / 100) - 1) < 1e-4, "Far away it matches ρcUka²/2r."
lam = 343 / f
rmax = (a * a - (lam / 2) ** 2) / lam
assert abs(piston_axis(a, f, U, rmax) - 2 * 1.21 * 343 * U) < 1e-9, "The last maximum, 2ρcU = 8.30 Pa, is at 7.5 cm."
assert piston_axis(a, f, U, (a * a - lam ** 2) / (2 * lam)) < 1e-9, "Closer in there's a null on the axis."
```

::: right
Right. Near the face the on-axis pressure is no guide to the far-field level, which is why loudspeakers are measured meters away or corrected.
:::
:::

::: check
kind: python

## Write `array_factor(N, kd, st, th)` (angles in radians) and `max_sidelobe(N, kd, st=0)` in dB, scanning 20,000 angles.

```python starter
def array_factor(N, kd, st, th):
    return 1.0

def max_sidelobe(N, kd, st=0.0):
    return -13.26
```

```python solution
import math

def array_factor(N, kd, st, th):
    u = kd * (math.sin(th) - math.sin(st))
    if abs(math.sin(u / 2)) < 1e-12:
        return 1.0
    return abs(math.sin(N * u / 2) / (N * math.sin(u / 2)))

def max_sidelobe(N, kd, st=0.0):
    best = 0.0
    for i in range(1, 20001):
        th = -math.pi / 2 + math.pi * i / 20001
        u = kd * (math.sin(th) - math.sin(st))
        if abs(u / (2 * math.pi) - round(u / (2 * math.pi))) < 1 / N:
            continue
        best = max(best, array_factor(N, kd, st, th))
    return 20 * math.log10(best)
```

```python tests
import math
assert abs(max_sidelobe(64, math.pi) + 13.25) < 0.02, "A long uniform array: first sidelobes at −13.3 dB."
assert abs(max_sidelobe(16, math.pi) + 13.15) < 0.02, "Sixteen elements: −13.1 dB."
assert abs(array_factor(8, math.pi, math.radians(30), math.radians(30)) - 1) < 1e-12, "Steered to 30°, the main lobe points there."
```

::: right
Right. Uniform weighting fixes the sidelobes near −13 dB however many elements there are; tapering the weights is the only way lower.
:::
:::

::: check
kind: python

## Write `xcorr_delay(x, y, maxlag=20)`: the delay of y relative to x in samples, with parabolic interpolation.

```python starter
def xcorr_delay(x, y, maxlag=20):
    return 0.0
```

```python solution
def xcorr_delay(x, y, maxlag=20):
    def cc(l):
        return sum(x[i] * y[i + l] for i in range(max(0, -l), min(len(x), len(y) - l)))
    vals = {l: cc(l) for l in range(-maxlag, maxlag + 1)}
    k = max(vals, key=vals.get)
    a, b, c = vals[k - 1], vals[k], vals[k + 1]
    return k + (a - c) / (2 * (a - 2 * b + c))
```

```python tests
import math, random
r = random.Random(3)
base = [r.gauss(0, 1) for _ in range(700)]
bl = [sum(base[i:i + 3]) / 3 for i in range(len(base) - 3)]
sinc = lambda v: 1.0 if abs(v) < 1e-12 else math.sin(math.pi * v) / (math.pi * v)
def delayed(d):
    return [sum(bl[32 + i - j] * sinc(j - d) * (0.5 + 0.5 * math.cos(math.pi * (j - d) / 17)) for j in range(-16, 17)) for i in range(500)]
est = xcorr_delay(delayed(0), delayed(3.4))
assert abs(est - 3.4) < 0.1, "A 3.4-sample delay is recovered to within a tenth of a sample."
assert abs(xcorr_delay(delayed(0), delayed(-5)) + 5) < 0.05, "Negative delays too."
```

::: right
Right. The parabola recovers most of a fraction of a sample; the remaining bias comes from the correlation peak's shape not being exactly parabolic.
:::
:::

::: check
kind: python

## Write `locate(mics, tdoa, c=343.0, guess=(0.0, 0.0))`: the 2D source position from delays of microphones 2, 3, … relative to microphone 1, by Gauss–Newton.

```python starter
def locate(mics, tdoa, c=343.0, guess=(0.0, 0.0)):
    return guess
```

```python solution
import math

def locate(mics, tdoa, c=343.0, guess=(0.0, 0.0)):
    x, y = guess
    for _ in range(100):
        J, res = [], []
        d0 = math.hypot(x - mics[0][0], y - mics[0][1])
        for (mx, my), t in zip(mics[1:], tdoa):
            di = math.hypot(x - mx, y - my)
            res.append(di - d0 - c * t)
            J.append([(x - mx) / di - (x - mics[0][0]) / d0, (y - my) / di - (y - mics[0][1]) / d0])
        a = sum(j[0] * j[0] for j in J)
        b = sum(j[0] * j[1] for j in J)
        d = sum(j[1] * j[1] for j in J)
        g0 = sum(j[0] * r for j, r in zip(J, res))
        g1 = sum(j[1] * r for j, r in zip(J, res))
        det = a * d - b * b
        dx, dy = -(d * g0 - b * g1) / det, -(-b * g0 + a * g1) / det
        x, y = x + dx, y + dy
        if abs(dx) + abs(dy) < 1e-12:
            break
    return x, y
```

```python tests
import math
mics = [(0, 0), (100, 0), (0, 100), (100, 100)]
src = (30, 170)
d = [math.hypot(src[0] - m[0], src[1] - m[1]) for m in mics]
td = [(di - d[0]) / 343 for di in d[1:]]
x, y = locate(mics, td, guess=(50, 50))
assert abs(x - 30) < 1e-6 and abs(y - 170) < 1e-6, "Four microphones locate the source exactly from exact delays."
x2, y2 = locate(mics, [t * 343 / 349 for t in td], c=343, guess=(50, 50))
assert math.hypot(x2 - 30, y2 - 170) > 1, "Delays measured in warmer air (349 m/s) misplace it."
```

::: right
Right. Three delays overdetermine a 2D position, so least squares averages out small errors; a wrong speed of sound biases every one of them alike.
:::
:::

::: explain
## Why does a piston's on-axis pressure oscillate near its face but fall smoothly as 1/r far away?

::: model
Every part of the piston sends a wavelet to a point on the axis, and their path lengths range from r for the center to $\sqrt{r^2 + a^2}$ for the rim. Close to the face that range spans several wavelengths, so as the point moves the contributions add and cancel in turn, giving maxima and nulls. Far away the range shrinks below half a wavelength, all the contributions arrive nearly in phase, and the piston acts as a single source whose pressure falls as 1/r. The changeover, near $a^2/\lambda$, marks where the far-field formulas, directivity patterns included, begin to apply.
:::
:::
