Blog · updated 18 Nov 2026 · 62 min read

Wavelets, from first principles

Windows that shrink as frequency grows; when such a transform can be inverted; Haar by hand; multiresolution analysis and the two-scale equation; the fast wavelet transform as a filter bank, and lifting; perfect reconstruction; vanishing moments and why they make signals sparse; Daubechies-4 solved by hand; denoising with a proof of the universal threshold; and what the discrete transform gets wrong. Part 3 of Seeing in frequencies.

Contents
  1. Notation and conventions
  2. 1. Let the window scale with the frequency
  3. 2. When can you invert it?
  4. 3. Too many coefficients
  5. 4. Haar, by hand
  6. 5. Multiresolution analysis
  7. 6. From functions to filters: the fast wavelet transform
  8. 7. Theory meets code
  9. Which way do the levels count?
  10. Why one filter is low-pass and the other high-pass
  11. Four filters, one list of numbers
  12. PyWavelets, rebuilt in NumPy
  13. Where do the samples enter?
  14. Where is each coefficient?
  15. 8. The lifting scheme
  16. 9. Perfect reconstruction
  17. 10. Vanishing moments: why wavelets make signals sparse
  18. 11. Daubechies-4, by hand
  19. 12. Where the filter numbers come from
  20. 13. Denoising
  21. 14. What the discrete transform gets wrong
  22. 15. Images, briefly
  23. 16. Choosing a tool
  24. 17. Implementation and caveats
  25. 18. Where it is used, and what is open
  26. Self-check
  27. References

Seeing in frequencies, part 3 of 4. Fourier analysis → the discrete Fourier world → wavelets → wavelets for images.

This is the picture most people meet first: an image split into a half-size approximation and three images of detail, then the approximation split again, and again.

It is beautiful, and it hides almost everything: which numbers produce each quadrant, why there are three detail images, why the transform loses nothing, and why most of it is black. This post builds the 1-D machinery behind it and connects it, line by line, to the code that draws it. Part 4 then does the 2-D picture by hand.

Part 2 ended at a wall. A spectrogram looks at a signal through one window, and the uncertainty principle forbids any window from being sharp in both time and frequency, so the song’s chord and its click could not both be seen clearly. Here is the same song through a different lens: a window that shrinks as the frequency grows.

Low frequencies get long windows, so the chord’s two tones separate (the band ripples because the tones beat against each other 16 times a second, but a slice through it, or its average over the chord, has two peaks, at 40 and 56 Hz); high frequencies get short windows, so the click stays a click; and the chirp climbs as a bright diagonal. Each wavelet still obeys the uncertainty principle, but it is spent differently at every scale. This post builds that idea from the ground up, and then makes it fast, exact and compact: the discrete wavelet transform, which costs less than an FFT, reconstructs perfectly, and represents piecewise-smooth signals with very few numbers.

The route: Sections 1 to 3 define the continuous transform and prove when it can be inverted. Section 4 does the simplest wavelet, Haar, by hand. Sections 5 and 6 build the machinery: multiresolution analysis and the fast algorithm. Section 7 connects that theory to the code you will actually run: which way the levels count, why one filter is low-pass and the other high-pass, how libraries store the four filters, and where the samples enter. Sections 8 and 9 cover lifting and perfect reconstruction. Sections 10 to 12 explain what makes a wavelet good, construct Daubechies’ famous one by hand, and show where the numbers in every wavelet library come from. Section 13 uses all of it to remove noise, with a proof of the standard threshold, Section 14 is honest about what goes wrong, and Section 17 collects the traps. Images get their own post, Part 4.

Notation and conventions

SymbolMeaning
ψa,b(t)=a−1/2ψ(t−ba)\psi_{a,b}(t) = a^{-1/2}\psi\big(\frac{t-b}{a}\big)continuous wavelet family
Wf(a,b)=⟨f,ψa,b⟩W_f(a, b) = \langle f, \psi_{a,b}\ranglecontinuous wavelet transform
φ\varphi, ψ\psiscaling function, wavelet
ψj,k(x)=2j/2ψ(2jx−k)\psi_{j,k}(x) = 2^{j/2}\psi(2^jx - k)discrete family; larger jj is finer
VjV_j, WjW_japproximation and detail spaces
hkh_k, gkg_klow-pass and high-pass filters
H(ω)=∑khke−ikωH(\omega) = \sum_k h_ke^{-ik\omega}frequency response, ω\omega in radians per sample
aj[k]a_j[k], dj[k]d_j[k]approximation and detail coefficients

Filter responses use radians per sample, ω=2πξ\omega = 2\pi\xi, because every wavelet book does; continuous transforms keep the ξ\xi of Parts 1 and 2. Daubechies and Strang count levels upward (Vj⊂Vj+1V_j \subset V_{j+1}, larger jj finer), as here; Mallat’s book counts the other way, and libraries count the number of splits. Section 7 translates between all three.

1. Let the window scale with the frequency

Pick one function ψ\psi, a short oscillation with zero mean, and make a family from it by stretching and sliding:

ψa,b(t)=1a ψ ⁣(t−ba),a>0, b∈R.\psi_{a,b}(t) = \frac{1}{\sqrt a}\,\psi\!\left(\frac{t - b}{a}\right), \qquad a > 0,\ b \in \mathbb{R}.

The scale aa stretches it (large aa, long and slow; small aa, short and fast) and bb slides it to time bb. The factor a−1/2a^{-1/2} keeps ∥ψa,b∥=∥ψ∥\lVert\psi_{a,b}\rVert = \lVert\psi\rVert.

Definition 1 (Continuous wavelet transform)

Wf(a,b)=⟨f,ψa,b⟩=∫f(t) 1a ψ ⁣(t−ba)‾ dt.W_f(a, b) = \langle f, \psi_{a,b}\rangle = \int f(t)\,\frac{1}{\sqrt a}\,\overline{\psi\!\left(\frac{t - b}{a}\right)}\,dt .

Two wavelets are worth knowing by name. The Morlet wavelet is a Gaussian-windowed complex wave, ψ(t)=e2πif0te−t2/2σ2\psi(t) = e^{2\pi i f_0t}e^{-t^2/2\sigma^2}, the one in the figure above. The Mexican hat is the negative second derivative of a Gaussian, (1−t2)e−t2/2(1 - t^2)e^{-t^2/2} up to normalisation, the 1-D case of the Laplacian of a Gaussian that computer vision uses to find blobs (see the Laplacian post).

What does Wf(a,⋅)W_f(a, \cdot) do in frequency? By Plancherel, ⟨f,ψa,b⟩=⟨f^,ψ^a,b⟩\langle f, \psi_{a,b}\rangle = \langle\hat f, \hat\psi_{a,b}\rangle, and by the shift and scaling rules of Part 1, ψ^a,b(ξ)=a ψ^(aξ) e−2πiξb\hat\psi_{a,b}(\xi) = \sqrt a\,\hat\psi(a\xi)\,e^{-2\pi i\xi b}. So

Wf(a,b)=∫f^(ξ) a ψ^(aξ)‾ e2πiξb dξ.W_f(a, b) = \int\hat f(\xi)\,\sqrt a\,\overline{\hat\psi(a\xi)}\,e^{2\pi i\xi b}\,d\xi .

For each scale, Wf(a,⋅)W_f(a, \cdot) is ff passed through a band-pass filter centred at ξc/a\xi_c/a, where ξc\xi_c is the centre frequency of ψ^\hat\psi. Doubling aa halves the centre frequency and halves the bandwidth: every filter has the same ratio of bandwidth to centre frequency (engineers call this “constant-Q”). That is the difference from the spectrogram, whose filters all have the same bandwidth.

The figure uses this directly. It labels each row by its centre frequency f=ξc/af = \xi_c/a and gives each wavelet a fixed number of cycles nn: its envelope has standard deviation σt=n/(2πf)\sigma_t = n/(2\pi f) in time and σf=f/n\sigma_f = f/n in frequency, so the ratio of bandwidth to frequency is 1/n1/n at every scale. These Gaussians meet the uncertainty bound of Part 2 exactly: the spreads of ∣ψ∣2\lvert\psi\rvert^2 and ∣ψ^∣2\lvert\hat\psi\rvert^2 are σt/2\sigma_t/\sqrt2 and σf/2\sigma_f/\sqrt2, whose product is σtσf/2=1/(4π)\sigma_t\sigma_f/2 = 1/(4\pi). What changes from row to row is only how the fixed area is shared between time and frequency.

2. When can you invert it?

A transform that cannot be undone is a feature extractor, not a representation. When does WfW_f determine ff?

Theorem 1 (Admissibility and the resolution of the identity)

Let ψ\psi be real with Cψ=∫0∞∣ψ^(ξ)∣2ξ dξ<∞C_\psi = \displaystyle\int_0^\infty\frac{\lvert\hat\psi(\xi)\rvert^2}{\xi}\,d\xi < \infty. Then for every f∈L2f \in L^2,

∫0∞ ⁣ ⁣∫−∞∞∣Wf(a,b)∣2 db daa2=Cψ ∥f∥2,\int_0^\infty\!\!\int_{-\infty}^{\infty}\lvert W_f(a, b)\rvert^2\,db\,\frac{da}{a^2} = C_\psi\,\lVert f\rVert^2 ,

and f=1Cψ∫0∞ ⁣ ⁣∫Wf(a,b) ψa,b db daa2f = \frac{1}{C_\psi}\displaystyle\int_0^\infty\!\!\int W_f(a, b)\,\psi_{a,b}\,db\,\frac{da}{a^2} (in the weak sense: inner products with any gg agree).

Proof

Step 1. Fix the scale and integrate over bb. By Section 1, b↦Wf(a,b)b \mapsto W_f(a, b) is the inverse Fourier transform of ξ↦f^(ξ)a ψ^(aξ)‾\xi \mapsto \hat f(\xi)\sqrt a\,\overline{\hat\psi(a\xi)}. Plancherel gives ∫∣Wf(a,b)∣2db=∫∣f^(ξ)∣2 a ∣ψ^(aξ)∣2 dξ\int\lvert W_f(a, b)\rvert^2db = \int\lvert\hat f(\xi)\rvert^2\,a\,\lvert\hat\psi(a\xi)\rvert^2\,d\xi.

Step 2. Integrate over aa and swap (everything is non-negative, so Tonelli allows it): ∫0∞daa2∫∣f^(ξ)∣2a∣ψ^(aξ)∣2dξ=∫∣f^(ξ)∣2(∫0∞∣ψ^(aξ)∣2daa)dξ\int_0^\infty\frac{da}{a^2}\int\lvert\hat f(\xi)\rvert^2a\lvert\hat\psi(a\xi)\rvert^2d\xi = \int\lvert\hat f(\xi)\rvert^2\Big(\int_0^\infty\lvert\hat\psi(a\xi)\rvert^2\frac{da}{a}\Big)d\xi.

Step 3. The inner integral does not depend on ξ\xi. For ξ>0\xi > 0 substitute u=aξu = a\xi, so da/a=du/uda/a = du/u: it equals ∫0∞∣ψ^(u)∣2du/u=Cψ\int_0^\infty\lvert\hat\psi(u)\rvert^2du/u = C_\psi. For ξ<0\xi < 0 substitute u=a∣ξ∣u = a\lvert\xi\rvert and use ∣ψ^(−u)∣=∣ψ^(u)∣\lvert\hat\psi(-u)\rvert = \lvert\hat\psi(u)\rvert, true because ψ\psi is real (Part 1): again CψC_\psi.

Step 4. Plancherel once more. What remains is Cψ∫∣f^∣2=Cψ∥f∥2C_\psi\int\lvert\hat f\rvert^2 = C_\psi\lVert f\rVert^2.

Step 5. Inversion. Applying Steps 1 to 4 to f+gf + g and f+igf + ig (polarisation) gives ∬Wf Wg‾ db da/a2=Cψ⟨f,g⟩\iint W_f\,\overline{W_g}\,db\,da/a^2 = C_\psi\langle f, g\rangle for all f,gf, g. The left side is ⟨∬Wf(a,b)ψa,b db da/a2, g⟩\big\langle\iint W_f(a,b)\psi_{a,b}\,db\,da/a^2,\ g\big\rangle, which is the claimed formula tested against gg.

Question 1

Why must a wavelet have zero mean?

Answer

Because CψC_\psi must be finite. If ψ^(0)=∫ψ≠0\hat\psi(0) = \int\psi \ne 0, then by continuity ∣ψ^(ξ)∣2≥c>0\lvert\hat\psi(\xi)\rvert^2 \ge c > 0 on some interval (0,δ)(0, \delta), and ∫0δc dξ/ξ=∞\int_0^\delta c\,d\xi/\xi = \infty. So ψ^(0)=0\hat\psi(0) = 0: the wavelet must oscillate, which is where the name (“small wave”) comes from. A wavelet responds to change, never to a constant.

The Morlet wavelet is not quite admissible: its Gaussian spectrum is centred at f0f_0 but leaks a little to zero frequency, so the classical definition subtracts a tiny correction term. In practice it is negligible. For the 8-cycle wavelets in the figure the spectrum at zero is e−32≈10−14e^{-32} \approx 10^{-14} of its peak.

3. Too many coefficients

The continuous transform turns a function of one variable into a function of two. It is extremely redundant: nearby (a,b)(a, b) give almost the same number, which is why the scalogram looks smooth. For analysis by eye, redundancy is a gift. For storage, compression or fast computation, it is waste.

Keep only a grid: scales a=2−ja = 2^{-j} and, at each scale, positions b=k 2−jb = k\,2^{-j}, spaced in proportion to the wavelet’s width. Fine scales get many positions, coarse scales few. On this dyadic grid the family becomes

ψj,k(x)=2j/2 ψ(2jx−k),j,k∈Z.\psi_{j,k}(x) = 2^{j/2}\,\psi(2^jx - k), \qquad j, k \in \mathbb{Z}.

For a general ψ\psi this family is a frame: it is redundant but stable, and every ff can be rebuilt from ⟨f,ψj,k⟩\langle f, \psi_{j,k}\rangle using a dual family. The remarkable fact is that for special ψ\psi it is an orthonormal basis of L2(R)L^2(\mathbb{R}): no redundancy at all, the three facts of Part 1 (coefficients are inner products, the sum rebuilds, energy is preserved) hold exactly, and the coefficients can be computed faster than an FFT. The simplest such ψ\psi is a hundred years older than the word “wavelet”.

4. Haar, by hand

In 1910 Alfréd Haar used the function that is +1+1 on [0,12)[0, \tfrac12) and −1-1 on [12,1)[\tfrac12, 1). Here is what it does to eight numbers.

Example 1 (Eight numbers, three steps)

Start with (9,7,3,5,6,10,2,6)(9, 7, 3, 5, 6, 10, 2, 6) (the first four are the example of Stollnitz, DeRose and Salesin’s primer).

Step 1. Pair them up, (9,7),(3,5),(6,10),(2,6)(9,7), (3,5), (6,10), (2,6), and replace each pair (p,q)(p, q) by its average p+q2\frac{p+q}2 and half-difference p−q2\frac{p-q}2: averages (8,4,8,4)(8, 4, 8, 4), details (1,−1,−2,−2)(1, -1, -2, -2). Nothing is lost, since p=average+detailp = \text{average} + \text{detail} and q=average−detailq = \text{average} - \text{detail}.

Step 2. Repeat on the averages only: (8,4)→6,2(8, 4) \to 6, 2 and (8,4)→6,2(8, 4) \to 6, 2. Averages (6,6)(6, 6), details (2,2)(2, 2).

Step 3. (6,6)→6,0(6, 6) \to 6, 0.

Result: (6∣0∣2,2∣1,−1,−2,−2)(6 \mid 0 \mid 2, 2 \mid 1, -1, -2, -2). One overall mean, then details at three scales, coarsest first. Setting the two details of size 1 to zero changes the rebuilt signal by at most 1 anywhere, while keeping 5 of 8 numbers: compression, in miniature.

Averages and half-differences are easy to compute but do not preserve energy. Dividing by 2\sqrt2 instead of 22 does: (p+q2)2+(p−q2)2=p2+q2\big(\frac{p+q}{\sqrt2}\big)^2 + \big(\frac{p-q}{\sqrt2}\big)^2 = p^2 + q^2. That is the orthonormal Haar transform, and its coefficients are the averaging ones multiplied by 2j/22^{j/2} at level jj: here (16.97∣0∣4,4∣1.41,−1.41,−2.83,−2.83)(16.97 \mid 0 \mid 4, 4 \mid 1.41, -1.41, -2.83, -2.83), exactly what PyWavelets returns (Section 17).

Proposition 1 (The Haar system is an orthonormal basis)

On [0,1)[0, 1), the constant 11 and the functions ψj,k(x)=2j/2ψ(2jx−k)\psi_{j,k}(x) = 2^{j/2}\psi(2^jx - k), j≥0j \ge 0, 0≤k<2j0 \le k < 2^j, with ψ=1[0,1/2)−1[1/2,1)\psi = \mathbf{1}_{[0,1/2)} - \mathbf{1}_{[1/2,1)}, form an orthonormal basis of L2[0,1)L^2[0, 1).

Proof

Step 1. Unit length. ψj,k\psi_{j,k} equals ±2j/2\pm2^{j/2} on an interval of length 2−j2^{-j}, so ∫ψj,k2=2j⋅2−j=1\int\psi_{j,k}^2 = 2^j\cdot2^{-j} = 1.

Step 2. Two different Haar functions are orthogonal. Their supports are dyadic intervals, which are either disjoint (product zero) or nested. If nested and different, the coarser function is constant on the finer one’s support, and the finer one has integral zero there. The constant 11 is orthogonal to every ψj,k\psi_{j,k} for the same reason.

Step 3. Completeness. By induction on JJ, the span of 11 and ψj,k\psi_{j,k} with j<Jj < J is all functions that are constant on each interval [m2−J,(m+1)2−J)[m2^{-J}, (m+1)2^{-J}): it has the right dimension, 2J2^J, and Step 1 of the example shows how to reach any such function. Step functions on dyadic intervals are dense in L2[0,1)L^2[0, 1), so nothing is orthogonal to all of them.

Haar is perfect for steps and poor for smooth curves: it approximates a sine wave by a staircase. Can we find wavelets that are orthonormal like Haar but smooth? The framework that answers yes is the next section.

5. Multiresolution analysis

The Haar example had a structure worth naming: a sequence of ever-finer approximations, each adding detail to the last.

Definition 2 (Multiresolution analysis (Mallat, Meyer))

A sequence of closed subspaces ⋯⊂V−1⊂V0⊂V1⊂⋯\cdots \subset V_{-1} \subset V_0 \subset V_1 \subset \cdots of L2(R)L^2(\mathbb{R}) such that

  1. nested: Vj⊂Vj+1V_j \subset V_{j+1};
  2. self-similar: f(x)∈Vjf(x) \in V_j if and only if f(2x)∈Vj+1f(2x) \in V_{j+1};
  3. complete: the union of all VjV_j is dense in L2L^2, and their intersection is {0}\{0\};
  4. generated by one function: there is a scaling function φ\varphi whose integer shifts φ(x−k)\varphi(x - k) form an orthonormal basis of V0V_0.

For Haar, VjV_j is the functions constant on intervals of length 2−j2^{-j} and φ=1[0,1)\varphi = \mathbf{1}_{[0,1)}. Read VjV_j as “what a camera with pixels 2−j2^{-j} wide can see”. Each VjV_j sits inside Vj+1V_{j+1}, and the difference is a detail space WjW_j, the orthogonal complement: Vj+1=Vj⊕WjV_{j+1} = V_j \oplus W_j.

a multiresolution analysis: spaces inside spacesV₃V₂V₁V₀W₀W₁W₂V₀ ⊂ V₁ ⊂ V₂ ⊂ V₃ ⊂ …Vⱼ: what a camera with pixels 2⁻ʲ wide seesVⱼ₊₁ = Vⱼ ⊕ WⱼWⱼ: the detail Vⱼ missesφ spans V₀ (by its shifts)ψ spans W₀ (by its shifts)so V₃ = V₀ ⊕ W₀ ⊕ W₁ ⊕ W₂
A function projected onto the Haar spaces V₀ ⊂ V₁ ⊂ … ⊂ V₆. Each step adds the detail the finer space can see.

Everything else follows from axiom 4 combined with nesting.

Proposition 2 (The two-scale equation)

There are numbers hkh_k with

φ(x)=2∑khk φ(2x−k),∑khk=2,∑khk hk−2m={1m=00m≠0.\varphi(x) = \sqrt2\sum_k h_k\,\varphi(2x - k), \qquad \sum_k h_k = \sqrt2, \qquad \sum_k h_k\,h_{k - 2m} = \begin{cases}1 & m = 0\\ 0 & m \ne 0.\end{cases}
Proof

Step 1. A basis of V1V_1. By axiom 4, {φ(x−k)}\{\varphi(x - k)\} is orthonormal in V0V_0; by axiom 2 and the substitution x→2xx \to 2x, {2φ(2x−k)}\{\sqrt2\varphi(2x - k)\} is orthonormal and spans V1V_1 (the 2\sqrt2 restores unit length).

Step 2. Expand φ\varphi in it. φ∈V0⊂V1\varphi \in V_0 \subset V_1, so φ=∑khk2φ(2x−k)\varphi = \sum_k h_k\sqrt2\varphi(2x - k) with hk=⟨φ,2φ(2⋅−k)⟩h_k = \langle\varphi, \sqrt2\varphi(2\cdot{} - k)\rangle, by fact 1 of Part 1.

Step 3. The sum of the hkh_k. Normalise ∫φ=1\int\varphi = 1 (one can show ∫φ≠0\int\varphi \ne 0 from axiom 3). Integrate both sides: 1=∑khk2∫φ(2x−k)dx=∑khk2⋅121 = \sum_kh_k\sqrt2\int\varphi(2x - k)dx = \sum_kh_k\sqrt2\cdot\tfrac12, so ∑khk=2\sum_kh_k = \sqrt2.

Step 4. Orthogonality of shifts. Shift Step 2 by mm: φ(x−m)=∑lhl2φ(2x−2m−l)=∑khk−2m2φ(2x−k)\varphi(x - m) = \sum_lh_l\sqrt2\varphi(2x - 2m - l) = \sum_kh_{k - 2m}\sqrt2\varphi(2x - k). The inner product of φ\varphi with φ(⋅−m)\varphi(\cdot - m) is, by orthonormality of the 2φ(2x−k)\sqrt2\varphi(2x - k), ∑khkhk−2m\sum_kh_kh_{k-2m}, and by axiom 4 it is 11 if m=0m = 0 and 00 otherwise.

For Haar, φ(x)=φ(2x)+φ(2x−1)\varphi(x) = \varphi(2x) + \varphi(2x - 1), so h=(12,12)h = (\tfrac1{\sqrt2}, \tfrac1{\sqrt2}). The whole multiresolution analysis is encoded in this short list of numbers, and so is the wavelet.

Proposition 3 (The wavelet from the scaling function)

Let gk=(−1)kh1−kg_k = (-1)^kh_{1-k} and ψ(x)=2∑kgkφ(2x−k)\psi(x) = \sqrt2\sum_kg_k\varphi(2x - k). Then the shifts ψ(x−m)\psi(x - m) are orthonormal, and each is orthogonal to every φ(x−n)\varphi(x - n).

Proof

Step 1. Reduce to sums of filter coefficients. As in Step 4 above, both functions are combinations of the orthonormal 2φ(2x−k)\sqrt2\varphi(2x - k), so ⟨ψ(⋅−m),φ(⋅−n)⟩=∑kgk−2mhk−2n\langle\psi(\cdot - m), \varphi(\cdot - n)\rangle = \sum_kg_{k-2m}h_{k-2n} and ⟨ψ(⋅−m),ψ(⋅−n)⟩=∑kgk−2mgk−2n\langle\psi(\cdot - m), \psi(\cdot - n)\rangle = \sum_kg_{k-2m}g_{k-2n}.

Step 2. ψ\psi against φ\varphi. Write the terms of ∑kgk−2mhk−2n=∑k(−1)kh1−k+2mhk−2n\sum_kg_{k-2m}h_{k-2n} = \sum_k(-1)^kh_{1-k+2m}h_{k-2n} as T(k)T(k) (using (−1)k−2m=(−1)k(-1)^{k - 2m} = (-1)^k). The substitution k′=1+2m+2n−kk' = 1 + 2m + 2n - k gives T(k′)=(−1)1+2m+2n−khk−2nh1+2m−k=−T(k)T(k') = (-1)^{1 + 2m + 2n - k}h_{k - 2n}h_{1 + 2m - k} = -T(k). The map k↦k′k \mapsto k' is its own inverse and has no fixed point (that would need 2k=1+2m+2n2k = 1 + 2m + 2n, impossible for integers), so the terms cancel in pairs and the sum is 00.

Step 3. ψ\psi against its shifts. gk−2mgk−2n=h1−k+2mh1−k+2ng_{k - 2m}g_{k - 2n} = h_{1 - k + 2m}h_{1 - k + 2n}, since the signs (−1)k−2m(−1)k−2n=1(-1)^{k-2m}(-1)^{k-2n} = 1. Substituting l=1−k+2ml = 1 - k + 2m, the sum becomes ∑lhlhl−2(m−n)\sum_lh_lh_{l - 2(m - n)}, which is 11 if m=nm = n and 00 otherwise, by the two-scale proposition.

One more fact, which we state without its longer proof: the shifts of ψ\psi span all of W0W_0 (see Daubechies, Ten Lectures, ch. 5). Then ψj,k\psi_{j,k} over all j,kj, k is an orthonormal basis of L2(R)L^2(\mathbb{R}), and VJ=V0⊕W0⊕⋯⊕WJ−1V_J = V_0 \oplus W_0 \oplus \cdots \oplus W_{J-1}.

Question 2

What is the scaling function for, if the wavelet does the work?

Answer

The scaling function is the camera; the wavelet records what the camera misses. A finite transform never uses all scales: it stops at some coarsest level, and everything coarser is summarised by the coefficients ⟨f,φJ,k⟩\langle f, \varphi_{J,k}\rangle, the low-resolution image that the details refine. That is the a3=6a_3 = 6 in the Haar example. Without φ\varphi, a finite transform would throw away the signal’s mean and slow trends.

6. From functions to filters: the fast wavelet transform

We never need φ\varphi or ψ\psi themselves to compute the coefficients. Write φj,k(x)=2j/2φ(2jx−k)\varphi_{j,k}(x) = 2^{j/2}\varphi(2^jx - k), and let aj[k]=⟨f,φj,k⟩a_j[k] = \langle f, \varphi_{j,k}\rangle and dj[k]=⟨f,ψj,k⟩d_j[k] = \langle f, \psi_{j,k}\rangle.

Proposition 4 (Mallat’s algorithm)

aj[k]=∑nhn−2k aj+1[n],dj[k]=∑ngn−2k aj+1[n],a_j[k] = \sum_nh_{n - 2k}\,a_{j+1}[n], \qquad d_j[k] = \sum_ng_{n-2k}\,a_{j+1}[n],aj+1[n]=∑khn−2k aj[k]+∑kgn−2k dj[k].a_{j+1}[n] = \sum_kh_{n-2k}\,a_j[k] + \sum_kg_{n - 2k}\,d_j[k].
Proof

Step 1. The two-scale equation at level jj. Substitute x→2jx−kx \to 2^jx - k in it and multiply by 2j/22^{j/2}: φj,k(x)=∑lhl 2(j+1)/2φ(2j+1x−2k−l)=∑nhn−2k φj+1,n(x)\varphi_{j,k}(x) = \sum_lh_l\,2^{(j+1)/2}\varphi(2^{j+1}x - 2k - l) = \sum_nh_{n - 2k}\,\varphi_{j+1,n}(x), with n=2k+ln = 2k + l. The same with gg gives ψj,k=∑ngn−2kφj+1,n\psi_{j,k} = \sum_ng_{n-2k}\varphi_{j+1,n}.

Step 2. Take inner products with ff. The inner product is linear and the filters are real: aj[k]=∑nhn−2k⟨f,φj+1,n⟩=∑nhn−2kaj+1[n]a_j[k] = \sum_nh_{n-2k}\langle f, \varphi_{j+1,n}\rangle = \sum_nh_{n-2k}a_{j+1}[n], and likewise dj[k]d_j[k].

Step 3. The inverse. {φj,k}∪{ψj,k}\{\varphi_{j,k}\}\cup\{\psi_{j,k}\} is an orthonormal basis of Vj+1V_{j+1}. Expanding φj+1,n\varphi_{j+1,n} in it, the coefficients are ⟨φj+1,n,φj,k⟩=hn−2k\langle\varphi_{j+1,n}, \varphi_{j,k}\rangle = h_{n-2k} (Step 1) and ⟨φj+1,n,ψj,k⟩=gn−2k\langle\varphi_{j+1,n}, \psi_{j,k}\rangle = g_{n-2k}. Taking the inner product of that expansion with ff gives the third formula.

Read the first line as signal processing: correlate the fine coefficients with the filter hh, then keep every second output. The second line does the same with gg. That is a two-channel filter bank, and repeating it on the approximation gives every level.

the fast wavelet transform is a filter bankxh̃↓2a↑2hg̃↓2d↑2gxanalysis: filter, keep every 2ndsynthesis: insert zeros, filter, addrepeat on a for the next level

Cost. Each level does two filterings of length LL on the current approximation and halves it: 2L(N+N/2+N/4+⋯ )<4LN2L(N + N/2 + N/4 + \cdots) < 4LN multiply-adds. That is O(N)O(N), cheaper than an FFT’s O(Nlog⁡N)O(N\log N).

Question 3

Where is the wavelet in the code?

Answer

Nowhere. A discrete wavelet transform is two short filters, a downsampling and a loop. The functions φ\varphi and ψ\psi appear only if you ask what the filters converge to when the loop is run backwards forever (Section 11’s cascade algorithm). Choosing a wavelet means choosing four, eight or twelve numbers.

Boundaries. A finite signal needs values beyond its ends, and libraries differ. Periodisation wraps around: exactly N/2N/2 coefficients per level, but a jump wherever the ends differ. Symmetric extension mirrors the signal: no jump, but more coefficients than samples. Measured with PyWavelets: Daubechies-4 (db2) on 8 samples gives 4 coefficients per band with periodisation and 5 with symmetric extension; Daubechies-8 (db4) on 256 samples gives 128 and 131.

7. Theory meets code

Most of the confusion around wavelets lives neither in the theory nor in the code, but in the translation between them. When I first learned this material, the proofs were clear and the library calls were easy; what I could not see was how one became the other. This section is that translation, point by point.

Which way do the levels count?

Three conventions are in common use, and they disagree about the direction of the index:

  • This post (Daubechies, Strang): Vj⊂Vj+1V_j \subset V_{j+1}, so a larger jj is finer, and ψj,k(x)=2j/2ψ(2jx−k)\psi_{j,k}(x) = 2^{j/2}\psi(2^jx - k) has width 2−j2^{-j}. The data sit in some finest space VJV_J, and each split moves down: VJ=VJ−1⊕WJ−1V_J = V_{J-1}\oplus W_{J-1}.
  • Mallat’s book: Vj+1⊂VjV_{j+1} \subset V_j, so a larger jj is coarser, and ψj,n(t)=2−j/2ψ(2−jt−n)\psi_{j,n}(t) = 2^{-j/2}\psi(2^{-j}t - n) has width 2j2^j. The data sit in V0V_0 and each split moves up: V0=V1⊕W1V_0 = V_1 \oplus W_1. This matches how code counts.
  • Libraries (PyWavelets, MATLAB): “level” counts how many splits have been done. cD1 is the first, finest detail; wavedec(x, w, level=n) returns [cA_n, cD_n, ..., cD_1], coarsest first.

The dictionary: Mallat’s jj is this post’s −j-j, and with data at this post’s level JJ, the library’s level ℓ\ell is this post’s j=J−ℓj = J - \ell. So cD1 =dJ−1= d_{J-1}, cD2 =dJ−2= d_{J-2}, and cA_n =aJ−n= a_{J-n}. Neither theory nor code is wrong: theory counts resolution upward from coarse, code counts the operations it has performed. When reading anything about wavelets, first check whether VjV_j grows or shrinks with jj.

Why one filter is low-pass and the other high-pass

Nothing in the two-scale equation says “filter”. The names come from two facts already proved:

  • H(0)=∑khk=2H(0) = \sum_kh_k = \sqrt2 (Section 5): a constant signal passes through hh with gain 2\sqrt2, the orthonormal version of averaging.
  • H(π)=∑k(−1)khk=0H(\pi) = \sum_k(-1)^kh_k = 0 (one vanishing moment, Section 10): the fastest possible signal, +1,−1,+1,…+1, -1, +1, \ldots, is blocked.

So hh passes slow signals and stops fast ones: low-pass. The alternating flip gives ∣G(ω)∣=∣H(ω+π)∣\lvert G(\omega)\rvert = \lvert H(\omega + \pi)\rvert, so gg does the opposite, G(0)=0G(0) = 0 and ∣G(π)∣=2\lvert G(\pi)\rvert = \sqrt2: high-pass. Orthonormality, ∣H∣2+∣G∣2=2\lvert H\rvert^2 + \lvert G\rvert^2 = 2 (Section 9), says that together they split the energy at every frequency exactly. For Haar, ∣H(ω)∣=2∣cos⁡(ω/2)∣\lvert H(\omega)\rvert = \sqrt2\lvert\cos(\omega/2)\rvert and ∣G(ω)∣=2∣sin⁡(ω/2)∣\lvert G(\omega)\rvert = \sqrt2\lvert\sin(\omega/2)\rvert; the wavelet zoo in Section 11 plots both for every wavelet. In one line: low-pass = scaling function = averages = approximation; high-pass = wavelet = differences = detail.

Four filters, one list of numbers

A library stores four filters per orthogonal wavelet, and every one of them is the single list hh rearranged. With LL taps:

Library nameIs
rec_lohh
dec_lohh reversed
rec_higg, with g[n]=(−1)nh[L−1−n]g[n] = (-1)^nh[L - 1 - n]
dec_higg reversed

For Daubechies-4 in PyWavelets: rec_lo =(0.4830,0.8365,0.2241,−0.1294)= (0.4830, 0.8365, 0.2241, -0.1294), dec_lo =(−0.1294,0.2241,0.8365,0.4830)= (-0.1294, 0.2241, 0.8365, 0.4830), rec_hi =(−0.1294,−0.2241,0.8365,−0.4830)= (-0.1294, -0.2241, 0.8365, -0.4830) and dec_hi =(−0.4830,0.8365,−0.2241,−0.1294)= (-0.4830, 0.8365, -0.2241, -0.1294) (all four checked). Why reversed? Mallat’s formula aj[k]=∑nhn−2k aj+1[n]a_j[k] = \sum_nh_{n-2k}\,a_{j+1}[n] is a correlation with hh. Libraries implement every filter as a convolution, and convolving with the reversed filter is correlating with the original. So analysis (“decomposition”) uses the reversed filters, and synthesis (“reconstruction”), which really is a convolution after inserting zeros, uses hh and gg as they are.

Two smaller differences you will meet. Section 5 used gk=(−1)kh1−kg_k = (-1)^kh_{1-k}, the convention of most books; libraries use (−1)nh[L−1−n](-1)^nh[L - 1 - n]. These are the same filter shifted by L−2L - 2 places, an even number, so the detail coefficients move by (L−2)/2(L - 2)/2 positions and nothing else changes. And some texts flip the sign of gg, which negates every detail coefficient. Magnitudes always agree.

PyWavelets, rebuilt in NumPy

Here is pywt.dwt in periodisation mode, written out with nothing but the numbers above:

import numpy as np, pywt

w = pywt.Wavelet('db2'); h = np.array(w.rec_lo); L = len(h)
g = np.array([(-1)**n * h[L - 1 - n] for n in range(L)])            # the alternating flip
x = np.array([9, 7, 3, 5, 6, 10, 2, 6.]); N = len(x)
s = L // 2 - 1                                                       # PyWavelets centres each window
a = np.array([sum(h[n] * x[(2*k + n - s) % N] for n in range(L)) for k in range(N // 2)])
d = np.array([sum(g[n] * x[(2*k + n - s) % N] for n in range(L)) for k in range(N // 2)])
cA, cD = pywt.dwt(x, 'db2', mode='periodization')
print(np.allclose(a, cA), np.allclose(d, cD))                        # True True

The textbook formula starts the kk-th window at sample 2k2k; PyWavelets starts it at 2k−(L/2−1)2k - (L/2 - 1), which centres it. I checked this offset for Haar, Daubechies-4, 6 and 8, Symlet-8 and Coiflet-12. Two correct implementations can therefore differ by a shift of the coefficients. Compare reconstructions or magnitudes, never raw arrays from different libraries. The default boundary mode, symmetric, is the same recipe on a mirrored signal: pad L−1L - 1 mirrored samples (edge repeated) on each side, convolve with dec_lo, and keep every second output starting at index LL. That yields ⌊(N+L−1)/2⌋\lfloor(N + L - 1)/2\rfloor coefficients per band, which is where the 5 (for N=8N = 8, L=4L = 4) and the 131 (for N=256N = 256, L=8L = 8) of Section 6 come from.

Where do the samples enter?

This is the gap that confused me longest. The theory says the finest coefficients are inner products, aJ[k]=⟨f,φJ,k⟩a_J[k] = \langle f, \varphi_{J,k}\rangle. The code is handed samples, x[k]=f(k2−J)x[k] = f(k2^{-J}), and treats them as the aJ[k]a_J[k]. Gilbert Strang calls this “the wavelet crime”. How wrong is it?

Proposition 5 (Samples are scaling coefficients, shifted)

Let c=∫u φ(u) duc = \int u\,\varphi(u)\,du be the centroid of φ\varphi (with ∫φ=1\int\varphi = 1). For smooth ff,

⟨f,φJ,k⟩=2−J/2 f ⁣(k+c2J)+O(2−5J/2).\langle f, \varphi_{J,k}\rangle = 2^{-J/2}\,f\!\left(\frac{k + c}{2^J}\right) + O\big(2^{-5J/2}\big).
Proof

Step 1. Substitute u=2Jx−ku = 2^Jx - k: ⟨f,φJ,k⟩=∫f(x) 2J/2φ(2Jx−k) dx=2−J/2∫f(k+u2J)φ(u) du\langle f, \varphi_{J,k}\rangle = \int f(x)\,2^{J/2}\varphi(2^Jx - k)\,dx = 2^{-J/2}\int f\big(\tfrac{k + u}{2^J}\big)\varphi(u)\,du.

Step 2. Taylor-expand ff around xc=(k+c)/2Jx_c = (k + c)/2^J: f(k+u2J)=f(xc)+f′(xc)u−c2J+O(2−2J)f\big(\tfrac{k + u}{2^J}\big) = f(x_c) + f'(x_c)\frac{u - c}{2^J} + O(2^{-2J}).

Step 3. Integrate against φ\varphi. ∫φ=1\int\varphi = 1, and ∫(u−c)φ(u) du=c−c=0\int(u - c)\varphi(u)\,du = c - c = 0 by the definition of cc. Only f(xc)f(x_c) survives, plus the remainder, which is 2−J/2⋅O(2−2J)2^{-J/2}\cdot O(2^{-2J}).

So a sample is, up to the factor 2−J/22^{-J/2}, a scaling coefficient at a shifted position. For Daubechies-4, c=0.634c = 0.634. Measured on a smooth signal at J=6J = 6: using the samples directly as scaling coefficients is 10% off; using samples taken at the shifted positions, 0.02% off. Coiflets are designed so that the higher moments of φ\varphi also vanish about its centre, which makes the shifted samples an excellent approximation.

Three consequences, worth stating plainly:

  1. Perfect reconstruction never depends on this. The discrete transform is an exact orthonormal change of basis of whatever vector you give it.
  2. Interpretation does. Reading the finest detail coefficients as samples of continuous wavelet coefficients is right only up to this shift and a slight smoothing.
  3. The factor 2J/22^{J/2} explains a common surprise: for a constant signal, each level of the orthonormal transform multiplies the approximation coefficients by 2\sqrt2 (Section 4’s 6⋅23/2=16.976\cdot2^{3/2} = 16.97).

Where is each coefficient?

A detail coefficient at library level ℓ\ell depends on a window of about (L−1)(2ℓ−1)+1(L - 1)(2^\ell - 1) + 1 samples, starting near 2ℓk2^\ell k (plus the library’s offset). To plot coefficients against time, place coefficient kk of level ℓ\ell near sample 2ℓ(k+12)2^\ell(k + \tfrac12). Daubechies filters are minimum phase: their energy is concentrated at the start of the filter, so their coefficients lag the features that cause them. Symlets were designed to reduce exactly this lag.

8. The lifting scheme

There is a second way to compute the same transform, faster, in place, and able to map integers to integers. Split the signal into even and odd samples, then alternately use each half to predict and update the other.

lifting: split, predict, update (Haar, in place)xevenoddpredictd = odd − evenupdatea = even + d / 2adto invert, run the ladder backwards with the signs flipped: every step undoes itself exactly
Example 2 (Haar by lifting)

Same eight numbers: evens (9,3,6,2)(9, 3, 6, 2), odds (7,5,10,6)(7, 5, 10, 6).

Predict each odd sample by its even neighbour and keep the error: d=odd−even=(−2,2,4,4)d = \text{odd} - \text{even} = (-2, 2, 4, 4).

Update the evens so that they become averages: a=even+d/2=(8,4,8,4)a = \text{even} + d/2 = (8, 4, 8, 4).

These are the averages of Section 4, and dd is −2-2 times the half-differences (lifting conventions differ by such factors). To invert, run the ladder backwards with flipped signs: even=a−d/2\text{even} = a - d/2, then odd=d+even\text{odd} = d + \text{even}. Each step is undone exactly, whatever was computed inside it.

That last remark is the point. Because each step adds a function of one half to the other, it can be inverted even if the function rounds. JPEG 2000’s lossless mode uses the 5/3 integer wavelet this way:

d[n]=x[2n+1]−⌊x[2n]+x[2n+2]2⌋,s[n]=x[2n]+⌊d[n−1]+d[n]+24⌋.d[n] = x[2n+1] - \Big\lfloor\frac{x[2n] + x[2n+2]}{2}\Big\rfloor, \qquad s[n] = x[2n] + \Big\lfloor\frac{d[n-1] + d[n] + 2}{4}\Big\rfloor .
Example 3 (5/3 by hand)

With symmetric extension (x[8]=x[6]x[8] = x[6], d[−1]=d[0]d[-1] = d[0]) on (9,7,3,5,6,10,2,6)(9, 7, 3, 5, 6, 10, 2, 6):

  • d[0]=7−⌊(9+3)/2⌋=1d[0] = 7 - \lfloor(9 + 3)/2\rfloor = 1, d[1]=5−⌊(3+6)/2⌋=1d[1] = 5 - \lfloor(3 + 6)/2\rfloor = 1, d[2]=10−⌊(6+2)/2⌋=6d[2] = 10 - \lfloor(6 + 2)/2\rfloor = 6, d[3]=6−⌊(2+2)/2⌋=4d[3] = 6 - \lfloor(2 + 2)/2\rfloor = 4;
  • s[0]=9+⌊(1+1+2)/4⌋=10s[0] = 9 + \lfloor(1 + 1 + 2)/4\rfloor = 10, s[1]=3+⌊(1+1+2)/4⌋=4s[1] = 3 + \lfloor(1 + 1 + 2)/4\rfloor = 4, s[2]=6+⌊(1+6+2)/4⌋=8s[2] = 6 + \lfloor(1 + 6 + 2)/4\rfloor = 8, s[3]=2+⌊(6+4+2)/4⌋=5s[3] = 2 + \lfloor(6 + 4 + 2)/4\rfloor = 5.

Integers in, integers out, and running the steps backwards recovers (9,7,3,5,6,10,2,6)(9, 7, 3, 5, 6, 10, 2, 6) exactly (checked).

Daubechies and Sweldens (1998) proved that every two-channel perfect-reconstruction filter bank with finite filters factors into such lifting steps, so lifting is not a special trick but a universal way to implement wavelets, typically with about half the arithmetic.

9. Perfect reconstruction

When do analysis filters h~,g~\tilde h, \tilde g followed by synthesis filters h,gh, g return the input exactly? The answer is two equations in the frequency domain, and the key is what “keep every second sample, then put zeros back” does to a spectrum.

Lemma 1 (Downsampling then upsampling)

If yy has transform Y(ω)Y(\omega), the sequence that keeps y[n]y[n] at even nn and puts 00 at odd nn has transform 12(Y(ω)+Y(ω+π))\tfrac12\big(Y(\omega) + Y(\omega + \pi)\big).

Proof

That sequence is y[n]⋅12(1+(−1)n)y[n]\cdot\tfrac12(1 + (-1)^n), and (−1)n=eiπn(-1)^n = e^{i\pi n}. Multiplying by eiπne^{i\pi n} shifts a spectrum by π\pi (the modulation rule of Part 1, in radians), so the transform is 12Y(ω)+12Y(ω+π)\tfrac12Y(\omega) + \tfrac12Y(\omega + \pi).

The term Y(ω+π)Y(\omega + \pi) is aliasing, the folding of Part 2. Push XX through both channels with the lemma, and the output is

X^(ω)=12[HH~+GG~](ω) X(ω)+12[H(ω)H~(ω+π)+G(ω)G~(ω+π)]X(ω+π).\hat X(\omega) = \tfrac12\big[H\tilde H + G\tilde G\big](\omega)\,X(\omega) + \tfrac12\big[H(\omega)\tilde H(\omega + \pi) + G(\omega)\tilde G(\omega + \pi)\big]X(\omega + \pi).

Perfect reconstruction needs the second bracket (aliasing) to vanish and the first (distortion) to equal 22. Each channel aliases on its own; the two aliases cancel.

Proposition 6 (Orthonormal filters reconstruct perfectly)

For real hh, the orthonormality condition ∑khkhk−2m=δm\sum_kh_kh_{k-2m} = \delta_m is equivalent to

∣H(ω)∣2+∣H(ω+π)∣2=2for all ω.\lvert H(\omega)\rvert^2 + \lvert H(\omega + \pi)\rvert^2 = 2 \quad\text{for all }\omega .

With H~=H‾\tilde H = \overline H, G~=G‾\tilde G = \overline G (analysis filters reversed in time) and G(ω)=−e−iω H(ω+π)‾G(\omega) = -e^{-i\omega}\,\overline{H(\omega + \pi)} (the alternating flip of Section 5), both conditions of perfect reconstruction hold.

Proof

Step 1. Autocorrelation. Let rm=∑khkhk−mr_m = \sum_kh_kh_{k-m}. Then ∣H(ω)∣2=H(ω)H(ω)‾=∑mrme−imω\lvert H(\omega)\rvert^2 = H(\omega)\overline{H(\omega)} = \sum_mr_me^{-im\omega}.

Step 2. Add the shifted copy. ∣H(ω)∣2+∣H(ω+π)∣2=∑mrm(1+(−1)m)e−imω=2∑mr2me−2imω\lvert H(\omega)\rvert^2 + \lvert H(\omega+\pi)\rvert^2 = \sum_mr_m\big(1 + (-1)^m\big)e^{-im\omega} = 2\sum_mr_{2m}e^{-2im\omega}: odd lags cancel.

Step 3. Equivalence. This equals the constant 22 for all ω\omega exactly when r2m=δmr_{2m} = \delta_m, by uniqueness of Fourier coefficients. That is the orthonormality condition.

Step 4. No aliasing. The alias bracket is H(ω)H(ω+π)‾+G(ω)G(ω+π)‾H(\omega)\overline{H(\omega + \pi)} + G(\omega)\overline{G(\omega + \pi)}. Using G(ω+π)=−e−i(ω+π)H(ω+2π)‾=e−iωH(ω)‾G(\omega + \pi) = -e^{-i(\omega + \pi)}\overline{H(\omega + 2\pi)} = e^{-i\omega}\overline{H(\omega)}, the second product is −e−iωH(ω+π)‾⋅eiωH(ω)=−H(ω)H(ω+π)‾-e^{-i\omega}\overline{H(\omega + \pi)}\cdot e^{i\omega}H(\omega) = -H(\omega)\overline{H(\omega + \pi)}, which cancels the first.

Step 5. No distortion. ∣H(ω)∣2+∣G(ω)∣2=∣H(ω)∣2+∣H(ω+π)∣2=2\lvert H(\omega)\rvert^2 + \lvert G(\omega)\rvert^2 = \lvert H(\omega)\rvert^2 + \lvert H(\omega+\pi)\rvert^2 = 2 by Step 3.

In words: the low-pass and the high-pass responses are mirror images about ω=π/2\omega = \pi/2 and their energies add to a constant. These are the “quadrature mirror” and “conjugate quadrature” filters of 1970s and 1980s speech coding, which turned out to be wavelets in disguise.

10. Vanishing moments: why wavelets make signals sparse

Haar is orthonormal, but it approximates smooth curves with staircases. What makes a better wavelet? Not smoothness of ψ\psi, primarily, but this:

Definition 3 (Vanishing moments)

ψ\psi has pp vanishing moments if ∫xmψ(x) dx=0\int x^m\psi(x)\,dx = 0 for m=0,1,…,p−1m = 0, 1, \ldots, p - 1.

A wavelet with pp vanishing moments is blind to polynomials of degree below pp. Where the signal is smooth it looks like a polynomial at fine scales, so the wavelet coefficients there are tiny.

Theorem 2 (Decay away from singularities)

Let ψ\psi have pp vanishing moments and support in an interval [0,S][0, S]. If ff has pp continuous derivatives on the support of ψj,k\psi_{j,k}, then

∣dj[k]∣=∣⟨f,ψj,k⟩∣  ≤  C 2−j(p+1/2)max⁡∣f(p)∣,C=1p!∫∣u∣p∣ψ(u)∣ du.\lvert d_j[k]\rvert = \lvert\langle f, \psi_{j,k}\rangle\rvert \;\le\; C\,2^{-j(p + 1/2)}\max\lvert f^{(p)}\rvert, \qquad C = \frac{1}{p!}\int\lvert u\rvert^p\lvert\psi(u)\rvert\,du .
Proof

Step 1. Taylor’s theorem around x0=k2−jx_0 = k2^{-j}: f(x)=P(x)+R(x)f(x) = P(x) + R(x), with PP a polynomial of degree p−1p - 1 and ∣R(x)∣≤max⁡∣f(p)∣p!∣x−x0∣p\lvert R(x)\rvert \le \frac{\max\lvert f^{(p)}\rvert}{p!}\lvert x - x_0\rvert^p.

Step 2. The polynomial is invisible. ⟨P,ψj,k⟩=0\langle P, \psi_{j,k}\rangle = 0: after the substitution u=2jx−ku = 2^jx - k, it is a combination of the moments ∫umψ(u)du\int u^m\psi(u)du with m<pm < p, all zero.

Step 3. Bound the remainder. ∣⟨R,ψj,k⟩∣≤∫∣R(x)∣ 2j/2∣ψ(2jx−k)∣ dx≤max⁡∣f(p)∣p!∫∣x−x0∣p 2j/2∣ψ(2jx−k)∣ dx\lvert\langle R, \psi_{j,k}\rangle\rvert \le \int\lvert R(x)\rvert\,2^{j/2}\lvert\psi(2^jx - k)\rvert\,dx \le \frac{\max\lvert f^{(p)}\rvert}{p!}\int\lvert x - x_0\rvert^p\,2^{j/2}\lvert\psi(2^jx - k)\rvert\,dx.

Step 4. Substitute u=2jx−ku = 2^jx - k, so x−x0=2−jux - x_0 = 2^{-j}u and dx=2−jdudx = 2^{-j}du: the integral becomes 2−jp⋅2j/2⋅2−j∫∣u∣p∣ψ(u)∣du=2−j(p+1/2)∫∣u∣p∣ψ(u)∣du2^{-jp}\cdot2^{j/2}\cdot2^{-j}\int\lvert u\rvert^p\lvert\psi(u)\rvert du = 2^{-j(p + 1/2)}\int\lvert u\rvert^p\lvert\psi(u)\rvert du.

So on smooth stretches, each finer level shrinks the coefficients by a factor 2p+1/22^{p + 1/2}. Measured on a smooth periodic signal: 2.832.83 per level for Haar (p=1p = 1), 5.665.66 for Daubechies-4 (p=2p = 2) and 22.622.6 for Daubechies-8 (p=4p = 4), matching 2p+1/22^{p+1/2} to three digits. At a jump no number of moments helps: the coefficients shrink only like 2−j/22^{-j/2}. Every jump leaves a trail of a few large coefficients down the scales, and everything else is small. That is sparsity, and it is why wavelets compress and denoise piecewise-smooth signals so well. More moments are not free, though: they need longer filters, and a longer filter overlaps each singularity at more positions. In the figure below, coefficients larger than 0.01 number 134 of 240 for Haar, 33 for Daubechies-4 and 42 for Daubechies-8. The best wavelet balances the smoothness of the signal between its singularities against how many singularities there are. (Compare Part 1: Fourier coefficients of a single jump decay like 1/k1/k everywhere in the spectrum. Wavelets confine the damage to the jump’s neighbourhood.)

Vanishing moments are easy to read off the filter.

Proposition 7 (Moments from the filter)

ψ\psi has pp vanishing moments if and only if H(ω)H(\omega) has a zero of order pp at ω=π\omega = \pi, that is, ∑k(−1)kkmhk=0\sum_k(-1)^kk^mh_k = 0 for m=0,…,p−1m = 0, \ldots, p - 1.

Proof

Step 1. Moments are derivatives at zero. By Part 1’s rule for multiplication by xx, ∫xmψ=(i2π)mψ^(m)(0)\int x^m\psi = \big(\tfrac{i}{2\pi}\big)^m\hat\psi^{(m)}(0). So pp vanishing moments means ψ^\hat\psi vanishes to order pp at 00.

Step 2. The wavelet equation in frequency. Transforming ψ(x)=2∑kgkφ(2x−k)\psi(x) = \sqrt2\sum_kg_k\varphi(2x - k) with the scaling and shift rules gives ψ^(ξ)=12G(πξ) φ^(ξ/2)\hat\psi(\xi) = \tfrac1{\sqrt2}G(\pi\xi)\,\hat\varphi(\xi/2), where G(ω)=∑kgke−ikωG(\omega) = \sum_kg_ke^{-ik\omega}.

Step 3. Only GG matters. φ^(0)=∫φ=1≠0\hat\varphi(0) = \int\varphi = 1 \ne 0, so by the product rule ψ^\hat\psi vanishes to order pp at 00 exactly when GG does at ω=0\omega = 0.

Step 4. Move to HH. ∣G(ω)∣=∣H(ω+π)∣\lvert G(\omega)\rvert = \lvert H(\omega + \pi)\rvert for the alternating flip, so GG has a zero of order pp at 00 exactly when HH has one at π\pi.

Step 5. Derivatives of HH at π\pi. H(m)(π)=∑khk(−ik)me−ikπ=(−i)m∑k(−1)kkmhkH^{(m)}(\pi) = \sum_kh_k(-ik)^me^{-ik\pi} = (-i)^m\sum_k(-1)^kk^mh_k.

11. Daubechies-4, by hand

Now design a wavelet. Ask for the shortest filter with two vanishing moments. Haar’s two taps can give only one, so try four, h=(h0,h1,h2,h3)h = (h_0, h_1, h_2, h_3).

Example 4 (Solving for the Daubechies-4 filter)

The conditions:

  1. h0+h1+h2+h3=2h_0 + h_1 + h_2 + h_3 = \sqrt2 (normalisation, Section 5);
  2. h0−h1+h2−h3=0h_0 - h_1 + h_2 - h_3 = 0 (first vanishing moment, m=0m = 0);
  3. −h1+2h2−3h3=0-h_1 + 2h_2 - 3h_3 = 0 (second, m=1m = 1; the k=0k = 0 term is zero);
  4. h0h2+h1h3=0h_0h_2 + h_1h_3 = 0 (orthogonal to its shift by two).

(The condition ∑hk2=1\sum h_k^2 = 1 follows: square 1 and 2 and add to get 2∑hk2+4(h0h2+h1h3)=22\sum h_k^2 + 4(h_0h_2 + h_1h_3) = 2, then use 4.)

Step 1. Adding and subtracting 1 and 2: h0+h2=h1+h3=sh_0 + h_2 = h_1 + h_3 = s, with s=1/2s = 1/\sqrt2.

Step 2. Write everything in terms of d=h3d = h_3: h1=s−dh_1 = s - d. Equation 3 becomes −(s−d)+2h2−3d=0-(s - d) + 2h_2 - 3d = 0, so h2=d+s/2h_2 = d + s/2, and then h0=s−h2=s/2−dh_0 = s - h_2 = s/2 - d.

Step 3. Substitute into 4: (s/2−d)(d+s/2)+(s−d)d=0(s/2 - d)(d + s/2) + (s - d)d = 0, which expands to s2/4−d2+sd−d2=0s^2/4 - d^2 + sd - d^2 = 0, that is, 2d2−sd−s2/4=02d^2 - sd - s^2/4 = 0.

Step 4. The quadratic formula gives d=s±s2+2s24=s(1±3)4d = \frac{s \pm\sqrt{s^2 + 2s^2}}{4} = \frac{s(1 \pm\sqrt3)}{4}. Take the minus sign (the plus sign gives the same filter reversed in time), and s=1/2s = 1/\sqrt2:

h=142(1+3,    3+3,    3−3,    1−3)≈(0.4830, 0.8365, 0.2241, −0.1294).h = \frac{1}{4\sqrt2}\big(1 + \sqrt3,\;\; 3 + \sqrt3,\;\; 3 - \sqrt3,\;\; 1 - \sqrt3\big) \approx (0.4830,\ 0.8365,\ 0.2241,\ -0.1294).

This is exactly PyWavelets’ db2 reconstruction low-pass filter (checked).

What scaling function does this filter define? Run the two-scale equation as an iteration, starting from a box: the cascade algorithm.

The cascade algorithm with the Daubechies-4 filter: starting from a box, each step replaces φ by √2 Σ hₖ φ(2x − k).

The result is jagged at every scale: continuous, but rough (its Hölder exponent is only about 0.55, so it has corners at every scale, Daubechies 1992). The zoo below runs the same iteration for other filters.

Question 4

Why are Daubechies wavelets so ugly?

Answer

Because beauty was not the specification. They are the shortest orthonormal filters with a given number of vanishing moments, which is what makes them fast and sparse. Smoothness comes along slowly, and symmetry not at all. For analysis that needs smooth shapes, use more taps (D8, Symlets) or give up orthonormality (next answer).

There is a theorem behind the lopsidedness. No real, compactly supported, orthonormal wavelet is symmetric or antisymmetric, except Haar (Daubechies, Ten Lectures, Theorem 8.1.4; we state it without proof). Symmetry matters for images, because an asymmetric filter shifts edges differently on their two sides. The way out is biorthogonal wavelets: use one filter pair to analyse and a different pair to synthesise. The perfect-reconstruction conditions of Section 9 still hold, orthonormality is given up, and both pairs can be symmetric. The Cohen–Daubechies–Feauveau 9/7 pair (four vanishing moments on each side) is the lossy wavelet of JPEG 2000, and 5/3 is the lossless one of Section 8.

12. Where the filter numbers come from

Every wavelet library contains tables of numbers like 0.4830,0.8365,0.2241,−0.12940.4830, 0.8365, 0.2241, -0.1294. Where do they come from? Except for Haar, the filter is designed first, from conditions on H(ω)H(\omega); the functions φ\varphi and ψ\psi are whatever the cascade algorithm then produces.

Haar is the exception: the function comes first. Start from the box φ=1[0,1)\varphi = \mathbf{1}_{[0,1)}. It is the sum of two half-width boxes, φ(x)=φ(2x)+φ(2x−1)\varphi(x) = \varphi(2x) + \varphi(2x - 1), so by the formula of Section 5, hk=⟨φ,2φ(2⋅−k)⟩=2⋅12=12h_k = \langle\varphi, \sqrt2\varphi(2\cdot{} - k)\rangle = \sqrt2\cdot\tfrac12 = \tfrac{1}{\sqrt2} for k=0,1k = 0, 1. The alternating flip gives g=(12,−12)g = (\tfrac1{\sqrt2}, -\tfrac1{\sqrt2}): sum and difference, scaled.

Daubechies wavelets: spectral factorisation. Section 11 solved four equations by hand. That does not scale to longer filters; Daubechies’ construction does.

Proposition 8 (The Daubechies construction)

A real filter with pp vanishing moments and orthonormal shifts has H(ω)=2(1+e−iω2)pL(ω)H(\omega) = \sqrt2\Big(\frac{1 + e^{-i\omega}}{2}\Big)^pL(\omega), where ∣L(ω)∣2=P(sin⁡2ω2)\lvert L(\omega)\rvert^2 = P\big(\sin^2\tfrac\omega2\big) for a polynomial PP with

(1−y)pP(y)+ypP(1−y)=1.(1 - y)^pP(y) + y^pP(1 - y) = 1 .

The lowest-degree solution is P(y)=∑k=0p−1(p−1+kk)ykP(y) = \sum_{k=0}^{p-1}\binom{p - 1 + k}{k}y^k: 11; 1+2y1 + 2y; 1+3y+6y21 + 3y + 6y^2; 1+4y+10y2+20y31 + 4y + 10y^2 + 20y^3; and so on.

Proof

Step 1. Vanishing moments are zeros at π\pi (Section 10): HH has a zero of order pp at ω=π\omega = \pi, so it factors as (1+e−iω2)pL(ω)\big(\frac{1 + e^{-i\omega}}{2}\big)^pL(\omega) times a constant; H(0)=2H(0) = \sqrt2 and the factor equals 11 at ω=0\omega = 0, so the constant is 2\sqrt2 and L(0)=1L(0) = 1.

Step 2. Magnitudes. ∣1+e−iω2∣2=cos⁡2ω2\big\lvert\frac{1 + e^{-i\omega}}{2}\big\rvert^2 = \cos^2\frac\omega2. Write y=sin⁡2ω2y = \sin^2\frac\omega2, so cos⁡2ω2=1−y\cos^2\frac\omega2 = 1 - y. Because hh is real, ∣L(ω)∣2\lvert L(\omega)\rvert^2 is a real, even trigonometric polynomial, hence a polynomial in cos⁡ω=1−2y\cos\omega = 1 - 2y, hence a polynomial P(y)P(y). So ∣H(ω)∣2=2(1−y)pP(y)\lvert H(\omega)\rvert^2 = 2(1 - y)^pP(y).

Step 3. Orthonormality. Replacing ω\omega by ω+π\omega + \pi swaps sin⁡2ω2\sin^2\frac\omega2 and cos⁡2ω2\cos^2\frac\omega2, that is, y↔1−yy \leftrightarrow 1 - y. The condition ∣H(ω)∣2+∣H(ω+π)∣2=2\lvert H(\omega)\rvert^2 + \lvert H(\omega + \pi)\rvert^2 = 2 of Section 9 becomes (1−y)pP(y)+ypP(1−y)=1(1 - y)^pP(y) + y^pP(1 - y) = 1.

Step 4. Check the solution for p=2p = 2. (1−y)2(1+2y)+y2(3−2y)=(1−2y+y2)(1+2y)+3y2−2y3=1−3y2+2y3+3y2−2y3=1(1 - y)^2(1 + 2y) + y^2(3 - 2y) = (1 - 2y + y^2)(1 + 2y) + 3y^2 - 2y^3 = 1 - 3y^2 + 2y^3 + 3y^2 - 2y^3 = 1. (For general pp this is Bézout’s theorem; see Daubechies, Ten Lectures, §6.1.)

The last step, spectral factorisation, finds LL from ∣L∣2\lvert L\rvert^2. For p=2p = 2: P=1+2y=1+(1−cos⁡ω)=2−cos⁡ωP = 1 + 2y = 1 + (1 - \cos\omega) = 2 - \cos\omega. Try L(ω)=a+be−iωL(\omega) = a + be^{-i\omega}: then ∣L∣2=a2+b2+2abcos⁡ω\lvert L\rvert^2 = a^2 + b^2 + 2ab\cos\omega, so a2+b2=2a^2 + b^2 = 2 and 2ab=−12ab = -1, with a+b=L(0)=1a + b = L(0) = 1. The solution is a=1+32a = \frac{1 + \sqrt3}{2}, b=1−32b = \frac{1 - \sqrt3}{2} (swapping them reverses the filter). Multiply out 2(1+z2)2(a+bz)\sqrt2\big(\frac{1 + z}{2}\big)^2(a + bz) with z=e−iωz = e^{-i\omega}: the coefficients are 24(a, 2a+b, a+2b, b)=142(1+3, 3+3, 3−3, 1−3)\frac{\sqrt2}{4}\big(a,\ 2a + b,\ a + 2b,\ b\big) = \frac{1}{4\sqrt2}\big(1 + \sqrt3,\ 3 + \sqrt3,\ 3 - \sqrt3,\ 1 - \sqrt3\big): the same D4 filter as Section 11, by a second route.

For larger pp, ∣L∣2\lvert L\rvert^2 has p−1p - 1 pairs of roots rr and 1/rˉ1/\bar r, and building LL means choosing one root from each pair. That choice is what separates the families:

  • Daubechies (dbN): every root inside the unit circle (minimum phase). The energy is front-loaded and the filter is as asymmetric as possible.
  • Symlets (symN): the combination of roots whose filter is closest to symmetric (linear phase). Same length, same moments, less lag.
  • Coiflets (coifN): a different polynomial, chosen so that φ\varphi also has vanishing moments, which is what makes samples good scaling coefficients (Section 7).
  • Biorthogonal (biorN.M, CDF): split the factors between two different filters, analysis and synthesis, so that both can be symmetric. The 9/7 and 5/3 filters of JPEG 2000 come from this.

The construction fits in a few lines, and it reproduces the library’s tables:

from math import comb
import numpy as np, pywt

def daubechies(p):
    """Low-pass filter with p vanishing moments, by spectral factorisation."""
    y = np.poly1d([-0.25, 0.5, -0.25])                                  # z * sin^2(w/2), z = e^{iw}
    Q = sum(comb(p - 1 + k, k) * y**k * np.poly1d([1] + [0] * (p - 1 - k)) for k in range(p))   # z^(p-1) P(y)
    r = Q.r                                                             # roots come in pairs r, 1/r
    L = np.real(np.poly(r[np.abs(r) < 1]))                              # keep those inside the unit circle
    L /= L.sum()                                                        # so that L = 1 at w = 0
    return np.sqrt(2) * np.convolve((np.poly1d([1, 1])**p).c / 2**p, L) # sqrt2 ((1 + z)/2)^p L(z)

for p in (2, 3, 4):
    print(np.allclose(daubechies(p), pywt.Wavelet(f'db{p}').rec_lo))     # True True True

Libraries ship these numbers as precomputed tables (PyWavelets) or compute them with the same factorisation (MATLAB’s dbaux). Nothing about them is arbitrary: each table is the solution of a short list of conditions, normalisation, orthogonality and vanishing moments, plus a choice of roots.

13. Denoising

Now the payoff. Observe y=f+σzy = f + \sigma z, with ff piecewise smooth and zz white Gaussian noise. Three facts make wavelet denoising work, and each has a short proof.

Proposition 9 (Three facts behind wavelet denoising)

  1. Noise stays white. If W\mathbf{W} is an orthonormal transform and z∼N(0,I)\boldsymbol{z} \sim \mathcal{N}(\mathbf{0}, \mathbf{I}), then Wz∼N(0,I)\mathbf{W}\boldsymbol{z} \sim \mathcal{N}(\mathbf{0}, \mathbf{I}): in wavelet coordinates the noise is spread evenly over every coefficient.
  2. Soft thresholding is the best shrinkage under an ℓ1\ell^1 penalty. ηλ(y)=sign⁡(y)max⁡(∣y∣−λ,0)\eta_\lambda(y) = \operatorname{sign}(y)\max(\lvert y\rvert - \lambda, 0) minimises 12(θ−y)2+λ∣θ∣\tfrac12(\theta - y)^2 + \lambda\lvert\theta\rvert over θ\theta.
  3. The universal threshold catches pure noise. For NN independent N(0,σ2)\mathcal{N}(0, \sigma^2) values, P(max⁡i∣zi∣>σ2ln⁡N)≤2/π /2ln⁡N→0P\big(\max_i\lvert z_i\rvert > \sigma\sqrt{2\ln N}\big) \le \sqrt{2/\pi}\,/\sqrt{2\ln N} \to 0.
Proof

Fact 1. A linear map of a Gaussian vector is Gaussian, with covariance WIW⊤=WW⊤=I\mathbf{W}\mathbf{I}\mathbf{W}^{\top} = \mathbf{W}\mathbf{W}^{\top} = \mathbf{I} because W\mathbf{W} is orthonormal.

Fact 2. For θ>0\theta > 0 the objective is a parabola with minimum at θ=y−λ\theta = y - \lambda, valid if y>λy > \lambda. For θ<0\theta < 0 the minimum is at y+λy + \lambda, valid if y<−λy < -\lambda. Otherwise (∣y∣≤λ\lvert y\rvert \le \lambda) the objective increases away from θ=0\theta = 0 on both sides, since its slopes there are ±λ−y\pm\lambda - y with the right signs, so θ=0\theta = 0.

Fact 3, a Gaussian tail bound. For t>0t > 0, ∫t∞e−u2/2du≤∫t∞ute−u2/2du=e−t2/2t\int_t^\infty e^{-u^2/2}du \le \int_t^\infty\frac ut e^{-u^2/2}du = \frac{e^{-t^2/2}}{t}, so P(∣z∣>tσ)≤2/π e−t2/2/tP(\lvert z\rvert > t\sigma) \le \sqrt{2/\pi}\,e^{-t^2/2}/t.

Fact 3, the union bound. P(max⁡i∣zi∣>tσ)≤N2/π e−t2/2/tP(\max_i\lvert z_i\rvert > t\sigma) \le N\sqrt{2/\pi}\,e^{-t^2/2}/t. With t=2ln⁡Nt = \sqrt{2\ln N}, e−t2/2=1/Ne^{-t^2/2} = 1/N, leaving 2/π/2ln⁡N\sqrt{2/\pi}/\sqrt{2\ln N}.

The bound goes to zero, but slowly: measured on 4,000 trials, pure noise of length 1,024 exceeds the threshold 3.72σ3.72\sigma somewhere 19% of the time (the bound says at most 21%). In practice the occasional surviving noise coefficient is small. The noise level itself is estimated from the finest details, which are mostly noise: σ^=median⁡(∣d1∣)/0.6745\hat\sigma = \operatorname{median}(\lvert d_1\rvert)/0.6745, since 0.67450.6745 is the median of ∣N(0,1)∣\lvert\mathcal{N}(0, 1)\rvert (measured on 10510^5 samples: σ^=1.0005\hat\sigma = 1.0005).

The recipe (Donoho and Johnstone, 1994): transform, estimate σ\sigma, soft-threshold every detail coefficient at σ^2ln⁡N\hat\sigma\sqrt{2\ln N}, transform back.

A Fourier low-pass filter (the Gaussian blur) treats every place alike, so it must choose one width that suits the smooth wave, the jumps and the spike at once. The figure gives it an unfair advantage, the best possible width chosen by looking at the true signal. The wavelet methods see only the noisy data. Measured (RMS error for soft thresholding, hard thresholding and the oracle blur):

Noise σSoftHardBest blur
0.050.0460.0230.038
0.120.0870.0560.064
0.200.1180.0820.083
0.400.1610.1480.116

Hard thresholding beats even the oracle blur at low and moderate noise, keeping the jumps and the spike sharp. Soft thresholding at the universal threshold, the classical “VisuShrink”, is the most conservative: the threshold is designed so that essentially no noise survives, and soft thresholding also pulls every surviving coefficient toward zero, so it over-smooths. At heavy noise the jumps are buried, and an ideally tuned blur wins. Data-driven thresholds, such as Stein’s unbiased risk estimate per level (“SureShrink”; Donoho and Johnstone, 1995), close most of the gap for soft thresholding. Part 4 repeats the comparison on images.

14. What the discrete transform gets wrong

It is not shift-invariant. The discrete transform samples positions on a dyadic grid, so moving the input by one sample does not move the coefficients by one place: they change. Try the shift slider in the vanishing-moments figure and watch the finest-level energy change. Even the decay measurement at a jump shows it: for Haar, the ratio between successive levels at one jump came out 0.71,2.12,0.71,3.060.71, 2.12, 0.71, 3.06 instead of a steady 1.411.41, because the answer depends on where the jump sits relative to the grid. The cause is the one from Part 2: every level downsamples, and each band aliases a little.

Three remedies, in increasing sophistication:

  • Cycle spinning (Coifman and Donoho, 1995): denoise every shifted copy, shift back, average. Simple and effective.
  • The undecimated (stationary) transform: skip the downsampling and dilate the filters instead. Exactly shift-invariant, at J+1J + 1 times the storage for JJ levels.
  • The dual-tree complex wavelet transform (Kingsbury): two real trees whose filters form approximate Hilbert-transform pairs, giving complex coefficients whose magnitudes barely change under shifts, at only twice the storage in 1-D.

It knows nothing about direction. In 1-D there is none to know. In 2-D, Part 4 shows that the separable construction sees only horizontal, vertical and a “diagonal” that cannot tell / from \.

15. Images, briefly

Everything here extends to images by doing the 1-D transform along rows and then along columns. Each level then produces one approximation and three detail images. Part 4 derives why three, does a 2×22\times2 and a 4×44\times4 example by hand and an 8×88\times8 one live, settles the confusing naming of the bands, and shows compression, denoising and the shift problem on my sketches.

16. Choosing a tool

which transform? ask what you need to knowwhich frequencies, over the whole signal?Fourier (FFT)stationary signals, filters, convolutionwhich frequencies, when, at one resolution?STFT / spectrogramspeech, audio, steady textureswhich scales, where, in fine detail?continuous waveletstransients, chirps, analysis by eyea compact, invertible, fast representation?discrete waveletscompression, denoising, PDE solversa shift-stable, oriented representation of images?pyramids, complex waveletstexture, motion, features

17. Implementation and caveats

import numpy as np, pywt

x = np.array([9, 7, 3, 5, 6, 10, 2, 6], float)
cA, cD = pywt.dwt(x, 'haar')                 # one level, orthonormal: (a + b)/sqrt2, (a - b)/sqrt2
print(cA / np.sqrt(2), cD / np.sqrt(2))      # [8. 4. 8. 4.] [ 1. -1. -2. -2.]  -- Section 4
coeffs = pywt.wavedec(x, 'haar', level=3)    # [cA3, cD3, cD2, cD1]: coarsest first (Section 7)
print(pywt.waverec(coeffs, 'haar'))          # [ 9.  7.  3.  5.  6. 10.  2.  6.]
print(pywt.dwt_coeff_len(256, pywt.Wavelet('db4').dec_len, 'symmetric'))   # 131, not 128

Run as written; the outputs are in the comments. The traps:

  1. Levels count in opposite directions. A library’s level ℓ\ell is this post’s j=J−ℓj = J - \ell, and Mallat’s jj is this post’s −j-j; cD1 is the finest detail (Section 7).
  2. Two correct implementations can disagree by a shift. PyWavelets centres each filter window; a textbook formula starts it at 2k2k. For D4 the coefficients differ by one position. Compare reconstructions or magnitudes, never raw arrays across libraries (Section 7).
  3. Samples are not scaling coefficients. Feeding samples in place of ⟨f,φJ,k⟩\langle f, \varphi_{J,k}\rangle shifts everything by the centroid of φ\varphi (0.634 samples for D4). Reconstruction is unaffected; interpretation is not (Section 7).
  4. Coefficient counts. The default boundary mode is symmetric extension, which returns more coefficients than samples. Use mode='periodization' for exactly N/2N/2 per level (and accept the wrap-around jump).
  5. Which filter is which. rec_lo is the hh of this post, dec_lo is hh reversed, rec_hi is gg and dec_hi is gg reversed. Books and libraries differ in the sign and shift of gg; magnitudes do not (Section 7).
  6. Order of the output. wavedec returns the coarsest approximation first, then details from coarse to fine.
  7. How many levels. Each level needs the signal to be longer than the filter; pywt.dwt_max_level(256, 'db4') is 5 while Haar allows 8. Past that, the coefficients are dominated by boundary effects.
  8. Thresholding the approximation by mistake. Threshold the details only; the coarsest approximation carries the signal’s mean and slow trends.
  9. Continuous-transform scales are not frequencies. Convert with the wavelet’s centre frequency (pywt.scale2frequency), and check whether the library normalises for energy (a−1/2a^{-1/2}, as here) or for amplitude (a−1a^{-1}), which changes how the scalogram’s rows compare.
  10. Edges of a scalogram. Near the ends of the record, long wavelets hang off the data; that region (the “cone of influence”) should not be interpreted.
  11. Shift sensitivity. If features must not depend on alignment, use the stationary transform, cycle spinning or complex wavelets.
  12. Odd lengths. With periodisation, a level halves the length only if it is even; PyWavelets rounds up (9, 10 and 11 samples give 5, 5 and 6 coefficients per band). For several levels, pad to a multiple of 2levels2^{\text{levels}}.
  13. Approximations grow. Each orthonormal level multiplies a constant by 2\sqrt2 in 1-D (a constant 1 becomes 23/2=2.832^{3/2} = 2.83 after three Haar levels) and by 22 in 2-D. Divide by that gain before displaying an approximation as an image.

18. Where it is used, and what is open

Use wavelets whenPrefer something else when
the signal is piecewise smooth: edges, transients, spikesit is stationary and periodic: Fourier is simpler and exact
you need a compact, invertible, fast representationyou need shift-invariant features: use undecimated or complex transforms
events of very different durations matter at onceyou need fine direction in images: steerable or complex wavelets (Part 4)

In current work. Image compression (JPEG 2000, used in digital cinema and medical imaging); denoising in imaging, spectroscopy and astronomy; transient detection in ECG, EEG and seismic records; Morlet scalograms as the standard time–frequency tool in neuroscience; multiscale solvers and adaptive grids for PDEs; the scattering transform (Bruna and Mallat, 2013), a fixed cascade of wavelet transforms and moduli whose features are stable to small deformations and which explains part of why convolutional networks work; wavelet neural operators for learning PDE solution maps (Tripura and Chakraborty, 2023); and random wavelet features for scalable non-stationary Gaussian processes.

Open questions.

  1. Learned filter banks with guarantees. Learning the filters of a wavelet transform from data is easy; keeping perfect reconstruction, stability and vanishing moments while doing so is not. Lifting gives a route. Which guarantees matter for downstream learning?
  2. Wavelets off the line. On graphs, meshes and point clouds there is no dilation, and graph wavelets are defined through the spectrum of a Laplacian. Which properties of the Euclidean theory (fast transforms, vanishing moments, sparsity theorems) survive, and at what cost?
  3. Adaptive bases. Wavelet packets and best-basis search pick a tiling of the time–frequency plane per signal. When does that adaptivity pay off inside a learning pipeline, compared with a fixed transform followed by a learned model?
  4. Resolution independence in neural operators. Wavelet-based operators promise to work across discretisations. Under which conditions on the data and the architecture can this be proved, rather than observed?
Note (Shoulders to stand on)
  • Artem Kirsanov, Wavelets: a mathematical microscope (YouTube, 2022). The best visual introduction to the continuous wavelet transform.
  • Eric Stollnitz, Tony DeRose, David Salesin, Wavelets for Computer Graphics: A Primer (IEEE CG&A, 1995). Haar by hand in 1-D and 2-D; our eight numbers start from their example.
  • Gilbert Strang, Truong Nguyen, Wavelets and Filter Banks (Wellesley-Cambridge, 1996). The filter-bank view that makes implementation obvious.
  • Stéphane Mallat, A Wavelet Tour of Signal Processing (3rd ed., Academic Press, 2008). The definitive reference, for the full theory.

Self-check

Question 5

A wavelet has 3 vanishing moments. On a smooth stretch, by what factor do its detail coefficients shrink from one level to the next finer one?

Answer

2p+1/2=23.5≈11.32^{p + 1/2} = 2^{3.5} \approx 11.3 (Section 10).

Question 6

Show that the Haar filter satisfies ∣H(ω)∣2+∣H(ω+π)∣2=2\lvert H(\omega)\rvert^2 + \lvert H(\omega + \pi)\rvert^2 = 2.

Answer

H(ω)=12(1+e−iω)H(\omega) = \frac{1}{\sqrt2}(1 + e^{-i\omega}), so ∣H(ω)∣2=12∣1+e−iω∣2=1+cos⁡ω\lvert H(\omega)\rvert^2 = \tfrac12\lvert1 + e^{-i\omega}\rvert^2 = 1 + \cos\omega. Then ∣H(ω+π)∣2=1−cos⁡ω\lvert H(\omega + \pi)\rvert^2 = 1 - \cos\omega, and the sum is 22.

Question 7

How many vanishing moments does Haar have, and why not two?

Answer

One: ∫ψ=12−12=0\int\psi = \tfrac12 - \tfrac12 = 0, but ∫xψ(x)dx=∫01/2x dx−∫1/21x dx=18−38=−14≠0\int x\psi(x)dx = \int_0^{1/2}x\,dx - \int_{1/2}^1x\,dx = \tfrac18 - \tfrac38 = -\tfrac14 \ne 0. In filter terms, ∑(−1)kkhk=−h1=−12≠0\sum(-1)^kkh_k = -h_1 = -\tfrac1{\sqrt2} \ne 0.

Question 8

A signal of N=4096N = 4096 samples has noise σ=0.1\sigma = 0.1. What is the universal threshold?

Answer

σ2ln⁡N=0.12ln⁡4096≈0.1×4.08=0.408\sigma\sqrt{2\ln N} = 0.1\sqrt{2\ln 4096} \approx 0.1 \times 4.08 = 0.408.

Question 9

Why does the fast wavelet transform cost O(N)O(N) while the FFT costs O(Nlog⁡N)O(N\log N)?

Answer

Each level of the wavelet transform works only on the approximation of the previous level, which halves every time: N+N/2+N/4+⋯<2NN + N/2 + N/4 + \cdots < 2N samples in total, each touched by a fixed number of filter taps. The FFT does NN work at every one of its log⁡2N\log_2N stages (Part 2, Section 8).

Question 10

Why can the lifting steps of the 5/3 transform round to integers and still be inverted exactly?

Answer

Each step adds to one half of the samples a function of the other half only. Undoing it subtracts the same function of the same, unchanged half, so whatever the function computed, including any rounding, is subtracted exactly.

References

  • A. Haar. Zur Theorie der orthogonalen Funktionensysteme. Mathematische Annalen 69, 1910.
  • A. Grossmann, J. Morlet. Decomposition of Hardy functions into square integrable wavelets of constant shape. SIAM Journal on Mathematical Analysis 15(4), 1984.
  • S. Mallat. A theory for multiresolution signal decomposition: the wavelet representation. IEEE TPAMI 11(7), 1989.
  • I. Daubechies. Orthonormal bases of compactly supported wavelets. Communications on Pure and Applied Mathematics 41(7), 1988; and Ten Lectures on Wavelets, SIAM, 1992.
  • A. Cohen, I. Daubechies, J.-C. Feauveau. Biorthogonal bases of compactly supported wavelets. CPAM 45(5), 1992.
  • W. Sweldens. The lifting scheme: a custom-design construction of biorthogonal wavelets. Applied and Computational Harmonic Analysis 3(2), 1996.
  • I. Daubechies, W. Sweldens. Factoring wavelet transforms into lifting steps. Journal of Fourier Analysis and Applications 4(3), 1998.
  • D. L. Donoho, I. M. Johnstone. Ideal spatial adaptation by wavelet shrinkage. Biometrika 81(3), 1994.
  • D. L. Donoho, I. M. Johnstone. Adapting to unknown smoothness via wavelet shrinkage. JASA 90(432), 1995.
  • R. R. Coifman, D. L. Donoho. Translation-invariant de-noising. In Wavelets and Statistics, Springer, 1995.
  • I. W. Selesnick, R. G. Baraniuk, N. G. Kingsbury. The dual-tree complex wavelet transform. IEEE Signal Processing Magazine 22(6), 2005.
  • J. Bruna, S. Mallat. Invariant scattering convolution networks. IEEE TPAMI 35(8), 2013.
  • T. Tripura, S. Chakraborty. Wavelet neural operator for solving parametric partial differential equations in computational mechanics problems. CMAME 404, 2023.
  • G. Lee, R. Gommers, F. Wasilewski, K. Wohlfahrt, A. O’Leary. PyWavelets: a Python package for wavelet analysis. JOSS 4(36), 2019.
  • E. J. Stollnitz, T. D. DeRose, D. H. Salesin. Wavelets for computer graphics: a primer, part 1. IEEE Computer Graphics and Applications 15(3), 1995.
  • G. Strang, T. Nguyen. Wavelets and Filter Banks. Wellesley-Cambridge Press, 1996.
  • S. Mallat. A Wavelet Tour of Signal Processing: The Sparse Way. 3rd ed., Academic Press, 2008.