Exercise: filter banks and Daubechies wavelets
Before this unit: Localised basis functions and the Haar transform (previous unit); Day 1: Images, filters and frequencies.
See also: Day 3: Bit depth and noise.
Goal
By the end of this exercise you should be able to:
- run a short signal through the Haar filter bank by hand and relate it to the Haar transform,
- explain why the two branches together reconstruct the signal although each branch alone is aliased,
- check vanishing moments of a filter and predict which signals give zero details,
- count the cost of the cascade, and compare border rules.
Do the pen-and-paper part first, then the demos. Questions marked (math) are for those who want the derivations; the concept questions do not depend on them.
Background
| Idea | What it means |
|---|---|
| Analysis | a_k = \sum_n h_n\,x_{2k+n}, d_k = \sum_n g_n\,x_{2k+n}: filter, keep every second sample |
| Synthesis | upsample, filter, add: x_{2k+n} \mathrel{+}= h_n a_k + g_n d_k |
| Haar | h = (1, 1)/\sqrt2, g = (1, -1)/\sqrt2 |
| db2 | h = (1+\sqrt3,\ 3+\sqrt3,\ 3-\sqrt3,\ 1-\sqrt3)/(4\sqrt2) \approx (0.4830,\ 0.8365,\ 0.2241,\ -0.1294) |
| High-pass from low-pass | g_n = (-1)^n h_{L-1-n}; db2: g = (h_3, -h_2, h_1, -h_0) \approx (-0.1294,\ -0.2241,\ 0.8365,\ -0.4830) |
| Vanishing moments p | \sum_n n^m g_n = 0 for m = 0, \dots, p-1: polynomials of degree < p give zero details |
| Cascade | repeat on the low-pass branch; N + N/2 + N/4 + \dots < 2N samples filtered |
By hand
B1. Run x = (4, 6, 10, 12) through one level of the Haar filter bank (periodic border: x_4 = x_0, but you will not need it). (a) Compute a_0, a_1 and d_0, d_1. Compare with the averages and half-differences of the Haar unit. (b) Synthesis of the low branch alone (d = 0): upsample a, filter with h. What signal do you get? And the high branch alone? Check that they add up to x.
B2. For db2, use the decimals in the table. (a) Check \sum_n g_n = 0 and \sum_n n\,g_n = 0 (with n = 0, 1, 2, 3). (b) Compute \sum_n n^2 g_n. How many vanishing moments does db2 have?
B3. The ramp x = (0, 1, 2, 3, 4, 5, 6, 7), one level of db2. (a) Compute the detail d_0 = \sum_n g_n x_n and d_1 = \sum_n g_n x_{2+n}. (b) With a periodic border, the last detail d_3 uses x_6, x_7, x_0, x_1. Compute it. Why is it not zero? (c) With Haar, what are the four details?
B4. Count the multiply-adds of a 3-level DWT of a signal of 1024 samples with db2 (both filters have 4 taps; each output sample of each filter costs 4). Then for all 10 levels, and for db8 (16 taps) with all 10 levels. Compare with an FFT of 1024 samples, about \tfrac N2\log_2 N complex multiplications (4 real ones each).
B5. With symmetric or zero borders, every coefficient whose filter touches the signal is kept: (N + L - 2)/2 per branch for even N and L. How many coefficients does one level of db4 (L = 8) give for N = 64? How many with a periodic border?
B6. A signal is piecewise linear (straight pieces with a few kinks). Which of Haar, db2, db4 gives zero details inside the straight pieces? Which one would you choose to describe the signal with the fewest coefficients, and why not simply the one with the most vanishing moments?
B7. In B1(b) the low branch alone gave a staircase. Explain in words why dropping the high branch of db2 on a fast-oscillating signal produces slow wiggles that were not in the signal.
B1. (a) a_0 = (4 + 6)/\sqrt2 = 7.07, a_1 = (10 + 12)/\sqrt2 = 15.56; d_0 = (4 - 6)/\sqrt2 = -1.41, d_1 = (10 - 12)/\sqrt2 = -1.41. These are the averages (5, 11) and half-differences (-1, -1) times \sqrt2: the orthonormal Haar step. (b) Low branch: x_{2k} = h_0 a_k, x_{2k+1} = h_1 a_k, i.e. a_k/\sqrt2 twice: (5, 5, 11, 11). High branch: g_0 d_k, g_1 d_k: (-1, 1, -1, 1). Sum: (4, 6, 10, 12) ✓.
B2. (a) -0.1294 - 0.2241 + 0.8365 - 0.4830 = 0.0000; 0 - 0.2241 + 2 \cdot 0.8365 - 3 \cdot 0.4830 = -0.2241 + 1.6730 - 1.4489 = 0.0000. (b) 0 - 0.2241 + 4 \cdot 0.8365 - 9 \cdot 0.4830 = -0.2241 + 3.3460 - 4.3470 = -1.2251 (exactly -1.2247 with all digits): not zero. db2 has 2 vanishing moments.
B3. (a) d_0 = -0.1294 \cdot 0 - 0.2241 \cdot 1 + 0.8365 \cdot 2 - 0.4830 \cdot 3 = 0, and d_1 = \sum g_n (2 + n) = 2\sum g_n + \sum n g_n = 0. (So is d_2.) (b) d_3 = -0.1294 \cdot 6 - 0.2241 \cdot 7 + 0.8365 \cdot 0 - 0.4830 \cdot 1 = -0.776 - 1.569 - 0.483 = -2.83. The periodic border glues the end of the ramp (7) to its start (0): the filter sees a jump of -7, not a straight line. (With a symmetric border the details at both ends are \mp 0.61, much smaller.) (c) Haar: (0 - 1)/\sqrt2 = -0.71 for every pair: (-0.71, -0.71, -0.71, -0.71). Haar has only 1 vanishing moment.
B4. 3 levels, db2: 4 \cdot (1024 + 512 + 256) = 7168 (two filters, each producing half as many samples as there are inputs, so 4 per input sample). All 10 levels: 4 \cdot (1024 + 512 + \dots + 2) = 4 \cdot 2046 = 8184. db8: 16 \cdot 2046 = 32\,736. FFT: 512 \cdot 10 = 5120 complex multiplications, about 20\,480 real ones. The DWT grows like N, the FFT like N\log N; for short filters the DWT is cheaper.
B5. (64 + 8 - 2)/2 = 35 per branch, 70 numbers for 64 samples. Periodic: 32 + 32 = 64.
B6. db2 and db4 (2 and 4 vanishing moments) both give zero details inside straight pieces; Haar does not. db2 is the better choice: the filter is shorter (4 taps instead of 8), so each kink touches fewer coefficients, while the straight pieces are already handled. More vanishing moments only help if the signal is curved; they always cost length.
B7. The low-pass filter of db2 is not an ideal cut at half the band: some energy of fast oscillations passes through it. Keeping every second sample then folds those fast oscillations down to slow ones (aliasing). Normally the high-pass branch carries the same folded part with the opposite sign and cancels it on synthesis; without the high branch, the folded slow wiggles stay in the output.
Math questions
B8. (math) Show that the Haar functions satisfy the two-scale relations \varphi(t) = \varphi(2t) + \varphi(2t - 1) and \psi(t) = \varphi(2t) - \varphi(2t - 1), and read off h and g from \varphi(t) = \sqrt2\sum h_n\varphi(2t - n).
B9. (math) Using h = (1+\sqrt3,\ 3+\sqrt3,\ 3-\sqrt3,\ 1-\sqrt3)/(4\sqrt2), show exactly that \sum_n h_n^2 = 1 and h_0 h_2 + h_1 h_3 = 0.
B10. (math) Show that if \sum_n n^m g_n = 0 for m = 0, \dots, p-1, then every polynomial of degree < p gives d_k = 0 for all k (away from the borders).
B11. (math) Let A_\ell (N/2 \times N) map x to a and A_h map x to d, with A = \binom{A_\ell}{A_h} orthogonal (periodic border). Show that the low branch alone, P_\ell = A_\ell^\top A_\ell, is an orthogonal projection, that P_\ell + P_h = I, and that P_\ell P_h = 0. Interpret this as V_{j+1} = V_j \oplus W_j.
B8. \varphi(2t) is 1 on [0, \tfrac12), \varphi(2t - 1) is 1 on [\tfrac12, 1); their sum is \varphi and their difference is \psi. Matching \varphi(t) = \sqrt2(h_0\varphi(2t) + h_1\varphi(2t-1)) gives h_0 = h_1 = 1/\sqrt2; similarly g = (1, -1)/\sqrt2.
B9. With the common factor 1/32: (1+\sqrt3)^2 + (3+\sqrt3)^2 + (3-\sqrt3)^2 + (1-\sqrt3)^2 = (4 + 2\sqrt3) + (12 + 6\sqrt3) + (12 - 6\sqrt3) + (4 - 2\sqrt3) = 32, so \sum h_n^2 = 1. And (1+\sqrt3)(3-\sqrt3) + (3+\sqrt3)(1-\sqrt3) = (3 - \sqrt3 + 3\sqrt3 - 3) + (3 - 3\sqrt3 + \sqrt3 - 3) = 2\sqrt3 - 2\sqrt3 = 0.
B10. Let x_n = \sum_{q < p}\beta_q n^q. Then d_k = \sum_n g_n x_{2k+n} = \sum_q \beta_q\sum_n g_n (2k + n)^q = \sum_q\beta_q\sum_{m=0}^{q}\binom{q}{m}(2k)^{q-m}\sum_n g_n n^m. Every inner sum has m \le q < p and vanishes. “Away from the borders” means the filter only touches true samples of the polynomial.
B11. A^\top A = A_\ell^\top A_\ell + A_h^\top A_h = I gives P_\ell + P_h = I. AA^\top = I gives A_\ell A_\ell^\top = I and A_\ell A_h^\top = 0. Hence P_\ell^2 = A_\ell^\top(A_\ell A_\ell^\top)A_\ell = P_\ell, P_\ell^\top = P_\ell (an orthogonal projection), and P_\ell P_h = A_\ell^\top(A_\ell A_h^\top)A_h = 0. The ranges of P_\ell (the approximation space V_j) and P_h (the detail space W_j) are perpendicular and together give everything: V_{j+1} = V_j \oplus W_j.
Explore: the filter bank
A signal of 64 samples goes through one level of the filter bank. Top: input (black) and output (orange). Left column: the low-pass and high-pass filtered signals (faint) and the kept samples, every second one (dots). Right column: each branch after upsampling and filtering; the two add up to the output. Grey lines mark the signal’s borders. Break it with drop the high-pass branch or wrong synthesis filters, and change the boundary rule. cascade repeats the step on the low branch (levels); basis functions shows \varphi and \psi built by the cascade algorithm (iterations). The readout gives the largest reconstruction error and the largest detail, inside the signal and overall.
E1. Preset perfect reconstruction (db4, HeaviSine). What is the largest reconstruction error? How many coefficients are there, and how do \Sigma x^2 and \Sigma a^2 + \Sigma d^2 compare?
E2. Preset Haar = unit 1 (two steps). All details are zero. Why? (The jumps are at samples 20 and 40.)
E3. Preset remove one branch: aliasing (db2, chirp). What is the reconstruction error now? Describe the output. Untick the box and tick wrong synthesis filters instead: what is the error? Then try Haar and db8 with the high branch dropped.
E4. Preset quadratic: db3 details vanish. Compare the largest detail inside for Haar, db2, db3, db4. Where is the largest detail overall, and why?
E5. Signal cubic: which is the first wavelet in the list whose inside details vanish? Is that what you expect?
E6. Preset periodic boundary: false edge (db4, ramp). Read the largest detail inside and overall. Switch to symmetric and zero: what changes, including the number of coefficients and the energies?
E7. Preset cascade: 4 levels (db2, HeaviSine). How many coefficients in total, and how many samples were filtered? Switch the boundary to zero.
E8. Preset basis functions (db2). Move iterations from 0 to 10. When does the shape stop changing visibly? Compare Haar, db2, db4 and db8 at 10 iterations: support and smoothness.
E1. Max |x - \text{output}| = 1.8 \cdot 10^{-15}, i.e. rounding error. 32 + 32 = 64 coefficients; \Sigma x^2 = 604.988 = \Sigma a^2 + \Sigma d^2 (orthogonal transform, periodic border).
E2. The jumps sit between samples 19 and 20 and between 39 and 40: both fall between Haar pairs (0–1, 2–3, …), so every pair is flat and every difference is 0. The low branch alone reproduces the signal. (Shift a jump by one sample and one detail appears.)
E3. With the high branch dropped (db2): max error 1.18; the output is a smoothed chirp with spurious slow wiggles where the chirp is fast (the folded-down frequencies). Wrong synthesis filters: 1.97, worse than doing nothing. High branch dropped with Haar: 0.955, with db8: 1.19.
E4. Largest |d| inside: Haar 0.0435, db2 1.2 \cdot 10^{-3}, db3 8.3 \cdot 10^{-17}, db4 5.6 \cdot 10^{-17}. The quadratic has degree 2: db3 and db4 (3 and 4 vanishing moments) give zero; db2 almost (it kills the linear part, and a parabola is almost linear over 4 samples); Haar does not. Overall for db3, 0.0416 near the end (sample 60): the periodic border glues the parabola’s end to its start, which has a different slope.
E5. db4 (inside 8.3 \cdot 10^{-17}); db3 gives 1.0 \cdot 10^{-4}, db2 3.4 \cdot 10^{-3}. A cubic has degree 3, so it needs p \ge 4.
E6. Periodic: inside 1.4 \cdot 10^{-16}, overall 0.48 (at sample 58: the wrap-around jump from the top of the ramp to its bottom). Symmetric: overall 0.001, almost no border detail, but 35 + 35 = 70 coefficients and \Sigma a^2 + \Sigma d^2 = 21.343 \ne \Sigma x^2 = 20.836 (the extra coefficients see mirrored samples twice). Zero: overall 0.48 (the jump down to 0), also 35 + 35 coefficients, with \Sigma a^2 + \Sigma d^2 = 20.836. All three reconstruct to \sim 10^{-16}.
E7. Periodic: 32 + 16 + 8 + 4 details + 4 approximations = 64 coefficients; 64 + 32 + 16 + 8 = 120 samples filtered, less than 2N = 128. Zero border: 33 + 18 + 10 + 6 + 6 = 73 coefficients: each level keeps a few extra.
E8. At 0 iterations both are spikes, after 1 they are the filter taps themselves (\sqrt2 h, \sqrt2 g); the shape settles after about 5–6 iterations (the steps get finer, the outline stays). Supports: Haar [0, 1], db2 [0, 3], db4 [0, 7], db8 [0, 15]. Haar is a box, db2 is continuous with sharp corners, db4 and db8 are increasingly smooth and wide.
Explore: smoother wavelets, sparser coefficients
The sparsity demo of the Haar unit, now with four wavelets (5 levels, periodic border).
E9. Preset HeaviSine: db4 vs Haar (a smooth wave with two jumps). Read the K for 1 % error for Fourier, then select Haar, db2, db4 and db8 in turn.
E10. Signal step: the same four wavelets. Which wins now, and why?
E11. Signal Doppler and Blocks: which wavelet would you pick for each?
E9. Fourier 145; Haar 184, db2 54, db4 34, db8 40. The smooth parts need vanishing moments (Haar is worse than Fourier here!), but db8’s long filter spreads the two jumps over more coefficients than db4’s.
E10. Haar 11, db2 24, db4 39, db8 55 (Fourier 176). A pure jump has no smooth part to exploit, so only the filter length matters: the shortest wavelet wins.
E11. Doppler (smooth oscillation): db8 (69) or db4 (75), against Haar 196 and Fourier 231. Blocks (piecewise constant): Haar (39) against db2 77, db4 103, db8 131. Choose the wavelet to match the signal: vanishing moments for smooth parts, short filters for jumps.
Code
Write the algorithms of this unit yourself, in Rust. Setup, and how to run and check your code: coding exercises: getting started. Your code goes in wavelets/src/filterbank/exercise.rs. The low-pass filters of Haar, db2, db3 and db4 are given.
C1. highpass(h): the matching high-pass, g_n = (-1)^n h_{L-1-n}. The check is the db2 table.
C2. vanishing_moments(g): count m = 0, 1, 2, \dots while \sum_n n^m g_n = 0 (up to rounding). The check is B2: Haar 1, db2 2, db3 3, db4 4.
C3. analysis(x, h): one level of the filter bank with a periodic border:
for k = 0 … N/2 − 1:
a[k] = 0; d[k] = 0
for n = 0 … L − 1:
a[k] = a[k] + h[n] · x[(2k + n) mod N] # low-pass, every second sample
d[k] = d[k] + g[n] · x[(2k + n) mod N] # high-pass, every second sample
The checks are B1(a) and B3: the db2 details of the ramp 0, \dots, 7 are 0, 0, 0, -2.83.
C4. synthesis(a, d, h): each coefficient puts its filter back where it came from:
x = N zeros
for k = 0 … N/2 − 1:
for n = 0 … L − 1:
x[(2k + n) mod N] = x[(2k + n) mod N] + h[n] · a[k] + g[n] · d[k]
The checks are B1(b) (the low branch alone gives 5, 5, 11, 11), perfect reconstruction and \sum a^2 + \sum d^2 = \sum x^2 for all four wavelets.
C5. dwt(x, h, levels) and idwt(c, h, levels): the cascade, repeating C3 on the approximation only, and back with C4. Layout (a_L \mid d_L \mid \dots \mid d_1), as in the Haar unit. The checks: the Haar cascade gives the orthonormal Haar transform of the board example; idwt undoes dwt; a quadratic has zero db3 details.
Then cargo run -p wavelets -- filterbank and open the pictures in out/filterbank/.
C6. The program prints the largest level-1 detail of HeaviSine (a smooth wave with two jumps, 256 samples) on the smooth parts and at the jumps, for Haar, db2, db3 and db4. Before you run it: how should the smooth-part column change from one wavelet to the next? Then look at details.png (the level-1 details; from the top: Haar, db2, db3, db4).
C7. It also prints how many coefficients (5 levels) each wavelet needs for 1 % error on HeaviSine, a step and the Doppler signal. Which wavelet wins on each signal, and why?
C8. aliasing.png: a chirp (grey) through db2, rebuilt from both branches (dots) and from the low branch alone (orange). Where does the low branch fail, and what does it put there? How many samples does the 5-level cascade filter, compared with 2N?
The model solution is wavelets/src/filterbank/solution.rs in the download.
C6. On the smooth parts the largest detail falls by a factor of about 20 for every extra vanishing moment: Haar 0.14, db2 0.0059, db3 0.00026, db4 0.000012. At the jumps the details stay large but get smaller as the filter gets longer: 1.53, 0.96, 0.67, 0.46 (the jump is spread over more coefficients). In details.png the Haar details follow the sine along the whole signal; for db2 to db4 the curve is flat except for spikes at the two jumps.
C7. HeaviSine: Haar 184, db2 54, db3 35, db4 34: vanishing moments pay off on the smooth parts. Step: Haar 10, db2 24, db3 33, db4 39: a pure jump has nothing smooth, and the shortest filter touches the fewest coefficients. Doppler: 197, 109, 86, 75: a smooth oscillation needs many vanishing moments.
C8. Both branches together rebuild the chirp exactly (largest error 1.2 \cdot 10^{-7}, rounding). The low branch alone follows the slow start of the chirp, but where it oscillates fast (the right half) it gives a slow, wrong wave instead of the fast one: largest error 1.17. These are the fast oscillations folded down by keeping every second sample, normally cancelled by the high branch. The cascade filters 256 + 128 + 64 + 32 + 16 = 496 samples, less than 2N = 512.
Where this leads
Images are 2D: the next unit, the 2D wavelet transform, applies the 1D filter bank to every row and then every column. One level gives four subbands (an approximation and three orientations of detail), and the “keep the largest” experiment shows how sparse real images are in a wavelet basis.