Algorithms of Scientific ComputingFast Real DFT Computation of a real-valued DFT using complex FFT...

19
Algorithms of Scientific Computing FFT on Real Data Michael Bader Technical University of Munich Summer 2016

Transcript of Algorithms of Scientific ComputingFast Real DFT Computation of a real-valued DFT using complex FFT...

Page 1: Algorithms of Scientific ComputingFast Real DFT Computation of a real-valued DFT using complex FFT is inefficient: N redundant components computed (symmetry) complex arithmetics

Algorithms of Scientific Computing

FFT on Real Data

Michael BaderTechnical University of Munich

Summer 2016

Page 2: Algorithms of Scientific ComputingFast Real DFT Computation of a real-valued DFT using complex FFT is inefficient: N redundant components computed (symmetry) complex arithmetics

DFT and Symmetry – Outlook

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 | FFT on Real Data | Summer 2016 2

Page 3: Algorithms of Scientific ComputingFast Real DFT Computation of a real-valued DFT using complex FFT is inefficient: N redundant components computed (symmetry) complex arithmetics

Real-valued DFT (RDFT)Consider real-valued input data fn ∈ R, i.e.: f ∗n := fn = fn, then:

Fk =1N

N2∑

n=− N2 +1

fne−i2πnk/N =1N

N2∑

n=− N2 +1

fn

(cos

(2πnk

N

)− i sin

(2πnk

N

)).

Properties:

• Re {Fk} = 1N

N2∑

n=− N2 +1

fn cos( 2πnk

N

), Im {Fk} = − 1

N

N2∑

n=− N2 +1

fn sin( 2πnk

N

)• only N independent, real coefficients necessary, since:

F ∗k =1N

∑f ∗n{ω−nk

N

}∗=

1N

∑fnω−n(−k)N = F−k

Recall:{ω−nk

N

}∗={

e−i2πnk/N}∗ = cos(−2πnk/N)− i sin(−2πnk/N)

= cos(2πnk/N) + i sin(2πnk/N) = · · · = ωnkN

Michael Bader | Algorithms of Scientific Computing | FFT on Real Data | Summer 2016 3

Page 4: Algorithms of Scientific ComputingFast Real DFT Computation of a real-valued DFT using complex FFT is inefficient: N redundant components computed (symmetry) complex arithmetics

Real DFT (2)

Map N values to N coefficients (and vice versa):{f− N

2 +1, . . . , f0, . . . , f N2

}DFT ⇓ ⇑ IDFT

{F0,Re{F1}, Im{F1}, . . . , Re{F N

2−1}, Im{F N2−1},F N

2

}Note: real and imaginary parts of F−k correspond to those of Fk

Michael Bader | Algorithms of Scientific Computing | FFT on Real Data | Summer 2016 4

Page 5: Algorithms of Scientific ComputingFast Real DFT Computation of a real-valued DFT using complex FFT is inefficient: N redundant components computed (symmetry) complex arithmetics

Real DFT (3)

Situation:• only N real input values (as all N imaginary parts are 0)• only N independent, real output values (coefficient components)

due to symmetry F−k = F ∗k

Wanted→ new transformation:

N real input values→ N distinct real coefficient components

Hence: Insert symmetry F−k = F ∗k in IDFT!

Michael Bader | Algorithms of Scientific Computing | FFT on Real Data | Summer 2016 5

Page 6: Algorithms of Scientific ComputingFast Real DFT Computation of a real-valued DFT using complex FFT is inefficient: N redundant components computed (symmetry) complex arithmetics

Real DFT (4)Definition of “Real discrete Fourier transform” (RDFT)

• formulation 1

Fk =1N

N2∑

n=− N2 +1

fn

(cos

(2πnk

N

)− i sin

(2πnk

N

)), k = 0, . . . ,

N2

• formulation 2

Re{Fk} =1N

N2∑

n=− N2 +1

fn cos(

2πnkN

), k = 0, . . . ,

N2

Im{Fk} = − 1N

N2∑

n=− N2 +1

fn sin(

2πnkN

), k = 1, . . . ,

N2− 1

Michael Bader | Algorithms of Scientific Computing | FFT on Real Data | Summer 2016 6

Page 7: Algorithms of Scientific ComputingFast Real DFT Computation of a real-valued DFT using complex FFT is inefficient: N redundant components computed (symmetry) complex arithmetics

Inverse Real DFTGoal: compute a real representation of the DFT (i.e., no e−i2πnk/N ).

We start with an input vector (Fourier coefficients) of length 2N, then:

fn =N∑

k=−N+1

Fk ei2πnk/2N

= F0 +N−1∑k=1

(Fk ei2πnk/2N + F−k e−i2πnk/2N

)+ FNei2πnN/2N

= F0 +N−1∑k=1

(Fk ei2πnk/2N +

{Fk ei2πnk/2N

}∗)+ FNeiπn

= F0 + 2N−1∑k=1

Re{

Fk ei2πnk/2N}+ FNeiπn

= F0 + 2N−1∑k=1

(Re{Fk} cos

(πnkN

)− Im{Fk} sin

(πnkN

))+ FN cos (πn)

Michael Bader | Algorithms of Scientific Computing | FFT on Real Data | Summer 2016 7

Page 8: Algorithms of Scientific ComputingFast Real DFT Computation of a real-valued DFT using complex FFT is inefficient: N redundant components computed (symmetry) complex arithmetics

Inverse Real DFT (2)

Set ak := 2 Re{Fk} and bk := −2 Im{Fk} (but a0 := Re{F0} andaN := Re{FN}) to get :

fn = a0 +N−1∑k=1

(ak cos

(πnkN

)+ bk sin

(πnkN

))+ aN cos (πn)

“Real inverse discrete Fourier transform”

Using the (real-valued) formula for Fk :

ak =1N

N∑n=−N+1

fn cos(πnkN

), bk =

1N

N∑n=−N+1

fn sin(πnkN

)

but: a0 = 12N

∑. . . and aN = 1

2N

∑. . .

Michael Bader | Algorithms of Scientific Computing | FFT on Real Data | Summer 2016 8

Page 9: Algorithms of Scientific ComputingFast Real DFT Computation of a real-valued DFT using complex FFT is inefficient: N redundant components computed (symmetry) complex arithmetics

Inverse Real DFT – Compare with Textbooks

Alternate (probably more frequent) notation in textbooks:Set ak := 2 Re{Fk} and bk := −2 Im{Fk} to get

fn =12

a0 +N−1∑k=1

(ak cos

(πnkN

)+ bk sin

(πnkN

))+

12

aN cos (πn)

Using the (real-valued) formula for Fk :

ak =1N

N∑n=−N+1

fn cos(πnkN

), bk =

1N

N∑n=−N+1

fn sin(πnkN

)Differences:• no extra definition of a0 and aN

• but factors 12 in formula for fn → not as nicely related to interpolation

Michael Bader | Algorithms of Scientific Computing | FFT on Real Data | Summer 2016 9

Page 10: Algorithms of Scientific ComputingFast Real DFT Computation of a real-valued DFT using complex FFT is inefficient: N redundant components computed (symmetry) complex arithmetics

Real-valued Trigonometric Interpolation

Interpretation of the real DFT as an interpolation problem:• 2N ansatz functions:

gk (x) := cos(kx) k = 0, . . . ,Nhk (x) := sin(kx) k = 1, . . . ,N − 1

• 2N supporting points: xn :=2πn2N

=πnN

n = −N + 1, . . . ,N

• 2N interpolation conditions:

fn = a0 +N−1∑k=1

(ak cos

(πnkN

)+ bk sin

(πnkN

))+ aN cos (πn)

(cmp. exercises)

Michael Bader | Algorithms of Scientific Computing | FFT on Real Data | Summer 2016 10

Page 11: Algorithms of Scientific ComputingFast Real DFT Computation of a real-valued DFT using complex FFT is inefficient: N redundant components computed (symmetry) complex arithmetics

Fast Real DFT

Computation of a real-valued DFT using complex FFT is inefficient:

• N redundant components computed (symmetry)• complex arithmetics with lots of real/imaginary parts being 0

Possibilities to improve the efficiency:1. compute two real DFTs from one complex FFT2. compute a real DFT of length 2N from one complex FFT of length N3. “compact” real FFT – use symmetry of the data directly in the algorithm

Michael Bader | Algorithms of Scientific Computing | FFT on Real Data | Summer 2016 11

Page 12: Algorithms of Scientific ComputingFast Real DFT Computation of a real-valued DFT using complex FFT is inefficient: N redundant components computed (symmetry) complex arithmetics

Two Real DFTs from one complex FFT

Idea: for real-valued gn and hn, compute DFT of fn := gn + ihn:

Fk =1N

∑n

(gn + ihn)ω−nkN

Comparison with coefficients Gk and Hk of the two real DFTs:

Gk =1N

∑n

gnω−nkN Hk =

1N

∑n

hnω−nkN

Due to linearity of the Fourier transform:

Fk = Gk + iHk

Michael Bader | Algorithms of Scientific Computing | FFT on Real Data | Summer 2016 12

Page 13: Algorithms of Scientific ComputingFast Real DFT Computation of a real-valued DFT using complex FFT is inefficient: N redundant components computed (symmetry) complex arithmetics

Two Real DFTs from one complex FFT (2)

Since gn and hn are real data, we have the following symmetry:

Gk = G∗−k Hk = H∗−k .

Hence, we get for F ∗−k :

F ∗−k = (G−k + iH−k )∗ =

(G∗−k + i∗H∗−k

)= Gk − iHk .

Together with Fk = Gk + iHk , we obtain

Gk =12(Fk + F ∗−k

)and Hk = − i

2(Fk − F ∗−k

).

Michael Bader | Algorithms of Scientific Computing | FFT on Real Data | Summer 2016 13

Page 14: Algorithms of Scientific ComputingFast Real DFT Computation of a real-valued DFT using complex FFT is inefficient: N redundant components computed (symmetry) complex arithmetics

Two real DFTs from one complex FFT – Algorithm

Algorithm to compute two real DFTs:(1) set fn := gn + ihn

(2) compute Fk from FFT (using a library, e.g.)(3) compute Gk and Hk according to

Gk =12(Fk + F ∗−k

)and Hk = − i

2(Fk − F ∗−k

)⇒ “half” the costs compared to using complex FFT

but: additional operations for pre- and postprocessing

Michael Bader | Algorithms of Scientific Computing | FFT on Real Data | Summer 2016 14

Page 15: Algorithms of Scientific ComputingFast Real DFT Computation of a real-valued DFT using complex FFT is inefficient: N redundant components computed (symmetry) complex arithmetics

Real DFT of length 2N from complex FFT oflength N

Compute DFT of a real-valued vector (f−N+1, . . . , fN):

Fk =1

2N

N∑−N+1

fnω−nk2N for k = −N

2+ 1, . . . ,

N2

Split up in gn := f2n and hn := f2n−1; leads to butterfly scheme:

Fk = 12

(Gk + ωk

2NHk

),

Fk±N = 12

(Gk − ωk

2NHk

)for k = −N

2 + 1, . . . , N2 , respectively.

Michael Bader | Algorithms of Scientific Computing | FFT on Real Data | Summer 2016 15

Page 16: Algorithms of Scientific ComputingFast Real DFT Computation of a real-valued DFT using complex FFT is inefficient: N redundant components computed (symmetry) complex arithmetics

Real 2N-DFT from complex N-FFT

Now: compute Gk and Hk (two real DFTs) from one complex FFT→ applied to zn := gn + ihn = f2n + i f2n−1:

Gk = 12

(Zk + Z ∗−k

)and Hk = − i

2

(Zk − Z ∗−k

)Combine both schemes to:

Fk = 14 Zk

(1− iωk

2N

)+ 1

4 Z ∗−k

(1 + iωk

2N

), k = 0, . . . , N

2

Fk+N = 14 Zk

(1 + iωk

2N

)+ 1

4 Z ∗−k

(1− iωk

2N

), k = −N

2 + 1, . . . ,0

Michael Bader | Algorithms of Scientific Computing | FFT on Real Data | Summer 2016 16

Page 17: Algorithms of Scientific ComputingFast Real DFT Computation of a real-valued DFT using complex FFT is inefficient: N redundant components computed (symmetry) complex arithmetics

Real 2N-DFT from complex N-FFT – Algorithm

Algorithm for a real 2N-DFT:(1) set zn := f2n + i f2n−1

(2) compute Zk from FFT applied on zn (using a library, e.g.)(3) compute Fk according to

Fk = 14 Zk

(1− iωk

2N

)+ 1

4 Z ∗−k

(1 + iωk

2N

), k = 0, . . . , N

2

Fk+N = 14 Zk

(1 + iωk

2N

)+ 1

4 Z ∗−k

(1− iωk

2N

), k = −N

2 + 1, . . . ,0

⇒ Complexity determined by complex N-FFT

plus: additional operations for pre- and postprocessing

Michael Bader | Algorithms of Scientific Computing | FFT on Real Data | Summer 2016 17

Page 18: Algorithms of Scientific ComputingFast Real DFT Computation of a real-valued DFT using complex FFT is inefficient: N redundant components computed (symmetry) complex arithmetics

Compact Real FFT

Compute DFT of a real-valued vector (f−N+1, . . . , fN):

Fk =1

2N

N∑−N+1

fnω−nk2N for k = 0, . . . ,N

Split up in gn := f2n and hn := f2n−1; leads to butterfly scheme:

Fk = 12

(Gk + ωk

2NHk

)for k = 0, . . . , N

2 ,

Fk+N = 12

(Gk − ωk

2NHk

)for k = −N

2 + 1, . . . ,0 .

Gk and Hk are coefficients of a real-valued DFT of length N; hence:

Gk = G∗−k and Hk = H∗−k for k = 0, . . . , N2 − 1

Michael Bader | Algorithms of Scientific Computing | FFT on Real Data | Summer 2016 18

Page 19: Algorithms of Scientific ComputingFast Real DFT Computation of a real-valued DFT using complex FFT is inefficient: N redundant components computed (symmetry) complex arithmetics

Compact Real-valued FFT (2)

Use symmetry of Gk and Hk for the computation of Fk :

Fk = 12

(Gk + ωk

2NHk

)fur k = 0, . . . , N

2 ,

FN−k = 12

(G−k − ω−k

2N H−k

)= 1

2

(Gk − ωk

2NHk

)∗for k = 0, . . . , N

2 − 1

⇒ Computation of Fk (for k = 0, . . . ,N) reduced to the computation of Gkand Hk (for k = 0, . . . , N

2 , respectively).

“Edson’s algorithm” (1968)

Michael Bader | Algorithms of Scientific Computing | FFT on Real Data | Summer 2016 19