Technische Universität München Algorithms of Scientific Computing The Quarter-Wave DFT and the (Quarter-Wave) Discrete Cosine Transform (QW-DCT) Michael Bader Summer Term 2013 Michael Bader: Algorithms of Scientific Computing Quarter-Wave DFT and (Quarter-Wave) Discrete Cosine Transform (QW-DCT), Summer Term 2013 1 Technische Universität München DFT and Symmetry INPUT TRANSFORM real symmetry fn ∈ R → Real DFT (RDFT) even symmetry fn = f−n → Discrete Cosine Transform (DCT) odd symmetry fn = −f−n → Discrete Sine Transform (DST) “QUARTER-WAVE” INPUT TRANSFORM even symmetry fn = f−n−1 → QW-DCT odd symmetry fn = −f−n−1 → QW-DST Michael Bader: Algorithms of Scientific Computing Quarter-Wave DFT and (Quarter-Wave) Discrete Cosine Transform (QW-DCT), Summer Term 2013 2 Technische Universität München Motivation: Compression of Image Data (JPEG) Compression steps of the JPEG method 1. Conversion into a suitable colour model (YCbCr, e.g.), separation of brightness and colour information 2. Downsampling (in particular of the colour components) 3. blockwise “quarter-wave discrete cosine transform” (blocks of size 8 × 8) 4. Quantisation of the coefficients (→ reduce information) 5. run-length encoding, Huffman/arithmetic coding (loss-free compression of the quantified coefficients) Example: jpeg on matlab central (see link on webpage) Michael Bader: Algorithms of Scientific Computing Quarter-Wave DFT and (Quarter-Wave) Discrete Cosine Transform (QW-DCT), Summer Term 2013 3 Technische Universität München Discrete Fourier Transform (DFT) Definition: T For a vector of N complex numbers (f0 , . . . , fN−1 ) , the discrete T Fourier transform is given by the vector (F0 , . . . , FN−1 ) , where Fk = N−1 1 X fn e−i2πnk /N . N n=0 Interpretation: • as trigonometric interpolation/approxmiation • as approximation of the coefficients of the Fourier series Michael Bader: Algorithms of Scientific Computing Quarter-Wave DFT and (Quarter-Wave) Discrete Cosine Transform (QW-DCT), Summer Term 2013 4 Technische Universität München Fourier Coefficients and Numerical Quadrature For a 2π-periodic function f , the corresponding Fourier series is defined as f (x) ∼ ∞ X ikx ck e , k=−∞ where 1 ck = 2π Z2π f (x)e−ikx dx 0 The ck are called (continuous) Fourier coefficients. If f is piecewisely smooth, Fourier series converges pointwisely (i.e. for each x) towards + − 1 2 (f (x ) + f (x )), i.e. in particular towards f (x), if f is coninuously differentiable at x. Michael Bader: Algorithms of Scientific Computing Quarter-Wave DFT and (Quarter-Wave) Discrete Cosine Transform (QW-DCT), Summer Term 2013 5 Technische Universität München Computation of Fourier Coefficients ck ∞ X Assume: f (x) given by Fourier series, then f (x) = ck eikx k =−∞ Multiply by e−inx and integrate: Z2π f (x)e 0 −inx dx = ∞ Z2π X ikx −inx ck e e ∞ X dx = k=−∞ 0 k=−∞ Z2π ck 0 | ⇒ only term for k = n remains in the series, and 2π R ei(k−n)x dx {z =0, if k 6= n } ei(n−n)x dx = 2π 0 The Fourier coefficients ck thus need to be 1 ck = 2π Z2π f (x)e−ikx dx 0 Michael Bader: Algorithms of Scientific Computing Quarter-Wave DFT and (Quarter-Wave) Discrete Cosine Transform (QW-DCT), Summer Term 2013 6 Technische Universität München Approximate Computation of ck The continuous Fourier coefficients are given as 1 ck = 2π Z2π f (x)e−ikx dx 0 Steps to compute ck approximately: • consider ck only for ±k = 0, . . . , K ; then: f (x) ≈ K X ck eikx k =−K Z2π • compute numerical approximation of integral f (x)e−ikx dx 0 Michael Bader: Algorithms of Scientific Computing Quarter-Wave DFT and (Quarter-Wave) Discrete Cosine Transform (QW-DCT), Summer Term 2013 7 Technische Universität München Computation of ck via Trapezoidal Sum Trapezoidal sum: for equidistant xn := Z2π 2π g(x) dx ≈ TN {g} := N 2πn N : ! N−1 X 1 1 g(x0 ) + g(xn ) + g(xN ) 2 2 n=1 0 Use g(x) := f (x)e−ikx and fn := f (xn ), then: 1 ck ≈ TN {f (x)e−ikx } 2π = 1 N = 1 N N−1 1 0 X 1 f0 e + fn e−i2πnk/N + fN e−i2πNk/N 2 2 n=1 ! N−1 f0 X fN + fn e−i2πnk/N + 2 2 ! n=1 Michael Bader: Algorithms of Scientific Computing Quarter-Wave DFT and (Quarter-Wave) Discrete Cosine Transform (QW-DCT), Summer Term 2013 8 Technische Universität München Computation of ck via Trapezoidal Sum (2) If f0 = fN (periodic data), we obtain ck ≈ Fk = N−1 1 X fn e−i2πnk/N N n=0 ⇒ Fk are approximations of ck ⇒ approximate computation leads to solution of the interpolation problem ⇒ approximation error is of order O(N −2 ) For f0 6= fN , or for “discontinuities”, we get a recommendation: Average Values at Endpoints and Discontinuities (AVED) Michael Bader: Algorithms of Scientific Computing Quarter-Wave DFT and (Quarter-Wave) Discrete Cosine Transform (QW-DCT), Summer Term 2013 9 Technische Universität München Computation of ck via Midpoint Rule Midpoint rule: evaluate g(x) at midpoints xn : Z2π N−1 2π X g(x) dx ≈ g(xn ) N n=0 0 2π n + with xn := N 1 2 . With g(x) := f (x)e−ikx and fn := f (xn ), we obtain: N−1 X 1 ek := 1 ck ≈ F fn e−i2π(n+ 2 )k/N N n=0 “Quarter-Wave Discrete Fourier Transform” Michael Bader: Algorithms of Scientific Computing Quarter-Wave DFT and (Quarter-Wave) Discrete Cosine Transform (QW-DCT), Summer Term 2013 10 Technische Universität München Quarter-Wave Discrete Fourier Transform • new variant of DFT: N−1 X 1 ek := 1 F fn e−i2π(n+ 2 )k/N N n=0 fn := N−1 X ek ei2π(n+ 21 )k /N F k=0 • Comparison with coefficients Fk of the “usual” DFT: ek eiπk/N = F ek ω k/2 Fk = F N • Supporting points compared to “usual” DFT shifted by a “quarter wave length” (midpoints of intervals). • Derivation via midpoint rule motivates usage for piecewise constant data ⇒ Transformation of image data Michael Bader: Algorithms of Scientific Computing Quarter-Wave DFT and (Quarter-Wave) Discrete Cosine Transform (QW-DCT), Summer Term 2013 11 Technische Universität München Quarter-Wave DFT on Symmetric Data Given 2N real-valued input data f0 , . . . , f2N−1 with symmetry f2N−n−1 = fn Inserting the symmetric data in Quarter-Wave DFT results in ek F = 2N−1 1 X −k (n+ 21 ) fn ω2N 2N n=0 = = N−1 1 X −k (2N−n−1+ 12 ) f2N−n−1 ω2N 2N n=0 n=0 ! N−1 N−1 X 1 πk n + 12 1 1 X −k (n+ 2 ) −k (−n− 12 ) fn ω2N + ω2N = fn cos . 2N N N 1 2N N−1 X −k (n+ 12 ) fn ω2N + n=0 n=0 Michael Bader: Algorithms of Scientific Computing Quarter-Wave DFT and (Quarter-Wave) Discrete Cosine Transform (QW-DCT), Summer Term 2013 12 Technische Universität München Quarter-Wave DFT on Symmetric Data (2) Quarter-Wave DFT of symmetric data results in real-valued coefficients: ! N−1 πk n + 21 1 X e Fk = fn cos for k = 0, . . . , 2N − 1 N N n=0 Additional symmetry: e2N−k F = N−1 1 X fn cos N n=0 = 1 N N−1 X n=0 π(2N − k ) n + N 1 2 ! πk n + fn cos 2πn + π − N 1 2 ! ek = −F ⇒ again: only N independent coefficients Michael Bader: Algorithms of Scientific Computing Quarter-Wave DFT and (Quarter-Wave) Discrete Cosine Transform (QW-DCT), Summer Term 2013 13 Technische Universität München Quarter-Wave Even Discrete Cosine Transform Backward transform: fn := 2N−1 X ek e F i2π (n+ 12 )k /2N e2N−k =−F ek F −→ e0 +2 fn = F k=0 N−1 X ek cos F k=1 Definition of the quarter-wave even DCT: ! N−1 N−1 1 X X n + πk 1 2 ek = e0 +2 ek cos F fn cos fn = F F N N n=0 k =1 πk n + N πk n + N 1 2 1 2 ! ! N real values ←→ N real-valued coefficients (no symmetry any more in data/coefficients!) Michael Bader: Algorithms of Scientific Computing Quarter-Wave DFT and (Quarter-Wave) Discrete Cosine Transform (QW-DCT), Summer Term 2013 14 Technische Universität München 2D Cosine Transform Definition of the 2D-DCT: ekl F = N−1 M−1 1 XX fnm cos N ·M πk n + N n=0 m=0 fnm = 4 N−1 X M−1 X 0 0 k =0 πk n + N ekl cos F l=0 shortened notation: N−1 P0 k=0 xk := x0 2 + N−1 P 1 2 1 2 ! cos ! cos πl m + M πl m + M 1 2 1 2 ! ! xk k=1 Application: blockwise 2D-DCT in JPEG/MPEG compression Michael Bader: Algorithms of Scientific Computing Quarter-Wave DFT and (Quarter-Wave) Discrete Cosine Transform (QW-DCT), Summer Term 2013 15 Technische Universität München Reduction of the 2D-FCT to 1D-FCTs In the 2D cosine transform, we can rearrange: ! ! N−1 N−1 πl m + 12 πk n + 12 1 XX e Fkl = cos fnm cos N2 N N n=0 m=0 !! ! N−1 N−1 πk n + 12 πl m + 21 1 X 1 X cos = fnm cos . N N N N n=0 m=0 | {z } b := Fnl bnl are computed via N 1D transforms • For each n, F • we may first 1D-transform all rows and then all columns to get the 2D-transform Michael Bader: Algorithms of Scientific Computing Quarter-Wave DFT and (Quarter-Wave) Discrete Cosine Transform (QW-DCT), Summer Term 2013 16 Technische Universität München Application Example: Compression of Image Data (JPEG) Compression steps of the JPEG method 1. Conversion into a suitable colour model (YCbCr, e.g.), separation of brightness and colour information 2. Downsampling (in particular of the colour components) 3. blockwise “quarter-wave discrete cosine transform” (blocks of size 8 × 8) 4. Quantisation of the coefficients (→ reduce information) 5. run-length encoding, Huffman/arithmetic coding (loss-free compression of the quantified coefficients) Example: jpeg on matlab central (see link on webpage) Michael Bader: Algorithms of Scientific Computing Quarter-Wave DFT and (Quarter-Wave) Discrete Cosine Transform (QW-DCT), Summer Term 2013 17 Technische Universität München QW-DCT – Algorithm Reduce to Real FFT: (1) for n = 0, . . . , N − 1: gn = fn g2N−n−1 = fn (2) 2N-Real-FFT: compute Gk from gn (for k = 0, . . . , N) (3) for k = 0, . . . , N − 1: ek = Gk e−iπk/2N F Further optimisations: • substitute real 2N-FFT by complex N-FFT • compact (divide-and-conquer) real FFT • compact Fast (QW-)DCT −→ paper Swarztrauber Michael Bader: Algorithms of Scientific Computing Quarter-Wave DFT and (Quarter-Wave) Discrete Cosine Transform (QW-DCT), Summer Term 2013 18 Technische Universität München Compact Fast DCT Consider QW-DCT: with symmetry f2N−n−1 = fn 2N−1 X −k (n+ 21 ) ek = 1 F fn ω2N 2N n=0 −→ N−1 X ek = 1 fn cos F N n=0 πk n + N 1 2 ! . Split into even and odd indices: gn := f2n and hn := f2n+1 (as in FFT) • gn := f2n : gn = f2n = f2N−2n−1 = f2(N−n)−1 = f2(N−n−1)+1 = hN−n−1 • hn := f2n+1 : hn = f2n+1 = f2N−(2n+1)−1 = f2(N−n−1) = gN−n−1 • thus: two real DFTs with symmetric data sets see exercises: reversed-data DFT easily obtained from DFT Michael Bader: Algorithms of Scientific Computing Quarter-Wave DFT and (Quarter-Wave) Discrete Cosine Transform (QW-DCT), Summer Term 2013 19 Technische Universität München Compact Fast Inverse QW-DCT e2N−k = −F ek Consider backward transform: with symmetry F fn := 2N−1 X ek e F i2π (n+ 12 )k /2N k=0 −→ e0 +2 fn = F N−1 X ek cos F k =1 πk n + N 1 2 ! Split into even and odd indices: (as in FFT) e2k : again leads to Inverse QW-DCT • Gk := F e2k = F e2N−2k = F e2(N−k ) = GN−k −Gk = −F e2k+1 : leads to new kind of inverse DCT • Hk := F e2k+1 = F e2N−(2k +1) = F e2(N−k )−1 = F e2(N−k−1)+1 = HN−k −1 −Hk = −F (next even/odd split leads to two real DFTs with symm. data sets) Michael Bader: Algorithms of Scientific Computing Quarter-Wave DFT and (Quarter-Wave) Discrete Cosine Transform (QW-DCT), Summer Term 2013 20