---
title: Oscillations lab
heading: Resonance sweeps, ringdowns, normal modes, large swings and isolation, measured in code
eyebrow: B1 · Lesson 47 · Oscillations lab
xp: 50
---

::: hook
An oscillator's quality factor can be measured two ways: sweep the drive frequency and read the width of the resonance, or strike it and count how fast the ringing dies. Real systems have several modes, large swings bend the period, and isolators must be checked against the vibration they face. In this lab you'll measure all of these with simulations and compare each measurement with the theory of lessons 40 to 46.
:::

::: play
- A 1 Hz oscillator is struck and left to ring. The solid curve is its motion and the dashed one the envelope, $e^{-\pi t/Q}$.
- Raise Q: the ringing lasts longer. Each cycle the amplitude falls by the factor $e^{-\pi/Q}$.
- The readouts give that factor and the number of cycles until the amplitude halves, $\frac{Q\ln 2}{\pi}$. Counting cycles of a ringdown is one of the quickest ways to estimate Q on the bench.
:::

```sim plot
view: graph
x: t = 0..10 s
y: x = -1..1
slider: Q = 2..100 step 1 = 20
plot: exp(-pi*t/Q)*cos(2*pi*t) | label: ringdown | samples: 800
plot: exp(-pi*t/Q) | dashed | label: envelope
readout: amplitude ratio per cycle = exp(-pi/Q) | digits: 4
readout: cycles to halve = Q*ln(2)/pi | digits: 3
```

::: predict
## A 440 Hz tuning fork's sound falls to half its amplitude in 2 s. Roughly what is its Q?

- [ ] About 4
- [ ] About 400
- [x] About 4000
- [ ] About 400 000

::: reveal
About 4000. In 2 s the fork rings 880 cycles, and halving takes $\frac{Q\ln 2}{\pi}$ cycles, so $Q = \frac{880\pi}{\ln 2} = 3990$. A steel fork loses only about a 635th of its energy per cycle, which is why it sings for so long; a quartz crystal does a hundred times better still.
:::
:::

::: nameit
## Q from a frequency sweep

For a mass m on spring k with damping c, driven by force F at ω, the amplitude is

$$X = \frac{F}{\sqrt{(k - m\omega^2)^2 + (c\omega)^2}}.$$

It peaks near $\omega_0 = \sqrt{k/m}$ at about QF/k, with $Q = \frac{m\omega_0}{c} = \frac{1}{2\zeta}$. The **half-power bandwidth** Δω, the width of the peak where X has fallen to $\frac{1}{\sqrt2}$ of its maximum, gives $Q \approx \frac{\omega_0}{\Delta\omega}$ for light damping.

## Q from a ringdown

Struck and left alone, successive peaks shrink by $e^{-\delta}$, where the **logarithmic decrement** is $\delta = \frac{2\pi\zeta}{\sqrt{1 - \zeta^2}} \approx \frac{\pi}{Q}$. Measuring over n cycles, $\delta = \frac1n\ln\frac{x_0}{x_n}$.

## Normal modes from a matrix

For coupled masses, $M\ddot{\mathbf x} = -K\mathbf x$. Trying $\mathbf x = \mathbf v\cos\omega t$ gives the eigenvalue problem $K\mathbf v = \omega^2M\mathbf v$: the eigenvalues are the squared mode frequencies, the eigenvectors the mode shapes.

## Measuring by simulation

A periodic steady state is reached once the starting transient has died away, after many multiples of $\frac{1}{\zeta\omega_0}$. Then the largest |x| over one drive period is the amplitude, read from samples finely spaced enough that the peak isn't missed: sampling a sine n times per cycle can read it low by up to $1 - \cos\frac{\pi}{n}$.
:::

::: example
## A 0.2 kg mass on a 2000 N/m spring has damping 0.4 N·s/m. Find its Q, its peak response and its half-power bandwidth.

::: solution
1. **Natural frequency and damping ratio.**
   $$\begin{aligned} \omega_0 &= \sqrt{2000/0.2} \\ &= 100\ \text{rad/s} \\ \zeta &= \frac{c}{2m\omega_0} \\ &= \frac{0.4}{40} \\ &= 0.01 \end{aligned}$$
   so Q = 50.
2. **Peak response.** The displacement peaks at
   $$\omega_0\sqrt{1 - 2\zeta^2} = 99.99\ \text{rad/s}$$
   with amplitude 50.0 times the static deflection $F/k$.
3. **Half-power bandwidth.** It's about
   $$\frac{100}{50} = 2\ \text{rad/s}$$
   from 99 to 101 rad/s.
4. **What it means for a sweep.** A sweep in steps coarser than that would miss most of the peak.

**Answer:** Q = 50; the response peaks at 99.99 rad/s at 50.0 times the static deflection, and the half-power bandwidth is about 2 rad/s.
:::
:::

::: example
## The same oscillator is struck. By how much does its amplitude fall per cycle, and after five cycles?

::: solution
1. **Logarithmic decrement.**
   $$\begin{aligned} \delta &= \frac{2\pi \times 0.01}{\sqrt{1 - 0.0001}} \\ &= 0.06283 \end{aligned}$$
2. **Per cycle.** Each peak is
   $$e^{-0.06283} = 0.9391$$
   times the last.
3. **After five cycles.**
   $$0.9391^5 = 0.730$$
4. **Working backward.** Reading the ratio of the first and sixth peaks from a scope and taking $\frac15\ln\frac{1}{0.730}$ recovers δ, and $Q = \frac{\pi}{\delta} = 50.0$. Averaging over several cycles makes the estimate far less sensitive to noise on any one peak.

**Answer:** each peak is 0.9391 times the one before, and after five cycles the amplitude is 0.730 of where it started.
:::
:::

::: example
## Four 1 kg masses sit in a line between two walls, joined to each other and to the walls by five 100 N/m springs. What are the mode frequencies?

::: solution
1. **Stiffness matrix.**
   $$K = 100\begin{pmatrix} 2 & -1 & 0 & 0 \\ -1 & 2 & -1 & 0 \\ 0 & -1 & 2 & -1 \\ 0 & 0 & -1 & 2 \end{pmatrix}$$
2. **Mode frequencies.** Its eigenvalues give
   $$\omega_j = 2\sqrt{k/m}\sin\frac{j\pi}{10}$$
   which is 6.18, 11.76, 16.18 and 19.02 rad/s.
3. **Mode shapes.** The lowest mode's shape is $(0.59, 0.95, 0.95, 0.59)$, all moving together like half a sine wave along the chain; mode $j$ fits $j$ half-waves.
4. **The ceiling.** The more often neighbors oppose, the more the springs stretch and the higher the frequency, and no mode can go faster than $2\sqrt{k/m}$ = 20 rad/s.

**Answer:** the modes are at 6.18, 11.76, 16.18 and 19.02 rad/s.
:::
:::

::: check
kind: python

## Step 1. Write `q_from_sweep(m, k, c)` that computes the amplitude per unit force on 200 001 frequencies from half to one and a half times $\sqrt{k/m}$, finds the peak, and returns `(peak frequency, Q)` with Q estimated as the peak frequency over the half-power bandwidth.

```python starter
import numpy as np
import math

def q_from_sweep(m, k, c):
    return math.sqrt(k / m), 1.0

print(q_from_sweep(0.2, 2000, 0.4))
```

```python solution
import numpy as np
import math

def q_from_sweep(m, k, c):
    w0 = math.sqrt(k / m)
    w = np.linspace(0.5 * w0, 1.5 * w0, 200001)
    X = 1 / np.sqrt((k - m * w**2)**2 + (c * w)**2)
    i = int(np.argmax(X))
    band = w[X >= X[i] / math.sqrt(2)]
    return float(w[i]), float(w[i] / (band[-1] - band[0]))

print(q_from_sweep(0.2, 2000, 0.4))
```

```python tests
wp, Q = q_from_sweep(0.2, 2000, 0.4)
assert abs(wp - 99.99) < 0.001, f"The peak sits at ω0√(1 - 2ζ²) = 99.99 rad/s; got {wp}."
assert abs(Q - 50) < 0.02, f"The bandwidth estimate should give Q close to 50; got {Q:.4f}."
wp2, Q2 = q_from_sweep(1, 100, 2)
assert abs(Q2 - 4.899) < 0.01, f"With Q = 5 the half-power estimate gives 4.90 (the light-damping formula is approximate); got {Q2:.4f}."
```

::: right
Right. At Q = 50 the bandwidth estimate is within 0.01%; at Q = 5 it reads 4.90, a reminder that $\omega_0/\Delta\omega$ is a light-damping formula.
:::
:::

::: check
kind: python

## Step 2. Write `ring_q(m, k, c)` that releases the oscillator from x = 1 at rest, steps it with RK4 (h = 10⁻⁴ s for 1 s), records each peak (where the velocity changes from positive to negative), and returns $Q = \frac{\pi}{\delta}$ with δ from the first and sixth recorded peaks.

```python starter
import numpy as np
import math

def ring_q(m, k, c):
    return math.sqrt(k * m) / c * 0.5

print(ring_q(0.2, 2000, 0.4))
```

```python solution
import numpy as np
import math

def ring_q(m, k, c):
    h = 1e-4
    y = np.array([1.0, 0.0])
    f = lambda s: np.array([s[1], (-k * s[0] - c * s[1]) / m])
    peaks = []
    for _ in range(10000):
        k1 = f(y); k2 = f(y + h / 2 * k1); k3 = f(y + h / 2 * k2); k4 = f(y + h * k3)
        yn = y + h / 6 * (k1 + 2 * k2 + 2 * k3 + k4)
        if y[1] > 0 and yn[1] <= 0:
            peaks.append(yn[0])
        y = yn
    delta = math.log(peaks[0] / peaks[5]) / 5
    return math.pi / delta

print(ring_q(0.2, 2000, 0.4))
```

```python tests
Q = ring_q(0.2, 2000, 0.4)
assert abs(Q - 50) < 0.05, f"The ringdown should give Q close to 50; got {Q:.4f}."
Q2 = ring_q(0.1, 4000, 0.4)
assert abs(Q2 - 50) < 0.05, f"m = 0.1 kg, k = 4000 N/m, c = 0.4 N·s/m also has Q = mω0/c = 50; got {Q2:.4f}."
```

::: right
Right. The ringdown and the sweep agree on Q = 50: the same damping seen in time and in frequency.
:::
:::

::: check
kind: python

## Step 3. Write `mode_frequencies(k, m, n)` for n equal masses m in a line between two walls, joined by n + 1 springs k. Build the stiffness matrix and return the angular frequencies in increasing order.

```python starter
import numpy as np

def mode_frequencies(k, m, n):
    return np.sqrt(k / m) * np.ones(n)

print(mode_frequencies(100, 1, 3))
```

```python solution
import numpy as np

def mode_frequencies(k, m, n):
    K = k * (2 * np.eye(n) - np.eye(n, k=1) - np.eye(n, k=-1))
    return np.sqrt(np.linalg.eigvalsh(K / m))

print(mode_frequencies(100, 1, 3))
```

```python tests
import numpy as np
w = mode_frequencies(100, 1, 3)
assert np.allclose(w, [10 * np.sqrt(2 - np.sqrt(2)), 10 * np.sqrt(2), 10 * np.sqrt(2 + np.sqrt(2))]), f"Expect 7.654, 14.142 and 18.478 rad/s; got {w}."
w5 = mode_frequencies(50, 2, 5)
exact = 2 * np.sqrt(50 / 2) * np.sin(np.arange(1, 6) * np.pi / 12)
assert np.allclose(w5, exact), f"For n masses the modes are 2√(k/m) sin(jπ/(2(n + 1))); got {w5}."
```

::: right
Right. The chain's frequencies follow $2\sqrt{k/m}\sin\frac{j\pi}{2(n + 1)}$, the same pattern a transmission line's LC ladder shows.
:::
:::

::: check
kind: python

## Step 4. Write `pendulum_period(theta0, L)` that releases a pendulum ($\ddot\theta = -\frac{9.8}{L}\sin\theta$) from rest at `theta0` radians, steps it with RK4 (h = 10⁻⁴ s) until θ first crosses zero, interpolates the crossing time, and returns four times it.

```python starter
import math

def pendulum_period(theta0, L):
    return 2 * math.pi * math.sqrt(L / 9.8)

print(pendulum_period(math.pi / 3, 1))
```

```python solution
import numpy as np
import math

def pendulum_period(theta0, L):
    h = 1e-4
    f = lambda s: np.array([s[1], -9.8 / L * math.sin(s[0])])
    y = np.array([theta0, 0.0])
    t = 0.0
    while True:
        k1 = f(y); k2 = f(y + h / 2 * k1); k3 = f(y + h / 2 * k2); k4 = f(y + h * k3)
        yn = y + h / 6 * (k1 + 2 * k2 + 2 * k3 + k4)
        if y[0] > 0 and yn[0] <= 0:
            return 4 * (t + h * y[0] / (y[0] - yn[0]))
        y = yn
        t += h

print(pendulum_period(math.pi / 3, 1))
```

```python tests
import math
T = pendulum_period(math.pi / 3, 1)
assert abs(T - 2.15397) < 1e-4, f"Released from 60°, a 1 m pendulum's period is 2.1540 s (lesson 41's elliptic integral); got {T:.5f}."
T2 = pendulum_period(0.05, 2)
assert abs(T2 / (2 * math.pi * math.sqrt(2 / 9.8)) - 1.000156) < 1e-4, "A small swing comes within 0.02% of 2π√(L/g)."
```

::: right
Right. The simulation reproduces lesson 41's 2.154 s, 7.3% longer than the small-swing period.
:::
:::

::: check
kind: python

## Step 5. Write `measure_T(r, z)` that simulates a mass on a spring and damper whose base moves as $\sin rt$ ($\omega_n$ = 1 rad/s, damping ratio z): $\ddot x = -2z(\dot x - r\cos rt) - (x - \sin rt)$. Use RK4 with 100 steps per drive period for 300 periods, starting at rest, and return the largest |x| over the last period.

```python starter
import numpy as np
import math

def measure_T(r, z):
    return 1.0

print(measure_T(5, 0.05))
```

```python solution
import numpy as np
import math

def measure_T(r, z):
    steps = 100
    dt = 2 * math.pi / r / steps
    f = lambda t, s: np.array([s[1], -2 * z * (s[1] - r * math.cos(r * t)) - (s[0] - math.sin(r * t))])
    x = np.array([0.0, 0.0])
    t = 0.0
    peak = 0.0
    N = 300 * steps
    for i in range(N):
        k1 = f(t, x); k2 = f(t + dt / 2, x + dt / 2 * k1)
        k3 = f(t + dt / 2, x + dt / 2 * k2); k4 = f(t + dt, x + dt * k3)
        x = x + dt / 6 * (k1 + 2 * k2 + 2 * k3 + k4)
        t += dt
        if i >= N - steps:
            peak = max(peak, abs(x[0]))
    return peak

print(measure_T(5, 0.05))
```

```python tests
import math
Tf = lambda r, z: math.sqrt((1 + (2 * z * r)**2) / ((1 - r * r)**2 + (2 * z * r)**2))
for r, z in ((5, 0.05), (0.5, 0.2), (2**0.5, 0.1)):
    got = measure_T(r, z)
    assert abs(got / Tf(r, z) - 1) < 0.003, f"At r = {r:.3f}, ζ = {z} the transmissibility is {Tf(r, z):.5f}; measured {got:.5f}."
```

::: right
Right. The measurements land on lesson 44's formula, including T = 1 at $r = \sqrt2$ whatever the damping.
:::
:::

::: check
kind: parts
given: f = 100 | 440 | 1000; t = 0.5 | 2 | 5

## A resonator ringing at {f} Hz takes {t} s for its amplitude to halve.

::: part
answer: pi*f*t/ln(2)
tolerance: 0.5%

### What is its Q?

::: misses
- [f*t/ln(2)] The decrement per cycle is $\pi/Q$: include the π.
- [any] Halving takes $\frac{Q\ln 2}{\pi}$ cycles.
:::
:::

::: part
answer: ln(2)/(pi*t)
unit: Hz
tolerance: 0.5%

### What is its half-power bandwidth in hertz?

::: misses
- [any] $\Delta f = \frac{f_0}{Q}$.
:::
:::
:::

::: check
given: Q = 10 | 50 | 200; n = 3 | 10
answer: exp(-pi*n/Q)
tolerance: 0.5%

## An oscillator with Q = {Q} is struck. What fraction of its starting amplitude remains after {n} cycles?

::: right
Right. $e^{-n\pi/Q}$.
:::

::: misses
- [exp(-n/Q)] Each cycle's decrement is $\frac{\pi}{Q}$.
- [any] The decrement per cycle is $\pi/Q$.
:::
:::

::: check
given: k = 50 | 200; m = 0.5 | 2
answer: sqrt(k/m)*sqrt(2 - sqrt(2))
unit: rad/s
tolerance: 0.5%

## Three {m} kg masses sit between two walls, joined by four {k} N/m springs. What is the lowest mode's angular frequency?

::: right
Right. $\sqrt{(2 - \sqrt2)k/m}$, with all three masses moving the same way.
:::

::: misses
- [sqrt(k/m)] That's one mass on one spring; the chain's lowest mode is slower.
- [any] The smallest eigenvalue of K/m is $(2 - \sqrt2)\frac km$.
:::
:::

::: explain
## Why do a frequency sweep and a ringdown measure the same Q?

::: model
Both are governed by the same damping. In a ringdown the oscillator loses a fraction $\frac{2\pi}{Q}$ of its energy each cycle, so the amplitude decays as $e^{-\pi t/(QT)}$. A decaying sinusoid can only be built from frequencies spread around $\omega_0$, and the faster the decay, the wider that spread, by roughly the reciprocal of the decay time. The driven response is the same oscillator answering one frequency at a time, and its resonance is wide by exactly that spread, $\frac{\omega_0}{Q}$. Lightly damped means a slow ringdown and a sharp peak; heavily damped means a quick ringdown and a broad one.
:::
:::

::: practice

::: check
given: m = 0.1 | 1; k = 100 | 1000; c = 0.05 | 0.5
answer: sqrt(k*m)/c
tolerance: 0.5%

## Q of {m} kg on {k} N/m with damping {c} N·s/m?

::: misses
- [any] $Q = \frac{m\omega_0}{c} = \frac{\sqrt{km}}{c}$.
:::
:::

::: check
given: r = 3 | 6; z = 0.02 | 0.2
answer: sqrt((1 + (2*z*r)^2)/((1 - r^2)^2 + (2*z*r)^2))
tolerance: 0.5%

## Transmissibility at r = {r}, ζ = {z}?

::: misses
- [any] The base-excitation formula of lesson 44.
:::
:::
:::
