0% found this document useful (0 votes)
53 views31 pages

Algorithms of Scientific Computing: The Quarter-Wave DFT and The (Quarter-Wave) Discrete Cosine Transform (QW-DCT)

Uploaded by

Selen Çiğdem
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
53 views31 pages

Algorithms of Scientific Computing: The Quarter-Wave DFT and The (Quarter-Wave) Discrete Cosine Transform (QW-DCT)

Uploaded by

Selen Çiğdem
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 31

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

• Discrete Fourier transform


• Fast Fourier transform
• Special Fourier transforms:
– real-valued FFT
– sine/cosine transform
• Applications:
– Fast Poisson solver (FST)
– Computergraphics (FCT)
• Efficient Implementation

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)

Our next topics therefore:


• What is a “quarter-wave” transform?
• What is a “cosine transform”?

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

For a 2π-periodic function f , the corresponding Fourier series is defined as

∞ Z2π
X 1
f (x) ∼ ikx
ck e , where ck = f (x)e−ikx dx

k =−∞ 0

The ck are called (continuous) Fourier coefficients.

If f is piecewise smooth, the Fourier series converges pointwise


(i.e. for each x) towards
1 + −
2 (f (x ) + f (x )),

i.e. in particular towards f (x), if f is continuously differentiable at x.

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


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

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

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.

But we are dealing with complex numbers!


• vector scalar product is therefore v H w = v T ∗ w

1
Z
∗
• scalar product on functions is thus hf (x), g(x)i := f (x) g(x) dx

• thus: eikx are orthonormal basis functions!

The Fourier coefficients ck are thus computed via an orthogonal projection:


Z2π
D E 1
ck = eikx , f (x) = e−ikx f (x) dx

0

Michael Bader | Algorithms of Scientific Computing | Quarter-Wave DFT and DCT | Summer 2024 7
Approximate Computation of ck

The continuous Fourier coefficients are given as

Z2π
1
ck = f (x)e−ikx dx

0

Steps to compute ck approximately:


K
X
• consider ck only for ±k = 0, . . . , K ; then: f (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 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

Use g(x) := f (x)e−ikx and fn := f (xn ), then:


N−1
!
1 n o 1 1 0 X 1
ck ≈ TN f (x)e−ikx = f0 e + fn e−i2πnk /N + fN e−i2πNk/N
2π N 2 2
n=1
N−1
!
1 f0 X fN
= + fn e−i2πnk /N +
N 2 2
n=1

Michael Bader | Algorithms of Scientific Computing | Quarter-Wave DFT and DCT | Summer 2024 9
Computation of ck via Trapezoidal Sum (2)

If f0 = fN (periodic data), we obtain


N−1
1 X
ck ≈ Fk = 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 DCT | Summer 2024 10
Computation of ck via Midpoint Rule

Midpoint rule: evaluate g(x) at midpoints xn :

Z2π N−1 1

2π X 2π n + 2
g(x) dx ≈ g(xn ) with xn := .
N N
0 n=0

With g(x) := f (x)e−ikx and fn := f (xn ), we obtain:


N−1
ek := 1 1
fn e−i2π(n+ 2 )k/N
X
ck ≈ F
N
n=0

“Quarter-Wave Discrete Fourier Transform”

Michael Bader | Algorithms of Scientific Computing | Quarter-Wave DFT and DCT | Summer 2024 11
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 DCT | Summer 2024 12
Quarter-Wave Discrete Fourier Transform

• new variant of DFT:


N−1 N−1
1 X 1
ek ei2π(n+ 21 )k /N
fn e−i2π(n+ 2 )k/N
X
Fk :=
e fn := F
N
n=0 k=0

• Comparison with coefficients Fk of the “usual” DFT:

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

f2N−n−1 = fn e.g., for N = 4: • • • • • • • •


n=0 n=2N−1

Inserting the symmetric data in Quarter-Wave DFT results in


2N−1
1 X −k (n+ 21 )
F
ek = fn ω2N
2N
n=0
N−1 N−1
1 X 1 X
−k (n+ 21 ) −k (2N−n−1+ 12 )
= fn ω2N + f2N−n−1 ω2N
2N 2N
n=0 n=0
N−1 N−1 !
πk n + 12
 
1 X −k (n+ 21 ) −k (−n− 12 ) 1 X
= fn ω2N + ω2N = fn cos .
2N N N
n=0 n=0

Michael Bader | Algorithms of Scientific Computing | Quarter-Wave DFT and DCT | Summer 2024 14
Quarter-Wave DFT on Symmetric Data (2)

Quarter-Wave DFT of symmetric data results in real-valued coefficients:


N−1 !
1 X πk n + 12
Fk =
e fn cos for k = 0, . . . , 2N − 1
N N
n=0

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

⇒ again: only N independent coefficients

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

We define a pair of transforms – (inverse) quarter-wave even DCT:


N−1 1
! N−1 1
!
1 X πk n + 2
X πk n + 2
F
ek = fn cos fn = F
e0 + 2 F
ek cos
N N N
n=0 k =1

N real values ←→ N real-valued coefficients


(no symmetry any more in data/coefficients!)

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

Definition of the 2D QW-DCT:


N−1 M−1 1
! 1
!
1 XX πk n + 2 πl m + 2
F
ekl = fnm cos cos
N ·M N M
n=0 m=0

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

Application: blockwise 2D QW-DCT in JPEG/MPEG compression

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

1. Apply a 1D QW-FCT for all n = 0, . . . , N − 1:


N−1 1
!
bnl = 1
X πl m + 2
F fnm cos
N N
m=0

(transform all “rows”).

2. Apply a 1D QW-FCT for all l = 0, . . . , N − 1:


N−1 1
!
ekl = 1
X πk n + 2
F F
bnl cos .
N N
n=0

(transform all “columns”).

Michael Bader | Algorithms of Scientific Computing | Quarter-Wave DFT and DCT | Summer 2024 20
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 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 :

gn = f2n = f2N−2n−1 = f2(N−n)−1 = f2(N−n−1)+1 = hN−n−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 :

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

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 :

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 DCT | Summer 2024 23
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

Split into even and odd indices: (as in FFT)


• Gk := F e2k :
−Gk = −Fe2k = Fe2N−2k = Fe2(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

Split into even and odd indices: (as in FFT)


• Gk := F e2k : again leads to Inverse QW-DCT

−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

Split into even and odd indices: (as in FFT)


• Gk := F e2k : again leads to Inverse QW-DCT

−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

Split into even and odd indices: (as in FFT)


• Gk := F e2k : again leads to Inverse QW-DCT

−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

You might also like