Algorithm Search⌘K

Proofs: Haar wavelet transform

These are the human proofs of Haar wavelet transform, at full length.

HAAR computes the Haar coefficients, and INVERSE-HAAR inverts it full

This proof shows: correctness, complexity.

Statement. Let n=2tn = 2^t with t≥0t \ge 0, let xin∈Rnx^{\mathrm{in}} \in \mathbb{R}^n, and let c≠0c \ne 0. After HAAR(x,c)(x, c), x[0]=(2c)t⋅μx[0] = (2c)^t \cdot \mu where μ\mu is the mean of xinx^{\mathrm{in}}, and for 0≤j<t0 \le j < t and 0≤k<2j0 \le k < 2^j,

x[2j+k]=(2c)t−j⋅μj,k−−μj,k+2,x[2^j + k] = (2c)^{t-j} \cdot \frac{\mu^-_{j,k} - \mu^+_{j,k}}{2},

where μj,k−\mu^-_{j,k} and μj,k+\mu^+_{j,k} are the means of xinx^{\mathrm{in}} on the left half Bj,k−B^-_{j,k} and the right half Bj,k+B^+_{j,k} of the block Bj,k=[kLj,(k+1)Lj)B_{j,k} = [kL_j, (k+1)L_j), Lj=n/2jL_j = n/2^j. INVERSE-HAAR(x,c)(x, c) then gives back xinx^{\mathrm{in}}. With c=1/2c = 1/2 these are the coefficients by averages. With c=1/2c = 1/\sqrt 2, (2c)t−j=Lj(2c)^{t-j} = \sqrt{L_j} and (2c)t=n(2c)^t = \sqrt n, so the outputs are ⟨xin,φ⟩\langle x^{\mathrm{in}}, \varphi \rangle and ⟨xin,ψj,k⟩\langle x^{\mathrm{in}}, \psi_{j,k} \rangle: indeed Lj(μ−−μ+)/2=(∑B−x−∑B+x)/Lj\sqrt{L_j}(\mu^- - \mu^+)/2 = (\sum_{B^-} x - \sum_{B^+} x)/\sqrt{L_j} because each half has Lj/2L_j/2 entries, and nμ=∑x/n\sqrt n \mu = \sum x / \sqrt n.

Setup. Write M(i,L)M(i, L) for the mean of xinx^{\mathrm{in}} on [iL,(i+1)L)[iL, (i+1)L). The pass number q=0,1,…q = 0, 1, \ldots starts when line L2 tests m=n/2qm = n/2^q; let L=2qL = 2^q.

Invariant (I). When line L2 tests m=n/2qm = n/2^q:

  1. x[i]=(2c)qM(i,2q)x[i] = (2c)^q M(i, 2^q) for 0≤i<m0 \le i < m;
  2. for each level jj with 2j≥m2^j \ge m (that is, j≥t−qj \ge t - q) and each 0≤k<2j0 \le k < 2^j, x[2j+k]x[2^j + k] has the value of the statement.

Initialization. q=0q = 0, m=nm = n, M(i,1)=xiinM(i, 1) = x^{\mathrm{in}}_i and (2c)0=1(2c)^0 = 1, so part 1 holds. No level j<tj < t has 2j≥n2^j \ge n, so part 2 is empty.

Maintenance. Assume (I) at m≥2m \ge 2, and let h=m/2h = m/2, so h=2t−q−1h = 2^{t-q-1} and the new pass makes level j=t−q−1j = t - q - 1. For 0≤i<h0 \le i < h, lines L4 and L5 read x[2i]x[2i] and x[2i+1]x[2i+1], which by part 1 are (2c)qM(2i,2q)(2c)^q M(2i, 2^q) and (2c)qM(2i+1,2q)(2c)^q M(2i+1, 2^q). These positions are below mm, and lines L4 and L5 write only into TT, so every read sees the old values. The two blocks [2i⋅2q,(2i+1)2q)[2i \cdot 2^q, (2i+1)2^q) and [(2i+1)2q,(2i+2)2q)[(2i+1)2^q, (2i+2)2^q) are the halves of the block [i⋅2q+1,(i+1)2q+1)[i \cdot 2^{q+1}, (i+1) 2^{q+1}), which is Bj,iB_{j,i} since Lj=n/2j=2q+1L_j = n/2^j = 2^{q+1}. The mean of a block is the average of the means of its halves, so

T[i]=c⋅(2c)q(M(2i,2q)+M(2i+1,2q))=(2c)q+1M(i,2q+1),T[i] = c \cdot (2c)^q \bigl(M(2i, 2^q) + M(2i+1, 2^q)\bigr) = (2c)^{q+1} M(i, 2^{q+1}),
T[h+i]=c⋅(2c)q(μj,i−−μj,i+)=(2c)t−j⋅μj,i−−μj,i+2,T[h + i] = c \cdot (2c)^q \bigl(\mu^-_{j,i} - \mu^+_{j,i}\bigr) = (2c)^{t-j} \cdot \frac{\mu^-_{j,i} - \mu^+_{j,i}}{2},

using q+1=t−jq + 1 = t - j. Line L6 copies T[0…m−1]T[0 \ldots m-1] into xx and does not touch x[m…n−1]x[m \ldots n-1], which keeps the old part 2 for the levels j′>jj' > j. The new part 1 is the first line for i<hi < h, and the new part 2 adds level jj at the positions 2j+i=h+i2^j + i = h + i. Line L7 sets m=h=n/2q+1m = h = n/2^{q+1}, so (I) holds for q+1q + 1.

Termination. mm halves in each pass and starts at n=2tn = 2^t, so after exactly tt passes m=1m = 1 and the loop stops. Then part 1 gives x[0]=(2c)tM(0,n)=(2c)tμx[0] = (2c)^t M(0, n) = (2c)^t \mu, and part 2 covers all levels 0≤j<t0 \le j < t. This proves the claim about HAAR.

Inverse. We show that the pass of INVERSE-HAAR with length mm undoes the pass of HAAR with length mm. That pass of HAAR maps the old front v[0…m−1]v[0 \ldots m-1] to si=c(v2i+v2i+1)s_i = c(v_{2i} + v_{2i+1}) at ii and di=c(v2i−v2i+1)d_i = c(v_{2i} - v_{2i+1}) at h+ih + i. Then si+di=2c v2is_i + d_i = 2c\, v_{2i} and si−di=2c v2i+1s_i - d_i = 2c\, v_{2i+1}, so lines L11 and L12 recover v2iv_{2i} and v2i+1v_{2i+1} (here c≠0c \ne 0 is used), and line L13 writes them back to the front, leaving y[m…n−1]y[m \ldots n-1] unchanged. HAAR runs the passes with m=n,n/2,…,2m = n, n/2, \ldots, 2, and INVERSE-HAAR runs the inverse passes with m=2,4,…,nm = 2, 4, \ldots, n, the reverse order. A composition of maps undone in reverse order is the identity, so INVERSE-HAAR gives back xinx^{\mathrm{in}}. For n=1n = 1 both loops do nothing, which is correct since then the transform is the identity.

Operation count. The pass with length mm does m/2m/2 additions (line L4), m/2m/2 subtractions (line L5) and mm multiplications by cc. The passes have m=2t,2t−1,…,2m = 2^t, 2^{t-1}, \ldots, 2, and ∑q=1t2q=2t+1−2=2n−2\sum_{q=1}^{t} 2^q = 2^{t+1} - 2 = 2n - 2. So HAAR does exactly 2n−22n - 2 additions and subtractions and 2n−22n - 2 multiplications, for every input. The number of steps of all lines is proportional to this sum plus t+1t + 1 loop tests, so the time is Θ(n)\Theta(n) in the best and the worst case. The buffer TT has nn entries, and no other storage grows with nn, so the extra space is Θ(n)\Theta(n). ■\blacksquare

Back to Haar wavelet transform