Technische Universitat¨ M ¨unchen
Algorithms of Scientific Computing Discrete Cosine Transform (DCT)
Tobias Neckel, Dirk Pfluger¨ Summer Term 2011
Tobias Neckel, Dirk Pfl ¨uger: Algorithms of Scientific Computing Discrete Cosine Transform (DCT), Summer Term 2011 1 Technische Universitat¨ M ¨unchen DFT and Symmetry
INPUT TRANSFORM
real symmetry fn is real → Real-valued DFT (RDFT)
even symmetry fn = f−n → Discrete Cosine Transform (DCT)
odd symmetry fn = −f−n → Discrete Sine Transform (DST)
Tobias Neckel, Dirk Pfl ¨uger: Algorithms of Scientific Computing Discrete Cosine Transform (DCT), Summer Term 2011 2 Technische Universitat¨ M ¨unchen 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 cosine transform (blocks of size 8 × 8) 4. Quantification 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)
Tobias Neckel, Dirk Pfl ¨uger: Algorithms of Scientific Computing Discrete Cosine Transform (DCT), Summer Term 2011 3 Technische Universitat¨ M ¨unchen Discrete Fourier Transform (DFT)
Definition: For a vector of N complex numbers (f0,..., fN−1), the discrete Fourier transform is given by the vector (F0,..., FN−1), where
N−1 1 X F = f e−i2πnk/N . k N n n=0
Interpretation: • as trigonometric interpolation/approxmiation • as approximation of the coefficients of the Fourier series
Tobias Neckel, Dirk Pfl ¨uger: Algorithms of Scientific Computing Discrete Cosine Transform (DCT), Summer Term 2011 4 Technische Universitat¨ M ¨unchen Fourier Coefficients and Numerical Quadrature
For a 2π-periodic function f , the corresponding Fourier series is defined as
∞ 2π X 1 Z f (x) ∼ c eikx , mit c = f (x)e−ikx dx k k 2π k=−∞ 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.
Tobias Neckel, Dirk Pfl ¨uger: Algorithms of Scientific Computing Discrete Cosine Transform (DCT), Summer Term 2011 5 Technische Universitat¨ M ¨unchen
Approximate Computation of ck
The continuous Fourier coefficients are given as
2π 1 Z c = f (x)e−ikx dx k 2π 0
Approaches to compute ck approximately: K X ikx • compute ck only for ±k = 0,..., K ; then: f (x) ≈ ck e k=−K 2π Z • compute integral f (x)e−ikx dx numerically
0
Tobias Neckel, Dirk Pfl ¨uger: Algorithms of Scientific Computing Discrete Cosine Transform (DCT), Summer Term 2011 6 Technische Universitat¨ M ¨unchen
Computation of ck via Trapezoidal Sum
2πn Trapezoidal sum : for equidistant xn := N :
2π N−1 ! Z 2π 1 X 1 g(x) dx ≈ T {g} := g(x ) + g(x ) + g(x ) N N 2 0 n 2 N 0 n=1
−ikx Use g(x) := f (x)e and fn := f (xn); hence:
N−1 ! 1 1 1 X 1 c ≈ T {f (x)e−ikx } = f e0 + f e−i2πnk/N + f e−i2πNk/N k 2π N N 2 0 n 2 N n=1 N−1 ! 1 f0 X fN = + f e−i2πnk/N + N 2 n 2 n=1
Tobias Neckel, Dirk Pfl ¨uger: Algorithms of Scientific Computing Discrete Cosine Transform (DCT), Summer Term 2011 7 Technische Universitat¨ M ¨unchen
Computation of ck via Trapezoidal Sum (2)
If f0 = fN (periodic data), we obtain
N−1 1 X c ≈ F = f e−i2πnk/N k k 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)
Tobias Neckel, Dirk Pfl ¨uger: Algorithms of Scientific Computing Discrete Cosine Transform (DCT), Summer Term 2011 8 Technische Universitat¨ M ¨unchen
Computation of ck via Midpoint Rule
Midpoint rule: for evaluation at the midpoints, it holds:
2π N−1 1 Z 2π X 2π n + g(x) dx ≈ g(x ) with x := 2 . N n n N 0 n=0
−ikx With g(x) := f (x)e and fn := f (xn), we obtain:
N−1 1 X −i2π(n+ 1 )k/N c ≈ Fe := fne 2 k k N n=0
“Quarter-Wave Discrete Fourier Transform”
Tobias Neckel, Dirk Pfl ¨uger: Algorithms of Scientific Computing Discrete Cosine Transform (DCT), Summer Term 2011 9 Technische Universitat¨ M ¨unchen Quarter-Wave Discrete Fourier Transform
• new variant of DFT:
N−1 N−1 1 X −i2π(n+ 1 )k/N X i2π(n+ 1 )k/N Fe := fne 2 fn := Fe e 2 k N k n=0 k=0
• Comparison with coefficients Fk of the “usual” DFT:
iπk/N k/2 Fk = Fek e = Fek ω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
Tobias Neckel, Dirk Pfl ¨uger: Algorithms of Scientific Computing Discrete Cosine Transform (DCT), Summer Term 2011 10 Technische Universitat¨ M ¨unchen 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
2N−1 1 1 X −k(n+ 2 ) Fe = fnω k 2N 2N n=0 N−1 N−1 1 X −k(n+ 1 ) 1 X −k(2N−n−1+ 1 ) = f ω 2 + f ω 2 2N n 2N 2N 2N−n−1 2N n=0 n=0 N−1 N−1 ! 1 1 1 1 X −k(n+ ) −k(−n− ) 1 X πk n + = f ω 2 + ω 2 = f cos 2 . 2N n 2N 2N N n N n=0 n=0
Tobias Neckel, Dirk Pfl ¨uger: Algorithms of Scientific Computing Discrete Cosine Transform (DCT), Summer Term 2011 11 Technische Universitat¨ M ¨unchen Quarter-Wave DFT on Symmetric Data (2)
Quarter-Wave DFT of symmetric data results in real-valued coefficients:
N−1 1 ! 1 X πk n + 2 Fe = fn cos for k = 0,..., 2N − 1 k N N n=0 Additional symmetry:
N−1 1 ! 1 X π(2N − k) n + 2 Fe − = fn cos 2N k N N n=0 N−1 1 ! 1 X πk n + 2 = fn cos 2πn + π − = −Fe N N k n=0
⇒ again: only N independent coefficients
Tobias Neckel, Dirk Pfl ¨uger: Algorithms of Scientific Computing Discrete Cosine Transform (DCT), Summer Term 2011 12 Technische Universitat¨ M ¨unchen Quarter-Wave Even Discrete Cosine Transform
backward transform:
2N−1 N−1 1 ! X i2π(n+ 1 )k/2N Fe2N−k =−Fek X πk n + 2 fn := Fe e 2 −→ fn = Fe0+2 Fe cos k k N k=0 k=1
Definition of the quarter-wave even DCT:
N−1 1 ! N−1 1 ! 1 X πk n + 2 X πk n + 2 Fe = fn cos fn = Fe0+2 Fe cos k N N k N n=0 k=1 N real-valued data ←→ N real-valued coefficients (no symmetry any more in data/coefficients!)
Tobias Neckel, Dirk Pfl ¨uger: Algorithms of Scientific Computing Discrete Cosine Transform (DCT), Summer Term 2011 13 Technische Universitat¨ M ¨unchen QW-DCT – Algorithmus
Reduce to real-valued FFT: (1) for n = 0,..., N − 1:
gn = fn g2N−n−1 = fn
(2) real-valued 2N-FFT of gn supplies Gk for k = 0,..., 2N − 1 (3) for k = 0,..., N − 1:
−iπk/2N Fek = Gk e
Further optimisations: • substitute real-valued 2N-FFT by complex N-FFT • compact DCT −→ paper Swarztrauber
Tobias Neckel, Dirk Pfl ¨uger: Algorithms of Scientific Computing Discrete Cosine Transform (DCT), Summer Term 2011 14 Technische Universitat¨ M ¨unchen 2D Cosine Transform
Definition of the 2D-DCT:
N−1 M−1 1 ! 1 ! 1 X X πk n + 2 πl m + 2 Fe = fnm cos cos kl N · M N M n=0 m=0
N−1 M−1 1 ! 1 ! X 0 X 0 πk n + 2 πl m + 2 fnm = 4 Fe cos cos kl N M k=0 l=0
N−1 N−1 P 0 x0 P shortened notation: xk := 2 + xk k=0 k=1
Application: blockwise 2D-DCT in JPEG/MPEG compression
Tobias Neckel, Dirk Pfl ¨uger: Algorithms of Scientific Computing Discrete Cosine Transform (DCT), Summer Term 2011 15 Technische Universitat¨ M ¨unchen Reduction of the 2D-FCT to 1D-FCTs
In the 2D cosine transform, we can rearrange:
N−1 N−1 1 ! 1 ! 1 X X πk n + 2 πl m + 2 Fe = fnm cos cos kl N2 N N n=0 m=0 N−1 N−1 1 !! 1 ! 1 X 1 X πl m + πk n + = f cos 2 cos 2 . N N nm N N n=0 m=0 | {z } := Fbnl
• For each n, Fbnl are computed via N 1D transforms • we may first 1D-transform all rows and then all columns to get the 2D-transform
Tobias Neckel, Dirk Pfl ¨uger: Algorithms of Scientific Computing Discrete Cosine Transform (DCT), Summer Term 2011 16 Technische Universitat¨ M ¨unchen 2D-FCT – Algorithm
1. Apply a 1D FCT for all n = 0,..., N − 1:
N−1 1 ! 1 X πl m + 2 Fb = fnm cos nl N N m=0
(transform all “rows”).
2. Apply a 1D FCT for all l = 0,..., N − 1:
N−1 1 ! 1 X πk n + Fe = Fb cos 2 . kl N nl N n=0
(transform all “columns”).
Tobias Neckel, Dirk Pfl ¨uger: Algorithms of Scientific Computing Discrete Cosine Transform (DCT), Summer Term 2011 17 Technische Universitat¨ M ¨unchen 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 cosine transform (blocks of size 8 × 8) 4. Quantification 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)
Tobias Neckel, Dirk Pfl ¨uger: Algorithms of Scientific Computing Discrete Cosine Transform (DCT), Summer Term 2011 18