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=2t with t≥0, let xin∈Rn, and let c=0. After HAAR(x,c), x[0]=(2c)t⋅μ where μ is the mean of xin, and for 0≤j<t and 0≤k<2j,
x[2j+k]=(2c)t−j⋅2μj,k−−μj,k+,
where μj,k− and μj,k+ are the means of xin on the left half Bj,k− and the right half Bj,k+ of the block Bj,k=[kLj,(k+1)Lj), Lj=n/2j. INVERSE-HAAR(x,c) then gives back xin. With c=1/2 these are the coefficients by averages. With c=1/2, (2c)t−j=Lj and (2c)t=n, so the outputs are ⟨xin,φ⟩ and ⟨xin,ψj,k⟩: indeed Lj(μ−−μ+)/2=(∑B−x−∑B+x)/Lj because each half has Lj/2 entries, and nμ=∑x/n.
Setup. Write M(i,L) for the mean of xin on [iL,(i+1)L). The pass number q=0,1,… starts when line L2 tests m=n/2q; let L=2q.
Invariant (I). When line L2 tests m=n/2q:
x[i]=(2c)qM(i,2q) for 0≤i<m;
for each level j with 2j≥m (that is, j≥t−q) and each 0≤k<2j, x[2j+k] has the value of the statement.
Initialization.q=0, m=n, M(i,1)=xiin and (2c)0=1, so part 1 holds. No level j<t has 2j≥n, so part 2 is empty.
Maintenance. Assume (I) at m≥2, and let h=m/2, so h=2t−q−1 and the new pass makes level j=t−q−1. For 0≤i<h, lines L4 and L5 read x[2i] and x[2i+1], which by part 1 are (2c)qM(2i,2q) and (2c)qM(2i+1,2q). These positions are below m, and lines L4 and L5 write only into T, so every read sees the old values. The two blocks [2i⋅2q,(2i+1)2q) and [(2i+1)2q,(2i+2)2q) are the halves of the block [i⋅2q+1,(i+1)2q+1), which is Bj,i since Lj=n/2j=2q+1. The mean of a block is the average of the means of its halves, so
using q+1=t−j. Line L6 copies T[0…m−1] into x and does not touch x[m…n−1], which keeps the old part 2 for the levels j′>j. The new part 1 is the first line for i<h, and the new part 2 adds level j at the positions 2j+i=h+i. Line L7 sets m=h=n/2q+1, so (I) holds for q+1.
Termination.m halves in each pass and starts at n=2t, so after exactly t passes m=1 and the loop stops. Then part 1 gives x[0]=(2c)tM(0,n)=(2c)tμ, and part 2 covers all levels 0≤j<t. This proves the claim about HAAR.
Inverse. We show that the pass of INVERSE-HAAR with length m undoes the pass of HAAR with length m. That pass of HAAR maps the old front v[0…m−1] to si=c(v2i+v2i+1) at i and di=c(v2i−v2i+1) at h+i. Then si+di=2cv2i and si−di=2cv2i+1, so lines L11 and L12 recover v2i and v2i+1 (here c=0 is used), and line L13 writes them back to the front, leaving y[m…n−1] unchanged. HAAR runs the passes with m=n,n/2,…,2, and INVERSE-HAAR runs the inverse passes with m=2,4,…,n, the reverse order. A composition of maps undone in reverse order is the identity, so INVERSE-HAAR gives back xin. For n=1 both loops do nothing, which is correct since then the transform is the identity.
Operation count. The pass with length m does m/2 additions (line L4), m/2 subtractions (line L5) and m multiplications by c. The passes have m=2t,2t−1,…,2, and ∑q=1t2q=2t+1−2=2n−2. So HAAR does exactly 2n−2 additions and subtractions and 2n−2 multiplications, for every input. The number of steps of all lines is proportional to this sum plus t+1 loop tests, so the time is Θ(n) in the best and the worst case. The buffer T has n entries, and no other storage grows with n, so the extra space is Θ(n). ■