---
title: Wave lab
heading: Modes, decibels, junctions and a passing siren, then a plucked string simulated
eyebrow: B8 · Lesson 5 · Wave lab
xp: 50
---

::: hook
The first four lessons gave formulas for waves on strings, sound in air and the Doppler shift. In this lab you'll turn them into small tools: the modes of strings and pipes, the sum of several noise sources in decibels, what a junction reflects, and the speed of a passing vehicle from two pitches heard as it goes by. Then you'll simulate a plucked string on a grid, without any formula for its modes, and hear the right fundamental come out of the arithmetic.
:::

::: play
- A siren moves past you at speed v. The two levels are the pitches heard as it approaches and as it recedes.
- Raise v: the gap between them grows. From the two pitches alone you can recover both the siren's true pitch and its speed, which is what step 4 does.
- The readouts give the speed and true pitch recovered from the two pitches shown.
:::

```sim plot
view: graph
x: v = 0..60 m/s
y: f = 500..900 Hz
slider: u = 0..60 step 1 = 30 m/s
plot: 700*343/(343 - v) | label: approaching | samples: 200
plot: 700*343/(343 + v) | label: receding | color: ch2 | samples: 200
point: u, 700*343/(343 - u)
point: u, 700*343/(343 + u)
readout: recovered speed, m per s = 343*(700*343/(343 - u) - 700*343/(343 + u))/(700*343/(343 - u) + 700*343/(343 + u)) | digits: 3
readout: recovered pitch, Hz = 2*(700*343/(343 - u))*(700*343/(343 + u))/(700*343/(343 - u) + 700*343/(343 + u)) | digits: 4
```

::: predict
## Ten identical machines each make 60 dB at a spot. What level do all ten make together?

- [ ] 600 dB
- [ ] 66 dB
- [x] 70 dB
- [ ] 60 dB

::: reveal
70 dB. Incoherent sources add their powers, so ten of them carry ten times the intensity of one, which is 10 dB more. Each doubling adds 3 dB: two machines make 63 dB, four 66 dB, and it takes ten to reach 70.
:::
:::

::: nameit
## Modes

A string or an open pipe of length L fits whole half-wavelengths: $f_n = \frac{nv}{2L}$. A pipe closed at one end has a node at one end and an antinode at the other, so it fits odd quarter-wavelengths: $f_n = \frac{(2n - 1)v}{4L}$, only the odd harmonics, an octave lower than an open pipe of the same length.

## Adding levels

Incoherent sources add their intensities, so levels combine as $L = 10\log_{10}\sum_i10^{L_i/10}$. Two equal sources make 3 dB more than one; a source 10 dB quieter than another adds only 0.4 dB.

## Junctions

A wave on a medium of impedance $Z_1$ meeting impedance $Z_2$ reflects with amplitude ratio $r = \frac{Z_1 - Z_2}{Z_1 + Z_2}$, and passes on a fraction $1 - r^2$ of its power.

## A passing source

Approaching at speed v, a source of pitch $f_0$ sounds as $f_a = f_0\frac{c}{c - v}$; receding, $f_r = f_0\frac{c}{c + v}$. Solving the pair gives

$$v = c\,\frac{f_a - f_r}{f_a + f_r}, \qquad f_0 = \frac{2f_af_r}{f_a + f_r}.$$

## Simulating a string

The wave equation $\frac{\partial^2y}{\partial t^2} = v^2\frac{\partial^2y}{\partial x^2}$ on a grid of spacing Δx with time step Δt becomes the **leapfrog** update

$$y_i^{n+1} = 2y_i^n - y_i^{n-1} + \left(\frac{v\Delta t}{\Delta x}\right)^2\left(y_{i+1}^n - 2y_i^n + y_{i-1}^n\right).$$

It's stable for $\frac{v\Delta t}{\Delta x} \le 1$, and at exactly 1 it moves pulses along the grid without any error at all.
:::

::: example
## An open organ pipe and a pipe closed at one end are both 0.5 m long. What are their first three resonances (sound at 343 m/s)?

::: solution
1. **Open pipe.** With $2L = 1$ m,
   $$f_n = \frac{n \times 343}{1}$$
   so 343, 686 and 1029 Hz.
2. **Pipe closed at one end.** With $4L = 2$ m,
   $$f_n = \frac{(2n - 1) \times 343}{2}$$
   so 171.5, 514.5 and 857.5 Hz.
3. **Compare.** The closed pipe sounds an octave lower and has only odd harmonics, which gives it a hollower tone.
4. **Instruments.** A clarinet behaves like a closed pipe and a flute like an open one, which is much of why they sound so different at the same pitch.

**Answer:** the open pipe resonates at 343, 686 and 1029 Hz, and the closed one at 171.5, 514.5 and 857.5 Hz.
:::
:::

::: example
## A siren's pitch is 767.1 Hz as it approaches and 643.7 Hz as it recedes. How fast is it moving, and what is its true pitch?

::: solution
1. **Speed.**
   $$\begin{aligned} v &= 343 \times \frac{767.1 - 643.7}{767.1 + 643.7} \\ &= 343 \times 0.0875 \\ &= 30.0\ \text{m/s} \end{aligned}$$
2. **True pitch.**
   $$\begin{aligned} f_0 &= \frac{2 \times 767.1 \times 643.7}{1410.8} \\ &= 700\ \text{Hz} \end{aligned}$$
3. **Why both pitches.** Neither pitch alone says anything about the speed without knowing the siren's true pitch, but the pair settles both.
4. **Radar.** The same idea, with a stationary radar instead of an ear, measures speeds from the frequency shift alone.

**Answer:** the siren moves at 30.0 m/s, and its true pitch is 700 Hz.
:::
:::

::: example
## A string 0.65 m long carries waves at 143 m/s. What is its fundamental, and how many grid steps does a simulation need per period with 130 cells and $v\Delta t = \Delta x$?

::: solution
1. **Fundamental.**
   $$\begin{aligned} f_1 &= \frac{143}{2 \times 0.65} \\ &= 110\ \text{Hz} \end{aligned}$$
   a guitar's A string.
2. **Grid spacing and time step.** With 130 cells and $v\Delta t = \Delta x$:
   $$\begin{aligned} \Delta x &= 5\ \text{mm} \\ \Delta t &= 35\ \mu\text{s} \end{aligned}$$
3. **Steps per period.** A period of 9.09 ms takes
   $$\frac{9.09\ \text{ms}}{35\ \mu\text{s}} = 260\ \text{steps}$$
   exactly twice the number of cells, since a pulse must cross the string and return.
4. **What the simulation knows.** It knows nothing about modes or harmonics, only the wave equation at each point, yet it rings at 110 Hz with all the harmonics that the pluck's shape contains.

**Answer:** the fundamental is 110 Hz, and the simulation takes 260 steps per period.
:::
:::

::: check
kind: python

## Step 1. Write `modes(v, L, n, closed_one_end=False)` returning a NumPy array of the first n resonant frequencies of a string or open pipe, or of a pipe closed at one end.

```python starter
import numpy as np

def modes(v, L, n, closed_one_end=False):
    return np.arange(1, n + 1) * v / L

print(modes(343, 0.5, 3), modes(343, 0.5, 3, True))
```

```python solution
import numpy as np

def modes(v, L, n, closed_one_end=False):
    k = np.arange(1, n + 1)
    if closed_one_end:
        return (2 * k - 1) * v / (4 * L)
    return k * v / (2 * L)

print(modes(343, 0.5, 3), modes(343, 0.5, 3, True))
```

```python tests
import numpy as np
assert np.allclose(modes(343, 0.5, 3), [343, 686, 1029]), f"An open 0.5 m pipe resonates at 343, 686 and 1029 Hz; got {modes(343, 0.5, 3)}."
assert np.allclose(modes(343, 0.5, 3, True), [171.5, 514.5, 857.5]), "Closed at one end: 171.5, 514.5, 857.5 Hz."
assert np.allclose(modes(143, 0.65, 2), [110, 220]), "A 0.65 m string at 143 m/s: 110 and 220 Hz."
```

::: right
Right. The closed pipe fits quarter-wavelengths, so it starts an octave lower and skips every even harmonic.
:::
:::

::: check
kind: python

## Step 2. Write `add_db(levels)` that combines a list of incoherent sound levels in dB into one level.

```python starter
def add_db(levels):
    return sum(levels)

print(add_db([80, 80]))
```

```python solution
import math

def add_db(levels):
    return 10 * math.log10(sum(10**(L / 10) for L in levels))

print(add_db([80, 80]))
```

```python tests
assert abs(add_db([80, 80]) - 83.0103) < 1e-3, f"Two equal 80 dB sources make 83.0 dB; got {add_db([80, 80]):.4f}."
assert abs(add_db([80, 70]) - 80.4139) < 1e-3, "A source 10 dB quieter adds only 0.41 dB."
assert abs(add_db([60] * 10) - 70) < 1e-9, "Ten equal sources add 10 dB."
```

::: right
Right. Convert to intensities, add, and convert back: decibels themselves never add.
:::
:::

::: check
kind: python

## Step 3. Write `junction(Z1, Z2)` returning the reflected amplitude ratio and the fraction of power transmitted, for a wave going from impedance Z1 to Z2.

```python starter
def junction(Z1, Z2):
    return 0.0, 1.0

print(junction(1, 4))
```

```python solution
def junction(Z1, Z2):
    r = (Z1 - Z2) / (Z1 + Z2)
    return r, 1 - r * r

print(junction(1, 4))
```

```python tests
r, t = junction(1, 4)
assert abs(r + 0.6) < 1e-12 and abs(t - 0.64) < 1e-12, f"Into four times the impedance: r = -0.6, 64% passes; got {r} and {t}."
r2, t2 = junction(4, 1)
assert abs(r2 - 0.6) < 1e-12 and abs(t2 - 0.64) < 1e-12, "The reverse junction reflects the same power, with the opposite sign."
assert junction(2, 2) == (0.0, 1.0), "Matched impedances reflect nothing."
```

::: right
Right. A mismatch reflects the same power whichever way the wave crosses it; only the sign of the reflection changes.
:::
:::

::: check
kind: python

## Step 4. Write `doppler_pass(fa, fr, c=343)` that returns `(v, f0)`, the speed and true pitch of a source heard at fa approaching and fr receding.

```python starter
def doppler_pass(fa, fr, c=343):
    return 0.0, (fa + fr) / 2

print(doppler_pass(767.0927, 643.6997))
```

```python solution
def doppler_pass(fa, fr, c=343):
    v = c * (fa - fr) / (fa + fr)
    f0 = 2 * fa * fr / (fa + fr)
    return v, f0

print(doppler_pass(767.0927, 643.6997))
```

```python tests
v, f0 = doppler_pass(700 * 343 / 313, 700 * 343 / 373)
assert abs(v - 30) < 1e-9 and abs(f0 - 700) < 1e-9, f"Expect 30 m/s and 700 Hz; got {v} and {f0}."
v2, f2 = doppler_pass(440, 440)
assert v2 == 0 and abs(f2 - 440) < 1e-12, "Equal pitches mean a stationary source."
```

::: right
Right. The true pitch is the harmonic mean of the two, not their average: the average would be 705.4 Hz here, off by 5 Hz.
:::
:::

::: check
kind: python

## Step 5. Write `pluck_freq(L, v, N, T)` that simulates a string of length L and wave speed v on N cells with $v\Delta t = \Delta x$, plucked into a triangle 1 cm high peaking at 30% of its length and released from rest, runs for time T, and returns the frequency measured from the downward zero crossings at the middle cell. For the first step from rest, use $y^1 = y^0 + \frac12(y^0_{i+1} - 2y^0_i + y^0_{i-1})$.

```python starter
import numpy as np

def pluck_freq(L, v, N, T):
    return v / L

print(pluck_freq(0.65, 143, 130, 0.2))
```

```python solution
import numpy as np

def pluck_freq(L, v, N, T):
    dx = L / N
    dt = dx / v
    x = np.linspace(0, L, N + 1)
    y0 = np.where(x < 0.3 * L, x / (0.3 * L), (L - x) / (0.7 * L)) * 0.01
    y1 = y0.copy()
    y1[1:-1] = y0[1:-1] + 0.5 * (y0[2:] - 2 * y0[1:-1] + y0[:-2])
    yprev, y = y0, y1
    mid = N // 2
    prev, cross = None, []
    for s in range(int(T / dt)):
        ynew = np.zeros_like(y)
        ynew[1:-1] = 2 * y[1:-1] - yprev[1:-1] + (y[2:] - 2 * y[1:-1] + y[:-2])
        yprev, y = y, ynew
        val = y[mid]
        if prev is not None and prev > 0 and val <= 0:
            cross.append((s + 1) * dt - dt * val / (val - prev))
        prev = val
    return (len(cross) - 1) / (cross[-1] - cross[0])

print(pluck_freq(0.65, 143, 130, 0.2))
```

```python tests
f = pluck_freq(0.65, 143, 130, 0.2)
assert abs(f - 110) < 0.5, f"The simulated string should ring at v/2L = 110 Hz; got {f:.3f}."
f2 = pluck_freq(0.65, 286, 130, 0.1)
assert abs(f2 / f - 2) < 0.01, "Doubling the wave speed (four times the tension) doubles the pitch."
```

::: right
Right. With $v\Delta t = \Delta x$ the leapfrog scheme is exact on the grid, so the measured pitch lands on 110 Hz, though the program never mentions modes.
:::
:::

::: check
kind: parts
given: L = 50 | 70 | 90; n = 2 | 4 | 8

## {n} identical sources each make {L} dB at a spot.

::: part
answer: L + 10*log(n)
unit: dB
tolerance: 0.5%

### What level do they make together?

::: misses
- [L + 20*log(n)] Incoherent sources add powers: use 10 log.
- [any] $L + 10\log_{10}n$.
:::
:::

::: part
answer: 10*log(10^(L/10) + 10^((L - 10)/10))
unit: dB
tolerance: 0.5%

### What do one source of {L} dB and one 10 dB quieter make together?

::: misses
- [L + 10] Levels don't add; intensities do.
- [any] $10\log_{10}\left(10^{L_1/10} + 10^{L_2/10}\right)$.
:::
:::
:::

::: check
given: fa = 520 | 800; r = 0.85 | 0.92
answer: 343*(fa - fa*r)/(fa + fa*r)
unit: m/s
tolerance: 0.5%

## A passing horn sounds {fa} Hz as it approaches and {r} times that as it recedes. How fast is it moving?

::: right
Right. $v = c\frac{f_a - f_r}{f_a + f_r}$, independent of the horn's true pitch.
:::

::: misses
- [343*(fa - fa*r)/fa] Divide by the sum of the two pitches.
- [any] $v = c\frac{f_a - f_r}{f_a + f_r}$.
:::
:::

::: explain
## Why does the leapfrog simulation of a string give exactly the right pitch when $v\Delta t = \Delta x$, but not with a smaller time step?

::: model
The wave equation's solutions are shapes sliding left and right at speed v. When $v\Delta t = \Delta x$, each time step moves a shape exactly one grid cell, and the leapfrog update reduces to copying values from neighboring cells, so a pulse travels along the grid with no distortion and the round trip takes exactly the right number of steps. With a smaller time step, the shape has to move a fraction of a cell per step, and the grid can only approximate that: short wavelengths travel slightly too slowly (numerical dispersion), so the harmonics drift out of tune, though the fundamental, being long and smooth, stays close to correct.
:::
:::

::: practice

::: check
given: v = 250 | 343; L = 0.3 | 1.2
answer: v/(4*L)
unit: Hz
tolerance: 0.5%

## Lowest resonance of a {L} m pipe closed at one end, sound at {v} m/s?

::: misses
- [any] $\frac{v}{4L}$.
:::
:::

::: check
given: Z1 = 1 | 3; Z2 = 2 | 9
answer: 1 - ((Z1 - Z2)/(Z1 + Z2))^2
tolerance: 0.5%

## Fraction of power passing from impedance {Z1} to {Z2}?

::: misses
- [any] $1 - r^2$ with $r = \frac{Z_1 - Z_2}{Z_1 + Z_2}$.
:::
:::
:::
