Quiz: the Haar transform
Before this unit: Day 1: Images, filters and frequencies.
See also: Day 3: Bit depth and noise.
Short questions to test your understanding. Try each on paper first, then open the answer. By hand, a Haar step replaces a pair (a, b) by s = (a+b)/2 and d = (a-b)/2; the orthonormal version uses 1/\sqrt2 instead of 1/2.
Q1. A signal is zero except for one spike. Its Fourier magnitudes are all equal. Where in the Fourier coefficients is the position of the spike?
Magnitudes and phases.
In the phases: a spike at n_0 has X_k = e^{-2\pi i k n_0/N}, magnitude 1 for every k, phase -2\pi k n_0/N. The position is spread over all coefficients together; no single coefficient says “here”.
Q2. Why can a short-time Fourier transform with one window size not resolve both a sharp edge and a slow wave well?
What does a short window contain of a slow wave? What does a long window do to the edge?
A short window locates the edge, but contains only a fraction of a cycle of the slow wave, so its frequency cannot be determined. A long window contains many cycles, but smears the edge over the window length. Position and frequency resolution trade off against each other (uncertainty relation); wavelets use short windows for high frequencies and long windows for low ones.
Q3. Compute the Haar transform (by hand, 1/2) of (3, 5, 5, 3).
Two levels: averages and differences of the pairs, then of the two averages.
Level 1: averages (4, 4), differences (-1, 1). Level 2: average 4, difference 0. Result (4 \mid 0 \mid -1, 1). Check: 4 \pm 0 = (4, 4); 4 \pm (-1) = (3, 5), 4 \pm 1 = (5, 3).
Q4. Invert the by-hand Haar transform (10 \mid 2 \mid 1, -1).
a = s + d, b = s - d, coarse to fine.
10 \pm 2 = (12, 8); then 12 \pm 1 = (13, 11) and 8 \pm (-1) = (7, 9): x = (13, 11, 7, 9).
Q5. Which of these signals of 8 samples has the fewest nonzero Haar coefficients: a constant, a spike, a ramp, a step between samples 4 and 5?
A Haar pattern gives zero when it sees a constant.
The constant: 1 (only the average). The step exactly in the middle: 2 (the average and the coarsest detail). The spike: 4 (\log_2 8 + 1). The ramp: all 8, because every pattern sees a slope.
Q6. With the orthonormal Haar transform, a signal has coefficients (10, -4, 3, 0, 1, 0, -1, 0). You keep the two largest. What is the squared error, without reconstructing?
In an orthonormal basis the error is the sum of the dropped squares.
Keep 10 and -4; drop 3, 1, -1 (and the zeros): 9 + 1 + 1 = 11.
Q7. Why does the by-hand normalisation s = (a+b)/2 not preserve \sum x^2, while (a+b)/\sqrt2 does?
Compare s^2 + d^2 with a^2 + b^2 for one pair.
With 1/2: s^2 + d^2 = \tfrac14\big((a+b)^2 + (a-b)^2\big) = \tfrac12(a^2 + b^2), half the energy per step. With 1/\sqrt2: \tfrac12\big((a+b)^2 + (a-b)^2\big) = a^2 + b^2. The normalised step is an orthogonal 2\times2 matrix, a rotation plus a reflection.
Q8. (math) Show that the Haar basis vectors (1, 1, -1, -1)/2 and (1, -1, 0, 0)/\sqrt2 are orthogonal, and explain in words why any two Haar patterns at different levels are orthogonal.
Where the short pattern is nonzero, what does the long one look like?
Dot product: \tfrac{1}{2\sqrt2}(1 \cdot 1 + 1 \cdot (-1) + 0 + 0) = 0. In general, a finer pattern lies entirely inside one half of a coarser pattern, where the coarser one is constant. The dot product is then that constant times the sum of the finer pattern, which is zero (+ and - halves cancel). Two patterns at the same level do not overlap.