---
title: Wavelets and multiresolution
heading: The discrete wavelet transform as an iterated filter bank, Haar and Daubechies filters, and wavelet denoising
eyebrow: S7 · Lesson 43 · Wavelets and multiresolution
xp: 50
---

::: hook
The STFT (lesson 30) analyzes every frequency with the same window, so it must choose between seeing quick events and resolving close tones. Many signals want both at once: a transient's sharp edge needs fine time resolution at high frequencies, while a slow drift needs fine frequency resolution at low ones. The discrete wavelet transform gets this by splitting a signal in two with a perfect-reconstruction filter bank (lesson 42), then splitting the low half again, and again. The result is a set of octave bands, each sampled just fast enough, with as many coefficients as the signal had samples and an O(N) cost.
:::

::: play
- The curves are the lowpass responses of the two simplest orthogonal wavelets: Haar, $\sqrt2|\cos(\omega/2)|$, and Daubechies-4, $\sqrt{2\cos^4(\omega/2)(1 + 2\sin^2(\omega/2))}$.
- Both pass DC with gain √2 and reject ω = π completely; D4's extra zero at π makes it flatter at both ends.
- Each level of the transform reuses the same filters on the previous level's lowpass output, halving the band each time.
:::

```sim plot
view: graph
x: w = 0..1
y: H = 0..1.5
plot: sqrt(2)*abs(cos(pi*w/2)) | label: Haar
plot: sqrt(2*cos(pi*w/2)^4*(1 + 2*sin(pi*w/2)^2)) | label: Daubechies-4
```

::: predict
## A signal of 4096 samples at 8 kHz goes through a three-level wavelet transform. Which band does the third level's detail cover, and how many coefficients does it have?

- [ ] 0 to 1 kHz, 4096 coefficients
- [x] 0.5 to 1 kHz, 512 coefficients
- [ ] 2 to 4 kHz, 2048 coefficients
- [ ] 1 to 2 kHz, 1024 coefficients

::: reveal
Level 1 splits 0–4 kHz into a 2–4 kHz detail (2048 coefficients) and a 0–2 kHz approximation; level 2 splits that into 1–2 kHz (1024) and 0–1 kHz; level 3 into 0.5–1 kHz (512) and a 0–0.5 kHz approximation (512). The counts add back to 4096: the transform is critically sampled.
:::
:::

::: nameit
## The iterated filter bank

An orthogonal two-channel bank uses a lowpass $h_0$ of even length L and the highpass $h_1[n] = (-1)^nh_0[L - 1 - n]$, each followed by decimation by 2; synthesis uses the time-reversed filters and reconstructs perfectly. The **discrete wavelet transform** applies it J times to the lowpass output:
- level j's **detail** covers $f_s/2^{j+1}$ to $f_s/2^j$ and has $N/2^j$ coefficients; the final **approximation** has $N/2^J$;
- the bands are octaves, constant-Q: short windows at high frequencies, long ones at low;
- the total cost is about 2LN multiplications, since each level handles half the samples of the one before.

## Haar and Daubechies

- **Haar**: $h_0 = [1, 1]/\sqrt2$. Each level's approximation is the scaled sum of pairs, the detail the scaled difference. Simple, but its blocky basis spreads smooth signals into many details.
- **Daubechies-4**: $h_0 = \frac{1}{4\sqrt2}[1 + \sqrt3, 3 + \sqrt3, 3 - \sqrt3, 1 - \sqrt3] = [0.483, 0.837, 0.224, -0.129]$, with two **vanishing moments**: its details are zero for any linear trend, so smooth regions produce almost no detail coefficients.
- For orthonormal filters $\sum h_0 = \sqrt2$ and $\sum h_0^2 = 1$, so the transform preserves energy.

## Denoising by thresholding

A smooth signal's energy concentrates in a few large coefficients while white noise spreads evenly over all of them. Setting detail coefficients below a threshold to zero (hard) or shrinking all of them toward zero by it (soft), then inverting, removes most of the noise and keeps edges sharp. Donoho's **universal threshold** is $\sigma\sqrt{2\ln N}$ for noise of standard deviation σ in N coefficients.
:::

::: example
## Take two levels of the Haar transform of [4, 6, 10, 12] and check the energy.

::: solution
1. **Level 1 approximations.**
   $$\begin{aligned} (4 + 6)/\sqrt2 &= 7.07 \\ (10 + 12)/\sqrt2 &= 15.56 \end{aligned}$$
2. **Level 1 details.**
   $$\begin{aligned} (4 - 6)/\sqrt2 &= -1.41 \\ (10 - 12)/\sqrt2 &= -1.41 \end{aligned}$$
3. **Level 2.** Transform the two approximations again:
   $$\begin{aligned} (7.07 + 15.56)/\sqrt2 &= 16 \\ (7.07 - 15.56)/\sqrt2 &= -6 \end{aligned}$$
4. **Energy.**
   $$\begin{aligned} &16^2 + 6^2 + 1.41^2 + 1.41^2 \\ &= 296 \\ &= 4^2 + 6^2 + 10^2 + 12^2 \end{aligned}$$
   Most of it sits in one coefficient.

**Answer:** the transform is [16, −6, −1.41, −1.41], and its energy, 296, matches the signal's.
:::
:::

::: example
## Check the Daubechies-4 lowpass taps and find the first highpass tap.

::: solution
1. **Sum of the taps.**
   $$\begin{aligned} h_0 = [&0.483,\ 0.837, \\ &0.224,\ -0.129] \end{aligned}$$
   sums to 1.414 = √2.
2. **Sum of the squares.** The squares sum to 1, as orthonormality requires.
3. **Highpass.**
   $$\begin{aligned} h_1[n] &= (-1)^nh_0[3 - n] \\ &= [-0.129,\ -0.224, \\ &\qquad 0.837,\ -0.483] \end{aligned}$$
4. **Check.** Its entries sum to zero: no response at DC.

**Answer:** the lowpass taps sum to √2 with squares summing to 1, and the first highpass tap is −0.129.
:::
:::

::: example
## A 4096-sample record has white noise of standard deviation 0.1. What universal threshold does wavelet denoising use?

::: solution
1. **Universal threshold.**
   $$\begin{aligned} T &= \sigma\sqrt{2\ln N} \\ &= 0.1\sqrt{2\ln4096} \\ &= 0.1 \times 4.08 \\ &= 0.41 \end{aligned}$$
2. **What it does.** Pure noise coefficients almost never exceed it, so after thresholding the noise is nearly gone.
3. **What survives.** Signal coefficients larger than 0.41 survive, keeping edges that a linear lowpass filter would blur.

**Answer:** a threshold of 0.41.
:::
:::

::: check
kind: parts
given: a = 3 | 8; b = 1 | 5

## The Haar transform's first level acts on the pair [{a}, {b}].

::: part
answer: (a + b)/sqrt(2)
tolerance: 0.5%

### What is the approximation coefficient?

::: misses
- [(a + b)/2] The orthonormal Haar filter scales by $1/\sqrt2$.
- [any] $(x_0 + x_1)/\sqrt2$.
:::
:::

::: part
answer: (a - b)/sqrt(2)
tolerance: 0.5%

### What is the detail coefficient?

::: misses
- [any] $(x_0 - x_1)/\sqrt2$.
:::
:::
:::

::: check
kind: parts
given: s = 8 | 16 | 48; j = 1 | 2 | 4

## A wavelet transform runs on {s} kHz data; consider level {j}'s detail.

::: part
answer: s/2^(j + 1)
unit: kHz
tolerance: 0.5%

### What is its lower band edge?

::: misses
- [any] $f_s/2^{j+1}$.
:::
:::

::: part
answer: s/2^j
unit: kHz
tolerance: 0.5%

### What is its upper band edge?

::: misses
- [s/2^(j - 1)] Level 1 already covers the top octave, ending at $f_s/2$.
- [any] $f_s/2^j$.
:::
:::
:::

::: check
kind: parts
given: N = 1024 | 4096 | 65536; J = 3 | 5; j = 1 | 2 | 3

## An N = {N}-sample signal gets a J = {J}-level wavelet transform.

::: part
answer: N/2^j
unit: coefficients
tolerance: 0

### How many detail coefficients does level {j} have?

::: misses
- [any] $N/2^j$.
:::
:::

::: part
answer: N/2^J
unit: coefficients
tolerance: 0

### How many approximation coefficients remain?

::: misses
- [any] $N/2^J$.
:::
:::
:::

::: check
kind: parts
given: s = 0.01 | 0.1 | 0.5; N = 512 | 4096 | 1048576

## Wavelet denoising handles N = {N} coefficients with noise of standard deviation {s}.

::: part
answer: s*sqrt(2*ln(N))
tolerance: 0.5%

### What is the universal threshold?

::: misses
- [s*sqrt(2*log(N))] Use the natural logarithm.
- [any] $\sigma\sqrt{2\ln N}$.
:::
:::
:::

::: check
kind: parts
given: L = 2 | 4 | 8; N = 1024 | 8192

## A wavelet transform with length-{L} filters runs on {N} samples, to many levels.

::: part
answer: 2*L*N
unit: multiplications
tolerance: 0.5%

### About how many multiplications does it need?

::: misses
- [L*N] Each level costs L multiplications per output of each of two filters; the levels sum to twice the first.
- [any] About 2LN.
:::
:::
:::

::: check
kind: choice

## Why does a wavelet transform suit a signal with sharp transients on a slowly varying background better than an STFT with one fixed window?

- [ ] Wavelets have no frequency resolution
- [x] Its high-frequency bands use short effective windows, locating transients sharply in time, while its low-frequency bands use long ones, resolving the slow background in frequency; a single STFT window must compromise on both
- [ ] The wavelet transform is redundant, so it holds more information
- [ ] Wavelets are faster than FFTs

::: misses
- [1] They trade time and frequency resolution across scales; they have both.
- [3] The DWT is critically sampled, not redundant.
- [4] Both are O(N) to O(N log N); the difference is the tiling.
:::
:::

::: check
kind: derivation
vars: a = -10..10, b = -10..10

## A Haar level acts on [a, b].

::: step
answer: (a + b)/sqrt(2)

### What is the approximation s?
:::

::: step
answer: (a - b)/sqrt(2)

### What is the detail d?
:::

::: step
answer: a^2 + b^2

### What is $s^2 + d^2$?
:::

::: right
Right. The Haar step is a 45° rotation of the pair: it preserves energy and is its own inverse up to the order of outputs.
:::
:::

::: check
kind: python

## Write `haar(x, J)`: a J-level orthonormal Haar transform of a list whose length is divisible by $2^J$, returning (approximation, [detail level 1, …, detail level J]), and `ihaar(a, details)` that inverts it.

```python starter
def haar(x, J):
    return (list(x), [])

def ihaar(a, details):
    return list(a)
```

```python solution
import math

def haar(x, J):
    s = math.sqrt(2)
    a, details = list(x), []
    for _ in range(J):
        details.append([(a[2 * i] - a[2 * i + 1]) / s for i in range(len(a) // 2)])
        a = [(a[2 * i] + a[2 * i + 1]) / s for i in range(len(a) // 2)]
    return (a, details)

def ihaar(a, details):
    s = math.sqrt(2)
    a = list(a)
    for d in reversed(details):
        out = []
        for ai, di in zip(a, d):
            out += [(ai + di) / s, (ai - di) / s]
        a = out
    return a
```

```python tests
a, d = haar([4, 6, 10, 12], 2)
assert abs(a[0] - 16) < 1e-12 and abs(d[1][0] + 6) < 1e-12, "Two levels of [4, 6, 10, 12]: approximation 16, level-2 detail −6."
import random
r = random.Random(1)
x = [r.gauss(0, 1) for _ in range(64)]
a, d = haar(x, 4)
assert max(abs(u - v) for u, v in zip(ihaar(a, d), x)) < 1e-12, "Perfect reconstruction."
assert abs(sum(v * v for v in a) + sum(v * v for lev in d for v in lev) - sum(v * v for v in x)) < 1e-9, "Energy preserved."
```

::: right
Right. Swapping in Daubechies filters changes the arithmetic of each step, not the structure.
:::
:::

::: explain
## Why does thresholding wavelet coefficients remove noise while keeping sharp edges, when a lowpass filter blurs them?

::: model
In a wavelet transform, a signal that is smooth except for a few edges is represented by a few large coefficients: smooth regions produce almost nothing in the detail bands, and each edge produces a handful of large details concentrated at its location across the scales. White noise, by contrast, spreads evenly across every coefficient at a small level. Setting the small coefficients to zero removes most of the noise while keeping the large ones that describe the edges, so after inversion the edges remain sharp. A lowpass filter instead removes all high-frequency content everywhere, including the high frequencies that make an edge sharp.
:::
:::

::: practice
::: check
given: s = 44.1 | 96; j = 2 | 3
answer: s/2^(j + 1)
unit: kHz
tolerance: 0.5%

## Lower edge of level {j}'s detail at {s} kHz?

::: misses
- [any] $f_s/2^{j+1}$.
:::
:::

::: check
given: s = 0.05 | 0.3; N = 2048 | 16384
answer: s*sqrt(2*ln(N))
tolerance: 0.5%

## Universal threshold for σ = {s}, N = {N}?

::: misses
- [any] $\sigma\sqrt{2\ln N}$.
:::
:::

::: check
given: a = 2 | 9; b = 3 | 7
answer: (a + b)/sqrt(2)
tolerance: 0.5%

## Haar approximation of [{a}, {b}]?

::: misses
- [any] $(x_0 + x_1)/\sqrt2$.
:::
:::
:::
