---
title: Spectral analysis lab
heading: Window figures of merit, scalloping by direct evaluation, and Levinson–Durbin on a known AR process, in Python
eyebrow: S7 · Lesson 35 · Spectral analysis lab
xp: 60
---

::: hook
Lessons 29 to 34 quoted tables: a Hann window's coherent gain of 0.5 and noise bandwidth of 1.5 bins, scalloping losses of 3.92 and 1.42 dB, and the claim that Levinson–Durbin recovers an AR model's coefficients from its autocorrelation. This lab computes each from its definition: the window figures from the samples themselves, the scalloping loss by evaluating the window's transform half a bin off, and an AR(2) model recovered exactly from the autocorrelation the Yule–Walker equations predict.
:::

::: play
- The curve is the peak response of an N-point DFT to a tone d bins away from the nearest bin, for a rectangular window: $\left|\frac{\sin\pi d}{N\sin(\pi d/N)}\right|$ in dB, with N = 64.
- It is 0 dB on a bin and drops to −3.92 dB halfway between bins (the right edge): the scalloping loss. Past halfway the tone is nearer the next bin, so the picture repeats.
- A Hann window's wider main lobe flattens this to −1.42 dB, at the cost of 1.76 dB more noise bandwidth.
:::

```sim plot
view: graph
x: d = 0..0.5
y: L = -5..0 dB
plot: 20*log(abs(sin(pi*d + 1e-9)/(64*sin((pi*d + 1e-9)/64)))) | label: rectangular window
plot: 20*log(abs(sin(pi*d + 1e-9)/((pi*d + 1e-9)*(1 - d^2)))) | color: ch2 | label: Hann window
marker: y = -3.92 | label: −3.92 dB
marker: y = -1.42 | label: −1.42 dB
```

::: predict
## An AR(2) process has a₁ = 0.75, a₂ = −0.5 and unit driving noise. Its autocorrelation is computed exactly and fed to Levinson–Durbin with order 2. What comes out?

- [ ] Coefficients near, but not equal to, 0.75 and −0.5
- [x] Exactly 0.75 and −0.5, with a prediction error of exactly 1
- [ ] The reflection coefficients, which differ from the a's and can't be converted
- [ ] An unstable model

::: reveal
With exact autocorrelation (φ[0] = 1.778, φ[1] = 0.889, φ[2] = −0.222), the Yule–Walker equations are exactly the ones the process satisfies, so Levinson–Durbin returns a = [0.75, −0.5] and $E_2 = \sigma_w^2 = 1$. Estimated autocorrelations from finite data give estimates scattered around these.
:::
:::

::: nameit
## What the lab computes

- **Windows** (periodic forms): rectangular; Hann $0.5 - 0.5\cos(2\pi n/N)$; Hamming $0.54 - 0.46\cos(2\pi n/N)$.
- **Coherent gain** $\frac1N\sum w$ and **ENBW** $N\frac{\sum w^2}{(\sum w)^2}$ bins.
- **Scalloping loss**: $20\log_{10}\frac{|W(\pi/N)|}{|W(0)|}$, the window's transform half a bin from its peak, by direct summation.
- **Levinson–Durbin**: from φ[0..p], the reflection coefficients and predictor coefficients order by order, with $E_i = E_{i-1}(1 - k_i^2)$ (lesson 34).
:::

::: check
kind: python

## Write `window(name, N)` for 'rect', 'hann' and 'hamming' (periodic forms), and `cg(w)` and `enbw(w)`.

```python starter
def window(name, N):
    return [1.0] * N

def cg(w):
    return 1.0

def enbw(w):
    return 1.0
```

```python solution
import math

def window(name, N):
    if name == 'rect':
        return [1.0] * N
    a = 0.5 if name == 'hann' else 0.54
    return [a - (1 - a) * math.cos(2 * math.pi * n / N) for n in range(N)]

def cg(w):
    return sum(w) / len(w)

def enbw(w):
    return len(w) * sum(v * v for v in w) / sum(w) ** 2
```

```python tests
h = window('hann', 64)
assert abs(cg(h) - 0.5) < 1e-12 and abs(enbw(h) - 1.5) < 1e-12, "Hann: coherent gain 0.5, ENBW 1.5 bins exactly."
m = window('hamming', 64)
assert abs(cg(m) - 0.54) < 1e-12 and abs(enbw(m) - 1.363) < 1e-3, "Hamming: 0.54 and 1.36 bins."
assert enbw(window('rect', 64)) == 1.0, "Rectangle: 1 bin."
```

::: right
Right. Periodic windows give these round numbers exactly; symmetric ones differ slightly for small N.
:::
:::

::: check
kind: python

## Write `scalloping_db(w)`: the window's response half a bin off, relative to its peak, in dB, by direct evaluation of its DTFT.

```python starter
def scalloping_db(w):
    return 0.0
```

```python solution
import cmath, math

def scalloping_db(w):
    N = len(w)
    W = lambda f: abs(sum(w[n] * cmath.exp(-2j * math.pi * f * n / N) for n in range(N)))
    return 20 * math.log10(W(0.5) / W(0))
```

```python tests
import math
rect = [1.0] * 64
hann = [0.5 - 0.5 * math.cos(2 * math.pi * n / 64) for n in range(64)]
assert abs(scalloping_db(rect) + 3.92) < 0.01, "Rectangular: −3.92 dB."
assert abs(scalloping_db(hann) + 1.42) < 0.01, "Hann: −1.42 dB."
```

::: right
Right. A wider main lobe flattens the peak between bins; flat-top windows push it under 0.01 dB.
:::
:::

::: check
kind: python

## Write `levinson(phi, p)`: return (a, E) with a = [a₁, …, a_p] the predictor coefficients and E the final prediction error.

```python starter
def levinson(phi, p):
    return ([0.0] * p, phi[0])
```

```python solution
def levinson(phi, p):
    a, E = [], phi[0]
    for i in range(1, p + 1):
        k = (phi[i] - sum(a[j] * phi[i - 1 - j] for j in range(len(a)))) / E
        a = [a[j] - k * a[i - 2 - j] for j in range(len(a))] + [k]
        E *= 1 - k * k
    return (a, E)
```

```python tests
p0 = 1 / 0.5625
a, E = levinson([p0, 0.5 * p0, -0.125 * p0], 2)
assert abs(a[0] - 0.75) < 1e-12 and abs(a[1] + 0.5) < 1e-12 and abs(E - 1) < 1e-12, "The AR(2) model comes back exactly."
a2, E2 = levinson([1, 0.8, 0.5], 2)
assert abs(a2[0] - 1.1111) < 1e-4 and abs(a2[1] + 0.3889) < 1e-4 and abs(E2 - 0.3056) < 1e-4, "Lesson 34's example: 1.111, −0.389, 0.306."
```

::: right
Right. The recursion's reflection coefficients are those of the lattice in lesson 17.
:::
:::

::: explain
## Why does a window with lower scalloping loss usually have a larger noise bandwidth?

::: model
A window that keeps a tone's reading nearly constant when it falls between bins must have a broad, flat main lobe, so that half a bin away its response has hardly fallen. A broad main lobe in frequency means the window collects energy from a wider band around each bin, which for white noise means more noise power per bin: a larger equivalent noise bandwidth. Tapering the window also reduces its sum relative to its energy, which is exactly what the noise-bandwidth formula measures. So measuring tone amplitudes accurately and detecting weak tones in noise pull toward different windows.
:::
:::
