Algorithm Search⌘K

Proofs: Fast discrete cosine transform

These are the human proofs of Fast discrete cosine transform, at full length.

Makhoul's reduction computes the DCT-II, its inverse recovers the input, and the rounding error is O(u log n) full

This proof shows: correctness, complexity, error-bound.

Let n=2tn = 2^t with t≥1t \ge 1 (for n=1n = 1 line L2 returns X0=x0X_0 = x_0, which is the DCT-II of length 11). Let x∈Rnx \in \mathbb{R}^n, let vv be the vector built by lines L3 to L5, so vr=x2rv_r = x_{2r} and vn−1−r=x2r+1v_{n-1-r} = x_{2r+1} for 0≤r<n/20 \le r < n/2, let Vk=∑rvrωnrkV_k = \sum_r v_r \omega_n^{rk} with ωn=e−2πi/n\omega_n = e^{-2\pi i/n}, and let dk=e−πik/(2n)d_k = e^{-\pi i k/(2n)}. Write Xk=∑jxjcos⁡π(2j+1)k2nX_k = \sum_j x_j \cos\frac{\pi(2j+1)k}{2n}.

Lemma 1 (reordering). For 0≤k≤n−10 \le k \le n - 1, Xk=∑r=0n−1vrcos⁡π(4r+1)k2nX_k = \sum_{r=0}^{n-1} v_r \cos\frac{\pi(4r+1)k}{2n}.

Proof. Lines L3 to L5 define a bijection between the indices jj and rr: the even j=2rj = 2r go to r∈{0,…,n/2−1}r \in \{0, \ldots, n/2 - 1\}, and the odd j=2r+1j = 2r + 1 go to r′=n−1−r∈{n/2,…,n−1}r' = n - 1 - r \in \{n/2, \ldots, n - 1\}. For an even j=2rj = 2r, 2j+1=4r+12j + 1 = 4r + 1. For an odd j=2r+1j = 2r + 1 with r=n−1−r′r = n - 1 - r', 2j+1=4r+3=4n−(4r′+1)2j + 1 = 4r + 3 = 4n - (4r' + 1), so cos⁡π(2j+1)k2n=cos⁡(2πk−π(4r′+1)k2n)=cos⁡π(4r′+1)k2n\cos\frac{\pi(2j+1)k}{2n} = \cos\left(2\pi k - \frac{\pi(4r'+1)k}{2n}\right) = \cos\frac{\pi(4r'+1)k}{2n}, since cos⁡\cos is even and has period 2π2\pi. Summing over the bijection gives the claim. □\square

Theorem 1 (forward). FAST-DCT(x)(x) returns XX.

Proof. By Theorem 1 of the FFT proof, line L6 computes V=FnvV = F_n v. Then dkVk=∑rvre−πik/(2n)e−2πirk/n=∑rvre−πi(4r+1)k/(2n)d_k V_k = \sum_r v_r e^{-\pi i k/(2n)} e^{-2\pi i rk/n} = \sum_r v_r e^{-\pi i (4r+1)k/(2n)}. Since each vrv_r is real, the real part is ∑rvrcos⁡π(4r+1)k2n\sum_r v_r \cos\frac{\pi(4r+1)k}{2n}, which is XkX_k by Lemma 1. Line L8 stores this value for each kk. □\square

Lemma 2 (symmetry). For 1≤k≤n−11 \le k \le n - 1, Im(dkVk)=−Xn−k\mathrm{Im}(d_k V_k) = -X_{n-k}, and V0=X0V_0 = X_0.

Proof. Since vv is real, Vn−k=∑rvrωnr(n−k)=∑rvrωn−rk=Vk‾V_{n-k} = \sum_r v_r \omega_n^{r(n-k)} = \sum_r v_r \omega_n^{-rk} = \overline{V_k}. Also dn−k=e−πi/2eπik/(2n)=−i dk‾d_{n-k} = e^{-\pi i/2} e^{\pi i k/(2n)} = -i \, \overline{d_k}. So dn−kVn−k=−i dkVk‾d_{n-k} V_{n-k} = -i \, \overline{d_k V_k}. For z=a+ibz = a + ib, Re(−izˉ)=Re(−ia−b)=−b\mathrm{Re}(-i \bar z) = \mathrm{Re}(-ia - b) = -b. With z=dkVkz = d_k V_k and Theorem 1 applied at n−kn - k, Xn−k=Re(dn−kVn−k)=−Im(dkVk)X_{n-k} = \mathrm{Re}(d_{n-k} V_{n-k}) = -\mathrm{Im}(d_k V_k). Finally d0=1d_0 = 1, and V0=∑rvrV_0 = \sum_r v_r is real, so V0=Re(d0V0)=X0V_0 = \mathrm{Re}(d_0 V_0) = X_0. □\square

Theorem 2 (inverse). INVERSE-FAST-DCT(X)(X) returns xx.

Proof. By Theorem 1 and Lemma 2, dkVk=Xk−iXn−kd_k V_k = X_k - i X_{n-k} for 1≤k≤n−11 \le k \le n - 1, so Vk=dk‾(Xk−iXn−k)=eπik/(2n)(Xk−iXn−k)V_k = \overline{d_k} (X_k - i X_{n-k}) = e^{\pi i k/(2n)} (X_k - i X_{n-k}), using ∣dk∣=1|d_k| = 1. Together with V0=X0V_0 = X_0, line L10 sets W=VW = V. Since ωn\omega_n is a principal nn-th root of unity in C\mathbb{C} and nn is invertible, Theorem 2 of the FFT proof shows that line L11 returns w=vw = v, which is real. Line L12 undoes the bijection of lines L3 to L5, so it returns xx. □\square

Lemma 3 (row orthogonality). Let Ckj=cos⁡π(2j+1)k2nC_{kj} = \cos\frac{\pi(2j+1)k}{2n}. Then CCT=D=diag(n,n2,…,n2)C C^{\mathsf T} = D = \mathrm{diag}(n, \frac{n}{2}, \ldots, \frac{n}{2}), and ∥Cy∥2≥n/2 ∥y∥2\lVert C y \rVert_2 \ge \sqrt{n/2} \, \lVert y \rVert_2 for every y∈Rny \in \mathbb{R}^n.

Proof. First, for an integer mm with 0<∣m∣<2n0 < |m| < 2n, S(m)=∑j=0n−1cos⁡π(2j+1)m2n=0S(m) = \sum_{j=0}^{n-1} \cos\frac{\pi(2j+1)m}{2n} = 0. Put θ=πm/n\theta = \pi m/n, so eiθ≠1e^{i\theta} \ne 1. Then S(m)=Re(eiθ/2∑jeijθ)=Re(eiθ/2einθ−1eiθ−1)S(m) = \mathrm{Re}\left(e^{i\theta/2} \sum_j e^{ij\theta}\right) = \mathrm{Re}\left(e^{i\theta/2} \frac{e^{in\theta} - 1}{e^{i\theta} - 1}\right). If mm is even, einθ=eiπm=1e^{in\theta} = e^{i\pi m} = 1 and S(m)=0S(m) = 0. If mm is odd, einθ=−1e^{in\theta} = -1 and eiθ−1=2isin⁡(θ/2)eiθ/2e^{i\theta} - 1 = 2i \sin(\theta/2) e^{i\theta/2} with sin⁡(θ/2)≠0\sin(\theta/2) \ne 0, so S(m)=Re(−22isin⁡(θ/2))=Re(isin⁡(θ/2))=0S(m) = \mathrm{Re}\left(\frac{-2}{2i \sin(\theta/2)}\right) = \mathrm{Re}\left(\frac{i}{\sin(\theta/2)}\right) = 0. Now by cos⁡acos⁡b=12(cos⁡(a−b)+cos⁡(a+b))\cos a \cos b = \frac{1}{2}(\cos(a - b) + \cos(a + b)), (CCT)kl=12(S(k−l)+S(k+l))(C C^{\mathsf T})_{kl} = \frac{1}{2}(S(k - l) + S(k + l)), with S(0)=nS(0) = n. For k=l=0k = l = 0 this is nn. For k=l≥1k = l \ge 1 it is 12(n+S(2k))=n2\frac{1}{2}(n + S(2k)) = \frac{n}{2}, since 0<2k<2n0 < 2k < 2n. For k≠lk \ne l, both k−lk - l and k+lk + l are nonzero and less than 2n2n in absolute value, so the entry is 00. Hence Q=D−1/2CQ = D^{-1/2} C satisfies QQT=IQ Q^{\mathsf T} = I, so QQ is orthogonal, and ∥Cy∥2=∥D1/2Qy∥2≥n/2 ∥Qy∥2=n/2 ∥y∥2\lVert C y \rVert_2 = \lVert D^{1/2} Q y \rVert_2 \ge \sqrt{n/2} \, \lVert Q y \rVert_2 = \sqrt{n/2} \, \lVert y \rVert_2. □\square

Theorem 3 (rounding error). Work in floating point with unit roundoff uu and γm=mu/(1−mu)\gamma_m = mu/(1 - mu). Suppose the computed FFT satisfies ∥V^−V∥2≤δ∥V∥2\lVert \hat V - V \rVert_2 \le \delta \lVert V \rVert_2 (Higham, Theorem 24.2, gives such a δ=O(ulog⁡n)\delta = O(u \log n) when the twiddle factors are accurate), each computed rotation is d^k=dk(1+θk)\hat d_k = d_k(1 + \theta_k) with ∣θk∣≤μ|\theta_k| \le \mu, and line L8 evaluates c^kReV^k+s^kImV^k\hat c_k \mathrm{Re} \hat V_k + \hat s_k \mathrm{Im} \hat V_k in floating point, with d^k=c^k−is^k\hat d_k = \hat c_k - i \hat s_k. Then

∥X^−X∥2∥X∥2≤2 (δ+μ+2γ2)+O(u2)\frac{\lVert \hat X - X \rVert_2}{\lVert X \rVert_2} \le \sqrt 2 \, (\delta + \mu + \sqrt 2 \gamma_2) + O(u^2)

for x≠0x \ne 0.

Proof. Lines L3 to L5 copy entries, so vv is exact and ∥v∥2=∥x∥2\lVert v \rVert_2 = \lVert x \rVert_2. By Parseval for the unnormalized DFT, ∥V∥2=n∥v∥2=n∥x∥2\lVert V \rVert_2 = \sqrt n \lVert v \rVert_2 = \sqrt n \lVert x \rVert_2. The floating-point dot product of length 22 gives X^k=Re(d^kV^k)+ek\hat X_k = \mathrm{Re}(\hat d_k \hat V_k) + e_k with ∣ek∣≤γ2(∣c^kReV^k∣+∣s^kImV^k∣)≤2γ2∣d^k∣∣V^k∣|e_k| \le \gamma_2 (|\hat c_k \mathrm{Re} \hat V_k| + |\hat s_k \mathrm{Im} \hat V_k|) \le \sqrt 2 \gamma_2 |\hat d_k| |\hat V_k|, by the Cauchy-Schwarz inequality. Hence

∣X^k−Xk∣≤∣d^kV^k−dkVk∣+∣ek∣≤∣V^k−Vk∣+μ∣V^k∣+2γ2(1+μ)∣V^k∣,|\hat X_k - X_k| \le |\hat d_k \hat V_k - d_k V_k| + |e_k| \le |\hat V_k - V_k| + \mu |\hat V_k| + \sqrt 2 \gamma_2 (1 + \mu) |\hat V_k|,

using Xk=Re(dkVk)X_k = \mathrm{Re}(d_k V_k), ∣dk∣=1|d_k| = 1 and d^kV^k−dkVk=dk(V^k−Vk)+dkθkV^k\hat d_k \hat V_k - d_k V_k = d_k(\hat V_k - V_k) + d_k \theta_k \hat V_k. Taking 2-norms over kk and using ∥V^∥2≤(1+δ)∥V∥2\lVert \hat V \rVert_2 \le (1 + \delta) \lVert V \rVert_2,

∥X^−X∥2≤(δ+(μ+2γ2)(1+δ)+2γ2μ(1+δ))n ∥x∥2=(δ+μ+2γ2)n ∥x∥2+O(u2).\lVert \hat X - X \rVert_2 \le \left(\delta + (\mu + \sqrt 2 \gamma_2)(1 + \delta) + \sqrt 2 \gamma_2 \mu (1 + \delta)\right) \sqrt n \, \lVert x \rVert_2 = (\delta + \mu + \sqrt 2 \gamma_2) \sqrt n \, \lVert x \rVert_2 + O(u^2).

By Lemma 3, ∥X∥2=∥Cx∥2≥n/2 ∥x∥2\lVert X \rVert_2 = \lVert C x \rVert_2 \ge \sqrt{n/2} \, \lVert x \rVert_2. Dividing gives the bound. □\square

Operation count. Lines L3 to L5 and L12 only move data. Line L6 is one complex FFT of length nn: n2lg⁡n\frac{n}{2} \lg n complex multiplications and nlg⁡nn \lg n complex additions, which is 2nlg⁡n2 n \lg n real multiplications and 3nlg⁡n3 n \lg n real additions with the usual 44 multiplications and 22 additions per complex product. Lines L7 and L8 use at most 2n2n real multiplications and nn real additions, plus the nn rotation factors, which can be precomputed once per length. So FAST-DCT uses 5nlg⁡n+3n5 n \lg n + 3n real operations, and Θ(nlog⁡n)\Theta(n \log n) in total. Line L10 uses O(n)O(n) operations and line L11 one inverse FFT of length nn, so the inverse has the same order of cost. ■\blacksquare

Back to Fast discrete cosine transform