Algorithms of Scientific Computing: The Quarter-Wave DFT and The (Quarter-Wave) Discrete Cosine Transform (QW-DCT)
Algorithms of Scientific Computing: The Quarter-Wave DFT and The (Quarter-Wave) Discrete Cosine Transform (QW-DCT)
Michael Bader
Technical University of Munich
Summer 2024
Fast Fourier Transform – Outline
Literature/Reference:
William L. Briggs and Van Emden Henson: The DFT – An Owner’s Manual for the Discrete
Fourier Transform. SIAM, 1995. https://doi.org/10.1137/1.9781611971514
Michael Bader | Algorithms of Scientific Computing | Quarter-Wave DFT and DCT | Summer 2024 2
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)
Michael Bader | Algorithms of Scientific Computing | Quarter-Wave DFT and DCT | Summer 2024 3
Revisited: Discrete Fourier Transform (DFT)
Definition:
T
For a vector of N complex numbers (f0 , . . . , fN−1 ) , the discrete Fourier
T
transform is given by the vector (F0 , . . . , FN−1 ) , where
N−1
1 X
Fk = fn e−i2πnk /N .
N
n=0
Interpretation:
• as trigonometric interpolation/approximation
• as approximation of the coefficients of the Fourier series
Michael Bader | Algorithms of Scientific Computing | Quarter-Wave DFT and DCT | Summer 2024 4
Fourier Coefficients and Numerical Quadrature
∞ Z2π
X 1
f (x) ∼ ikx
ck e , where ck = f (x)e−ikx dx
2π
k =−∞ 0
Michael Bader | Algorithms of Scientific Computing | Quarter-Wave DFT and DCT | Summer 2024 5
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π ∞ Z2π
X ∞
X Z2π
−inx ikx −inx
f (x)e dx = ck e e dx = ck ei(k −n)x dx
0 k=−∞ 0 k =−∞ 0
| {z }
=0, if k 6= n
2π
ei(n−n)x dx = 2π
R
⇒ only term for k = n remains in the series, and
0
The Fourier coefficients ck thus need to be
Z2π
1
ck = f (x)e−ikx dx
2π
0
Michael Bader | Algorithms of Scientific Computing | Quarter-Wave DFT and DCT | Summer 2024 6
Orthogonal Functions
If we consider
• the functions eikx as vectors
• and the operation hf (x), g(x)i := 1
R
2π
f (x)g(x) dx as a scalar product
then the formula
Z2π Z2π
1 1 1 if k = n
eikx e−inx dx = ei(k−n)x dx =
2π 2π 0 if k 6= n
0 0
suggests that eikx and e−inx are orthogonal functions.
Michael Bader | Algorithms of Scientific Computing | Quarter-Wave DFT and DCT | Summer 2024 7
Approximate Computation of ck
Z2π
1
ck = f (x)e−ikx dx
2π
0
Michael Bader | Algorithms of Scientific Computing | Quarter-Wave DFT and DCT | Summer 2024 8
Computation of ck via Trapezoidal Sum
2πn
Trapezoidal sum: for equidistant xn := N :
Z2π N−1
!
2π 1 X 1
g(x) dx ≈ TN {g} := g(x0 ) + g(xn ) + g(xN )
N 2 2
0 n=1
Michael Bader | Algorithms of Scientific Computing | Quarter-Wave DFT and DCT | Summer 2024 9
Computation of ck via Trapezoidal Sum (2)
⇒ Fk are approximations of ck
⇒ approximate computation leads to solution of the interpolation problem
⇒ approximation error is of order O(N −2 )
Michael Bader | Algorithms of Scientific Computing | Quarter-Wave DFT and DCT | Summer 2024 10
Computation of ck via Midpoint Rule
Z2π N−1 1
2π X 2π n + 2
g(x) dx ≈ g(xn ) with xn := .
N N
0 n=0
Michael Bader | Algorithms of Scientific Computing | Quarter-Wave DFT and DCT | Summer 2024 11
DFT and Symmetry
INPUT TRANSFORM
Michael Bader | Algorithms of Scientific Computing | Quarter-Wave DFT and DCT | Summer 2024 12
Quarter-Wave Discrete Fourier Transform
ek eiπk/N = F k/2
Fk = F ek ω
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 DCT | Summer 2024 13
Quarter-Wave DFT on Symmetric Data
Given 2N real-valued input data f0 , . . . , f2N−1 with symmetry
Michael Bader | Algorithms of Scientific Computing | Quarter-Wave DFT and DCT | Summer 2024 14
Quarter-Wave DFT on Symmetric Data (2)
Additional symmetry:
N−1 1
!
1 X π(2N − k ) n + 2
F
e2N−k = fn cos
N N
n=0
N−1 1
!
1 X πk n + 2
= fn cos 2πn + π − = −F
ek
N N
n=0
Michael Bader | Algorithms of Scientific Computing | Quarter-Wave DFT and DCT | Summer 2024 15
Quarter-Wave Even Discrete Cosine Transform
Backward transform:
2N−1 N−1 1
!
X
ek ei2π(n+ 12 )k /2N F
e2N−k =−F
ek X πk n + 2
fn := F −→ fn = F
e0 + 2 F
ek cos
N
k =0 k=1
Michael Bader | Algorithms of Scientific Computing | Quarter-Wave DFT and DCT | Summer 2024 16
Summary: QW-DCT and inverse QW-DCT
We obtain a new pair of transforms:
1
N−1
X πk n + 12 N−1
X πk n + 12
Fek = fn cos fn = F
e0 + 2 F
ek cos
N n=0 N N
k =1
• both transforms work on data sets that are neither symmetric nor periodic
• however, if we extend the data sets according to the symmetry rules, then the reflected
(and thus symmetric) sets become periodic as well
The two transforms are connected to the QW-DFT and QW-iDFT via a 3-step procedure:
1. extend/duplicate the data set in a symmetric way
2. apply the QW-DFT/QW-iDFT
3. extract the symmetric half of the transformed data set
This equivalence has two important consequences:
1. we may compute the cosine transforms (N numbers that require sums over N terms
⇒ O(N 2 ) operations) by using an FFT in step 2 ⇒ reduces work to O(N log N)
2. we prove that QW-DCT and QW-iDCT are inverse operations to each other
(being a special case of the QW-DFT and QW-iDFT, which are inverse to each other)
Michael Bader | Algorithms of Scientific Computing | Quarter-Wave DFT and DCT | Summer 2024 17
2D Cosine Transform
N−1 ! !
X M−1 X πk n + 1
πl m + 1
0 0 2 2
fnm = 4 F
ekl cos cos
N M
k=0 l=0
N−1 N−1
P0 x0 P
shortened notation: xk := 2 + xk
k=0 k=1
Michael Bader | Algorithms of Scientific Computing | Quarter-Wave DFT and DCT | Summer 2024 18
Reducing 2D to 1D Transforms
In the 2D transform, we can rearrange:
N−1 N−1 1
! 1
!
1 XX πk n + 2 πl m + 2
F
ekl = fnm cos cos
N2 N N
n=0 m=0
N−1 N−1 1
!! 1
!
1 X 1 X πl m + 2 πk n + 2
= fnm cos cos .
N N N N
n=0 m=0
| {z }
:= F
bnl
• For each n, F
bnl are computed via N 1D transforms
• we may first 1D-transform all rows and then all columns to get the
2D-transform
→ see tutorials for more details!
Michael Bader | Algorithms of Scientific Computing | Quarter-Wave DFT and DCT | Summer 2024 19
2D QW-FCT – Algorithm
Michael Bader | Algorithms of Scientific Computing | Quarter-Wave DFT and DCT | Summer 2024 20
Application Example: Compression of Image Data
(JPEG)
Michael Bader | Algorithms of Scientific Computing | Quarter-Wave DFT and DCT | Summer 2024 21
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
Important Note:
• this results in an algorithm that requires O(N log N) operations (FFT!)
• whereas computing all F ek = 1 P fn cos πk(n + 1 )/N would require
N 2
O(N 2 ) operations
Possible Further Optimisations:
• substitute real 2N-FFT by complex N-FFT
• compact (divide-and-conquer) real FFT
• is there a compact/fast (QW-)DCT? −→ see paper by Swarztrauber
Michael Bader | Algorithms of Scientific Computing | Quarter-Wave DFT and DCT | Summer 2024 22
Compact Fast QW-DCT (→ Swarztrauber, 1986)
Consider QW-DCT: with symmetry f2N−n−1 = fn
2N−1 N−1 1
!
ek = 1
X −k (n+ 12 )
ek = 1
X πk n + 2
F fn ω2N −→ F fn cos .
2N N N
n=0 n=0
Michael Bader | Algorithms of Scientific Computing | Quarter-Wave DFT and DCT | Summer 2024 23
Compact Fast QW-DCT (→ Swarztrauber, 1986)
Consider QW-DCT: with symmetry f2N−n−1 = fn
2N−1 N−1 1
!
ek = 1
X −k (n+ 12 )
ek = 1
X πk n + 2
F fn ω2N −→ F fn cos .
2N N N
n=0 n=0
Split into even and odd indices: gn := f2n and hn := f2n+1 (as in FFT)
• • • • • • • • • • • • and • • • •
• gn := f2n :
Michael Bader | Algorithms of Scientific Computing | Quarter-Wave DFT and DCT | Summer 2024 23
Compact Fast QW-DCT (→ Swarztrauber, 1986)
Consider QW-DCT: with symmetry f2N−n−1 = fn
2N−1 N−1 1
!
ek = 1
X −k (n+ 12 )
ek = 1
X πk n + 2
F fn ω2N −→ F fn cos .
2N N N
n=0 n=0
Split into even and odd indices: gn := f2n and hn := f2n+1 (as in FFT)
• • • • • • • • • • • • and • • • •
• gn := f2n :
• hn := f2n+1 :
Michael Bader | Algorithms of Scientific Computing | Quarter-Wave DFT and DCT | Summer 2024 23
Compact Fast QW-DCT (→ Swarztrauber, 1986)
Consider QW-DCT: with symmetry f2N−n−1 = fn
2N−1 N−1 1
!
ek = 1
X −k (n+ 12 )
ek = 1
X πk n + 2
F fn ω2N −→ F fn cos .
2N N N
n=0 n=0
Split into even and odd indices: gn := f2n and hn := f2n+1 (as in FFT)
• • • • • • • • • • • • and • • • •
• gn := f2n :
• hn := f2n+1 :
e.g., for N = 4: • • • • 0 • • •
k =0 k =7
2N−1 N−1 1
!
X
i2π (n+ 21 )k/2N
X πk n + 2
fn := F
ek e −→ fn = F
e0 + 2 F
ek cos
N
k=0 k=1
Michael Bader | Algorithms of Scientific Computing | Quarter-Wave DFT and DCT | Summer 2024 24
Compact Fast Inverse QW-DCT
e2N−k = −F
Consider backward transform: with symmetry F ek
e.g., for N = 4: • • • • 0 • • •
k =0 k =7
2N−1 N−1 1
!
X
i2π (n+ 21 )k/2N
X πk n + 2
fn := F
ek e −→ fn = F
e0 + 2 F
ek cos
N
k=0 k=1
Michael Bader | Algorithms of Scientific Computing | Quarter-Wave DFT and DCT | Summer 2024 24
Compact Fast Inverse QW-DCT
e2N−k = −F
Consider backward transform: with symmetry F ek
e.g., for N = 4: • • • • 0 • • •
k =0 k =7
2N−1 N−1 1
!
X
i2π (n+ 21 )k/2N
X πk n + 2
fn := F
ek e −→ fn = F
e0 + 2 F
ek cos
N
k=0 k=1
−Gk = −F
e2k = F
e2N−2k = F
e2(N−k ) = GN−k
Michael Bader | Algorithms of Scientific Computing | Quarter-Wave DFT and DCT | Summer 2024 24
Compact Fast Inverse QW-DCT
e2N−k = −F
Consider backward transform: with symmetry F ek
e.g., for N = 4: • • • • 0 • • •
k =0 k =7
2N−1 N−1 1
!
X
i2π (n+ 21 )k/2N
X πk n + 2
fn := F
ek e −→ fn = F
e0 + 2 F
ek cos
N
k=0 k=1
−Gk = −F
e2k = F
e2N−2k = F
e2(N−k ) = GN−k
• Hk := F
e2k+1 :
−Hk = −F
e2k +1 = F
e2N−(2k+1) = F
e2(N−k)−1 = F
e2(N−k −1)+1 = HN−k−1
(next even/odd split leads to two real DFTs with symm. data sets)
Michael Bader | Algorithms of Scientific Computing | Quarter-Wave DFT and DCT | Summer 2024 24
Compact Fast Inverse QW-DCT
e2N−k = −F
Consider backward transform: with symmetry F ek
e.g., for N = 4: • • • • 0 • • •
k =0 k =7
2N−1 N−1 1
!
X
i2π (n+ 21 )k/2N
X πk n + 2
fn := F
ek e −→ fn = F
e0 + 2 F
ek cos
N
k=0 k=1
−Gk = −F
e2k = F
e2N−2k = F
e2(N−k ) = GN−k
• Hk := 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
(next even/odd split leads to two real DFTs with symm. data sets)
Michael Bader | Algorithms of Scientific Computing | Quarter-Wave DFT and DCT | Summer 2024 24