You are on page 1of 43

Fast Fourier Transform

Jordi Cortadella and Jordi Petit


Department of Computer Science
Sum of sinusoids

Fast Fourier Transform © Dept. CS, UPC 2


FFT application in Signal Processing

Converting a signal: time domain ↔ frequency domain


Fast Fourier Transform © Dept. CS, UPC 3
Fourier series
• Periodic function 𝑓(𝑡) of period 1:
∞ ∞
𝑎0
𝑓 𝑡 = + ෍ 𝑎𝑛 cos(2𝜋𝑛𝑡) + ෍ 𝑏𝑛 sin(2𝜋𝑛𝑡)
2
𝑛=1 𝑘=1

• Fourier coefficients:
𝑇 𝑇
𝑎𝑛 = 2 න 𝑓(𝑡) cos(2𝜋𝑛𝑡) 𝑑𝑡, 𝑏𝑛 = 2 න 𝑓(𝑥) sin 2𝜋𝑛𝑡 𝑑𝑡
0 0

• Fourier series is fundamental for signal analysis (to move from


time domain to frequency domain, and vice versa)

Fast Fourier Transform © Dept. CS, UPC 4


Why Fourier Transform?

Fast Fourier Transform © Dept. CS, UPC 5


Discrete Fourier Transform (DFT)
A method for converting a sequence of 𝑁
complex numbers 𝑥0 , 𝑥1 , … , 𝑥𝑁−1 to a new
sequence of 𝑁 complex numbers:
𝑁−1

𝑋𝑘 = ෍ 𝑥𝑛 𝑒 −2𝜋𝑖𝑘𝑛/𝑁
𝑛=0

such that
𝑁−1
1
𝑥𝑛 = ෍ 𝑋𝑘 𝑒 2𝜋𝑖𝑘𝑛/𝑁
𝑁
𝑘=0

Fast Fourier Transform © Dept. CS, UPC 6


Polynomials: coefficient representation

Fast Fourier Transform © Dept. CS, UPC 7


Polynomials: point-value representation
• Fundamental Theorem (Gauss): A degree 𝑛 polynomial
with complex coefficients has exactly 𝑛 complex roots.

• Corollary: A degree 𝑛 − 1 polymonial 𝐴(𝑥) is uniquely


identified by its evaluation at 𝑛 distinct values of 𝑥.

Fast Fourier Transform © Dept. CS, UPC 8


Polynomials: point-value representation

Fast Fourier Transform © Dept. CS, UPC 9


Lagrange formula: example
𝑃 𝑥 = { −1, −1 , 0, −2 , 2,2 }

𝑥 𝑥−2 𝑥+1 𝑥−2 𝑥+1 𝑥


𝑃 𝑥 =− −2 +2
3 −2 6

𝑃 𝑥 = 𝑥2 − 2

Fast Fourier Transform © Dept. CS, UPC 10


Conversion between both representations

representation addition multiplication evaluation


coefficient O(𝑛) O(𝑛2 ) O(𝑛)
point-value O(𝑛) O(𝑛) O(𝑛2 )

evaluation
𝑎0 , 𝑎1 , ⋯ , 𝑎𝑛−1 𝑥0 , 𝑦0 , ⋯ , 𝑥𝑛−1 , 𝑦𝑛−1
interpolation
Coefficient representation Point-value representation

Could we have an efficient algorithm to move from coefficient


to point-value representation and vice versa?

Fast Fourier Transform © Dept. CS, UPC 11


From coefficients to point-values

Horner’s rule:
𝑝 𝑥 = 𝑎0 + 𝑥 𝑎1 + 𝑥 𝑎2 + 𝑥 𝑎3 + ⋯ + 𝑥 𝑎𝑛−1 + 𝑥𝑎𝑛 ⋯

Fast Fourier Transform © Dept. CS, UPC 12


Evaluation by divide-and-conquer
• Credits: based on the intuitive explanation by Dasgupta,
Papadimitriou and Vazinari, Algorithms, McGraw-Hill, 2008.

• We want to evaluate 𝐴(𝑥) at 𝑛 different points. Let us choose


them to be positive-negative pairs: ±𝑥0 , ±𝑥1 , … , ±𝑥𝑛Τ2−1

• The computations for 𝐴(𝑥𝑖 ) and 𝐴(−𝑥𝑖 ) overlap a lot.

• Split the polynomial into odd and even powers


3 + 4𝑥 + 6𝑥 2 + 2𝑥 3 + 𝑥 4 + 10𝑥 5 = 3 + 6𝑥 2 + 𝑥 4 + 𝑥 4 + 2𝑥 2 + 10𝑥 4

• The terms in parenthesis are polynomials in 𝑥 2 :


𝐴 𝑥 = 𝐴𝑒 𝑥 2 + 𝑥𝐴𝑜 (𝑥 2 )

Fast Fourier Transform © Dept. CS, UPC 13


Evaluation by divide-and-conquer
• The calculations needed for 𝐴 𝑥𝑖 can be reused for
computing 𝐴 −𝑥𝑖 .

𝐴 𝑥𝑖 = 𝐴𝑒 𝑥𝑖2 + 𝑥𝑖 𝐴𝑜 𝑥𝑖2
𝐴 −𝑥𝑖 = 𝐴𝑒 𝑥𝑖2 − 𝑥𝑖 𝐴𝑜 𝑥𝑖2

• Evaluating 𝐴 𝑥 at 𝑛 paired points

±𝑥0 , ±𝑥1 , … , ±𝑥𝑛Τ2−1

reduces to evaluating 𝐴𝑒 𝑥 and 𝐴𝑜 (𝑥) at just


2
𝑛/2 points: 𝑥02 , ⋯ , 𝑥𝑛/2−1

Fast Fourier Transform © Dept. CS, UPC 14


Evaluation by divide-and-conquer
Evaluate: 𝐴 𝑥 +𝑥0 −𝑥0 +𝑥1 −𝑥1 ⋯ +𝑥𝑛/2−1 −𝑥𝑛/2−1
degree ≤ 𝑛 − 1

Evaluate:
𝐴𝑒 𝑥 and 𝐴𝑜 (𝑥) 𝑥02 𝑥12 ⋯ 2
𝑥𝑛/2−1
degree ≤ 𝑛/2 − 1

If we could recurse, we would get a running time:

𝑇 𝑛 = 2 ⋅ 𝑇 𝑛/2 + O 𝑛 = O(𝑛 log 𝑛)


But can we recurse?

Fast Fourier Transform © Dept. CS, UPC 15


Evaluation by divide-and-conquer
Evaluate: 𝐴 𝑥 +𝑥0 −𝑥0 +𝑥1 −𝑥1 ⋯ +𝑥𝑛/2−1 −𝑥𝑛/2−1
degree ≤ 𝑛 − 1

Evaluate:
𝐴𝑒 𝑥 and 𝐴𝑜 (𝑥) 𝑥02 𝑥12 ⋯ 2
𝑥𝑛/2−1
degree ≤ 𝑛/2 − 1

We need 𝑥02 and 𝑥12 to be a plus-minus pair.


The problem: ? But a square cannot be negative !

Fast Fourier Transform © Dept. CS, UPC 16


Selection of the evaluation points
+1 −1 +𝑖 −𝑖 + 𝑖 − 𝑖 + −𝑖 − −𝑖
+𝑥0 −𝑥0 +𝑥1 −𝑥1 +𝑥2 −𝑥2 +𝑥3 −𝑥3

𝑥02 +1 𝑥12 −1 𝑥22 +𝑖 𝑥32 −𝑖

𝑥04 +1 𝑥24 −1

Note:
1
𝑥08 +1 𝑖=±
2
1+𝑖
1
−𝑖 = ± (1 − 𝑖)
2
Fast Fourier Transform © Dept. CS, UPC 17
Selection of the evaluation points
+𝑖

− −𝑖 + 𝑖

−1 +1

− 𝑖 + −𝑖

−𝑖

Fast Fourier Transform © Dept. CS, UPC 18


Complex numbers: review
Imaginary

𝑧 = 𝑎 + 𝑏𝑖 𝑧 = 𝑟(cos 𝜃 + 𝑖 sin 𝜃) = 𝑟𝑒 𝑖𝜃
𝑏
Polar coordinates: (𝑟, 𝜃)

𝑟 Length: 𝑟 = 𝑎2 + 𝑏 2
𝑎 𝑏
Angle 𝜃 ∈ 0,2𝜋 : cos 𝜃 = , sin 𝜃 =
𝑟 𝑟
𝜃 can always be reduced modulo 2𝜋
𝜃
Real
𝑎
Some examples:
Number −1 𝑖 5 + 5𝑖
Polar coords (1, 𝜋) (1, 𝜋/2) (5 2, 𝜋Τ4)
Fast Fourier Transform © Dept. CS, UPC 19
Complex numbers: multiplication

(𝑟1 , 𝜃1 )
(𝑟2 , 𝜃2 )
𝑟1 , 𝜃1 × 𝑟2 , 𝜃2 = (𝑟1 𝑟2 , 𝜃1 + 𝜃2 )

(𝑟1 𝑟2 , 𝜃1 + 𝜃2 ) For any 𝑧 = 𝑟, 𝜃 :

−𝑧 = (𝑟, 𝜃 + 𝜋), since −1 = 1, 𝜋

If 𝑧 is on the unit circle, then 𝑧 𝑛 = (1, 𝑛𝜃)

Fast Fourier Transform © Dept. CS, UPC 20


Complex numbers: the 𝑛th roots of unity
Solutions to the equation 𝑧 𝑛 = 1
(𝑛 = 16)

Solutions are 𝑧 = 1, 𝜃 ,
4𝜋/𝑛
for 𝜃 a multiple of 2𝜋/𝑛
Angle 2𝜋/𝑛

All roots are plus-minus paired:

2𝜋 − 1, 𝜃 = (1, 𝜃 + 𝜋)
+𝜋
𝑛

Fast Fourier Transform © Dept. CS, UPC 21


Roots of unity for 𝑛 = 8
+𝑖

− −𝑖 + 𝑖

−1 +1

− 𝑖 + −𝑖

−𝑖

Fast Fourier Transform © Dept. CS, UPC 22


Divide-and-conquer step

Evaluate 𝐴𝑒 (𝑥) and 𝐴𝑜 𝑥


Evaluate 𝐴(𝑥) at 𝑛th roots of unity
at 𝑛Τ2 nd roots of unity

Fast Fourier Transform © Dept. CS, UPC 23


Divide-and-conquer steps

Fast Fourier Transform © Dept. CS, UPC 24


FFT algorithm
function FFT(𝑨,𝝎)
Inputs: 𝑨 = (𝒂𝟎 , 𝒂𝟏 , … , 𝒂𝒏−𝟏 ), for 𝒏 a power of 2
𝝎: A primitive 𝒏th root of unity
Output: 𝑨 𝟏 , 𝑨 𝝎 , 𝑨 𝝎𝟐 , … , 𝑨 𝝎𝒏−𝟏
if 𝝎=1: return 𝐴

𝑨𝒆 𝝎𝟎 , 𝑨𝒆 𝝎𝟐 , … , 𝑨𝒆 (𝝎𝒏−𝟐 ) = FFT(𝑨𝒆 , 𝝎𝟐 )
𝑨𝒐 𝝎𝟎 , 𝑨𝒐 𝝎𝟐 , … , 𝑨𝒐 (𝝎𝒏−𝟐 ) = FFT(𝑨𝒐 , 𝝎𝟐 )

for 𝒌 = 𝟎 to 𝒏 − 𝟏:
𝑨 𝝎𝒌 = 𝑨𝒆 𝝎𝟐𝒌 + 𝝎𝒌 𝑨𝒐 (𝝎𝟐𝒌 )

return 𝑨 𝟏 , 𝑨 𝝎 , 𝑨 𝝎𝟐 , … , 𝑨(𝝎𝒏−𝟏 )

Fast Fourier Transform © Dept. CS, UPC 25


FFT algorithm
for 𝒌 = 𝟎 to 𝒏 − 𝟏: 𝑨 𝝎𝒌 = 𝑨𝒆 𝝎𝟐𝒌 + 𝝎𝒌 𝑨𝒐 (𝝎𝟐𝒌 )

Example (𝒏 = 𝟖):
𝝎𝟐
𝐴 𝜔0 = 𝐴𝑒 𝜔 0 + 𝜔 0 𝐴𝑜 𝜔0
𝝎𝟑 𝝎𝟏
𝐴 𝜔1 = 𝐴𝑒 𝜔 2 + 𝜔1 𝐴𝑜 𝜔2
𝐴 𝜔2 = 𝐴𝑒 𝜔 4 + 𝜔 2 𝐴𝑜 𝜔4
𝐴 𝜔3 = 𝐴𝑒 𝜔 6 + 𝜔 3 𝐴𝑜 𝜔6
𝝎𝟒 𝝎𝟎
𝐴 𝜔4 = 𝐴𝑒 𝜔 8 + 𝜔 4 𝐴𝑜 𝜔8
𝐴 𝜔5 = 𝐴𝑒 𝜔10 + 𝜔 5 𝐴𝑜 𝜔10
𝐴 𝜔6 = 𝐴𝑒 𝜔12 + 𝜔 6 𝐴𝑜 𝜔12
𝐴 𝜔7 = 𝐴𝑒 𝜔14 + 𝜔 7 𝐴𝑜 𝜔14 𝝎𝟓 𝝎𝟕
𝝎𝟔
𝝎𝟒 = 𝝎𝟏𝟐 𝝎𝟐 = −𝝎𝟔
Fast Fourier Transform © Dept. CS, UPC 26
FFT algorithm
for 𝒌 = 𝟎 to 𝒏 − 𝟏: 𝑨 𝝎𝒌 = 𝑨𝒆 𝝎𝟐𝒌 + 𝝎𝒌 𝑨𝒐 (𝝎𝟐𝒌 )

Example (𝒏 = 𝟖):

𝐴 𝜔0 = 𝐴𝑒 𝜔 0 + 𝜔 0 𝐴𝑜 𝜔0 𝐴 𝜔0 = 𝐴𝑒 𝜔0 + 𝜔 0 𝐴𝑜 𝜔0
𝐴 𝜔1 = 𝐴𝑒 𝜔 2 + 𝜔1 𝐴𝑜 𝜔2 𝐴 𝜔1 = 𝐴𝑒 𝜔2 + 𝜔1 𝐴𝑜 𝜔2
𝐴 𝜔2 = 𝐴𝑒 𝜔 4 + 𝜔 2 𝐴𝑜 𝜔4 𝐴 𝜔2 = 𝐴𝑒 𝜔4 + 𝜔 2 𝐴𝑜 𝜔4
𝐴 𝜔3 = 𝐴𝑒 𝜔 6 + 𝜔 3 𝐴𝑜 𝜔6 𝐴 𝜔3 = 𝐴𝑒 𝜔6 + 𝜔 3 𝐴𝑜 𝜔6
𝐴 𝜔4 = 𝐴𝑒 𝜔 8 + 𝜔 4 𝐴𝑜 𝜔8 𝐴 𝜔4 = 𝐴𝑒 𝜔0 − 𝜔 0 𝐴𝑜 𝜔0
𝐴 𝜔5 = 𝐴𝑒 𝜔10 + 𝜔 5 𝐴𝑜 𝜔10 𝐴 𝜔5 = 𝐴𝑒 𝜔2 − 𝜔1 𝐴𝑜 𝜔2
𝐴 𝜔6 = 𝐴𝑒 𝜔12 + 𝜔 6 𝐴𝑜 𝜔12 𝐴 𝜔6 = 𝐴𝑒 𝜔4 − 𝜔 2 𝐴𝑜 𝜔4
𝐴 𝜔7 = 𝐴𝑒 𝜔14 + 𝜔 7 𝐴𝑜 𝜔14 𝐴 𝜔7 = 𝐴𝑒 𝜔6 − 𝜔 3 𝐴𝑜 𝜔6

𝝎𝟒 = 𝝎𝟏𝟐 𝝎𝟐 = −𝝎𝟔
Fast Fourier Transform © Dept. CS, UPC 27
FFT algorithm
for 𝒌 = 𝟎 to 𝒏 − 𝟏: 𝑨 𝝎𝒌 = 𝑨𝒆 𝝎𝟐𝒌 + 𝝎𝒌 𝑨𝒐 (𝝎𝟐𝒌 )

Example (𝒏 = 𝟖):

𝐴 𝜔0 = 𝐴𝑒 𝜔0 + 𝜔 0 𝐴𝑜 𝜔0 𝐴 𝜔0 = 𝐴𝑒 𝜔0 + 𝜔 0 𝐴𝑜 𝜔0
𝐴 𝜔4 = 𝐴𝑒 𝜔0 − 𝜔 0 𝐴𝑜 𝜔0 𝐴 𝜔1 = 𝐴𝑒 𝜔2 + 𝜔1 𝐴𝑜 𝜔2
𝐴 𝜔1 = 𝐴𝑒 𝜔2 + 𝜔1 𝐴𝑜 𝜔2 𝐴 𝜔2 = 𝐴𝑒 𝜔4 + 𝜔 2 𝐴𝑜 𝜔4
𝐴 𝜔5 = 𝐴𝑒 𝜔2 − 𝜔1 𝐴𝑜 𝜔2 𝐴 𝜔3 = 𝐴𝑒 𝜔6 + 𝜔 3 𝐴𝑜 𝜔6
𝐴 𝜔2 = 𝐴𝑒 𝜔4 + 𝜔 2 𝐴𝑜 𝜔4 𝐴 𝜔4 = 𝐴𝑒 𝜔0 − 𝜔 0 𝐴𝑜 𝜔0
𝐴 𝜔6 = 𝐴𝑒 𝜔4 − 𝜔 2 𝐴𝑜 𝜔4 𝐴 𝜔5 = 𝐴𝑒 𝜔2 − 𝜔1 𝐴𝑜 𝜔2
𝐴 𝜔3 = 𝐴𝑒 𝜔6 + 𝜔 3 𝐴𝑜 𝜔6 𝐴 𝜔6 = 𝐴𝑒 𝜔4 − 𝜔 2 𝐴𝑜 𝜔4
𝐴 𝜔7 = 𝐴𝑒 𝜔6 − 𝜔 3 𝐴𝑜 𝜔6 𝐴 𝜔7 = 𝐴𝑒 𝜔6 − 𝜔 3 𝐴𝑜 𝜔6

FFT shuffling
Fast Fourier Transform © Dept. CS, UPC 28
FFT algorithm
for 𝒌 = 𝟎 to 𝒏 − 𝟏: 𝑨 𝝎𝒌 = 𝑨𝒆 𝝎𝟐𝒌 + 𝝎𝒌 𝑨𝒐 (𝝎𝟐𝒌 )

Example (𝒏 = 𝟖):

𝐴 𝜔0 = 𝐴𝑒 𝜔0 + 𝜔 0 𝐴𝑜 𝜔0
𝐴 𝜔4 = 𝐴𝑒 𝜔0 − 𝜔 0 𝐴𝑜 𝜔0
𝐴 𝜔1 = 𝐴𝑒 𝜔2 + 𝜔1 𝐴𝑜 𝜔2 for 𝒌 = 𝟎 to 𝒏/𝟐 − 𝟏:
𝐴 𝜔5 = 𝐴𝑒 𝜔2 − 𝜔1 𝐴𝑜 𝜔2
𝑨 𝝎𝒌 = 𝑨𝒆 𝝎𝟐𝒌 + 𝝎𝒌 𝑨𝒐 𝝎𝟐𝒌
𝐴 𝜔2 = 𝐴𝑒 𝜔4 + 𝜔 2 𝐴𝑜 𝜔4
𝒏
𝐴 𝜔6 = 𝐴𝑒 𝜔4 − 𝜔 2 𝐴𝑜 𝜔4 𝑨 𝒌+
𝝎 𝟐 = 𝑨𝒆 𝝎𝟐𝒌 − 𝝎𝒌 𝑨𝒐 𝝎𝟐𝒌
𝐴 𝜔3 = 𝐴𝑒 𝜔6 + 𝜔 3 𝐴𝑜 𝜔6
𝐴 𝜔7 = 𝐴𝑒 𝜔6 − 𝜔 3 𝐴𝑜 𝜔6

Fast Fourier Transform © Dept. CS, UPC 29


FFT algorithm
function FFT(𝒂,𝝎)
Inputs: 𝒂 = (𝒂𝟎 , 𝒂𝟏 , … , 𝒂𝒏−𝟏 ), for 𝒏 a power of 2
𝝎: A primitive 𝒏th root of unity
Output: 𝒂 𝟏 , 𝒂 𝝎 , 𝒂 𝝎𝟐 , … , 𝒂 𝝎𝒏−𝟏
if 𝝎=1: return a // 𝒏 = 𝟏, 𝒂 has only one element

𝒔𝟎 , 𝒔𝟏 , … , 𝒔𝒏Τ𝟐−𝟏 = FFT( 𝒂𝟎 , 𝒂𝟐 , … , 𝒂𝒏−𝟐 , 𝝎𝟐 )


𝒔′𝟎 , 𝒔′𝟏 , … , 𝒔′𝒏Τ𝟐−𝟏 = FFT 𝒂𝟏 , 𝒂𝟑 , … , 𝒂𝒏−𝟏 , 𝝎𝟐

for 𝒌 = 𝟎 to 𝒏Τ𝟐 − 𝟏: // FFT shuffling


𝒓𝒌 = 𝒔𝒌 + 𝝎𝒌 𝒔′𝒌
𝒓𝒌+𝒏Τ𝟐 = 𝒔𝒌 − 𝝎𝒌 𝒔′𝒌

return (𝒓𝟎 , 𝒓𝟏 , … , 𝒓𝒏−𝟏 )


Fast Fourier Transform © Dept. CS, UPC 30
FFT: asymptotic complexity
• The runtime of the FFT can be expressed as:
𝑇 𝑛 = 2 ⋅ 𝑇 𝑛Τ2 + O 𝑛

• Using the Master Theorem we conclude:


Runtime FFT 𝑛 = O(𝑛 log 𝑛)

• Gilbert Strang (MIT, 1994):


“the most important numerical
algorithm of our lifetime”.

• Reference: Cooley, James W., and John W. Tukey, 1965,


“An algorithm for the machine calculation of complex
Fourier series,” Math. Comput. 19: 297-301.
Fast Fourier Transform © Dept. CS, UPC 31
Unfolding the FFT

𝑎0
𝑎2
+ 𝑟𝑘
⋮ 𝐹𝐹𝑇𝑛Τ2
𝜔𝑘
𝑎𝑛−2

𝑎1
𝑎3
𝜔 𝑘+𝑛Τ2 𝑟𝑘+𝑛Τ2
+
⋮ 𝐹𝐹𝑇𝑛Τ2

𝑎𝑛−1

Fast Fourier Transform © Dept. CS, UPC 32


Unfolding the FFT (butterfly diagram)
000 𝑎0 𝐴(𝜔0 ) 000

100 𝑎4
4
𝐴(𝜔1 ) 001

010 𝑎2 4
𝐴(𝜔2 ) 010
2
110 𝑎6 𝐴(𝜔3 ) 011
4 6

001 𝑎1 4 𝐴(𝜔4 ) 100


1
101 𝑎5 𝐴(𝜔5 ) 101
4 5
2
011 𝑎3 4 𝐴(𝜔6 ) 110
6
2 3
111 𝑎7 𝐴(𝜔7 ) 111
4 6 7
Fast Fourier Transform © Dept. CS, UPC 33
Why is it called a butterfly diagram?

Fast Fourier Transform © Dept. CS, UPC 34


Conversion between both representations

representation addition multiplication evaluation


coefficient O(𝑛) O(𝑛2 ) O(𝑛)
point-value O(𝑛) O(𝑛) O(𝑛2 )

values = FFT( coefficients , 𝜔)


evaluation
O(𝒏 𝐥𝐨𝐠 𝒏)
𝑎0 , 𝑎1 , ⋯ , 𝑎𝑛−1 𝑥0 , 𝑦0 , ⋯ , 𝑥𝑛−1 , 𝑦𝑛−1
interpolation
Coefficient representation Point-value representation

Fast Fourier Transform © Dept. CS, UPC 35


From point-values to coefficients

Fast Fourier Transform © Dept. CS, UPC 36


From point-values to coefficients

Fast Fourier Transform © Dept. CS, UPC 37


Conversion between both representations

representation addition multiplication evaluation


coefficient O(𝑛) O(𝑛2 ) O(𝑛)
point-value O(𝑛) O(𝑛) O(𝑛2 )

values = FFT( coefficients , 𝜔)


evaluation
𝑎0 , 𝑎1 , ⋯ , 𝑎𝑛−1 O(𝒏 𝐥𝐨𝐠 𝒏) 𝑥0 , 𝑦0 , ⋯ , 𝑥𝑛−1 , 𝑦𝑛−1
interpolation
Coefficient representation Point-value representation
1
coefficients = FFT( values , 𝜔−1 )
𝑛

Fast Fourier Transform © Dept. CS, UPC 38


Polynomial multiplication
Input: Coefficients of two polynomials 𝐴(𝑥) and 𝐵(𝑥),
of degree 𝑑𝐴 and 𝑑𝐵 , respectively. Let 𝑑 = 𝑑𝐴 + 𝑑𝐵 .
Output: The product 𝐶 = 𝐴 ⋅ 𝐵.

1. Selection:
 Pick 𝜔 = (1, 2𝜋Τ𝑛), such that 𝑛 ≥ 𝑑 + 1 and 𝑛 is a power of two.

2. Evaluation (FFT):
 Compute 𝐴 1 , 𝐴 𝜔 , 𝐴 𝜔2 , … , 𝐴 𝜔𝑛−1 .
 Compute 𝐵 1 , 𝐵 𝜔 , 𝐵 𝜔2 , … , 𝐵 𝜔𝑛−1 .

3. Multiplication:
 Compute 𝐶 𝜔𝑘 = 𝐴 𝜔𝑘 ⋅ 𝐵(𝜔𝑘 ), for all 𝑘 = 0, … , 𝑛 − 1.

4. Interpolation (inverse FFT):


 Recover 𝐶 𝑥 = 𝑐0 + 𝑐1 𝑥 + 𝑐2 𝑥 2 + ⋯ + 𝑐𝑑 𝑥 𝑑 .

Fast Fourier Transform © Dept. CS, UPC 39


Example: from values to coefficients
• Let us consider a polynomial:
𝑃 𝑥 = 𝑎0 + 𝑎1 𝑥 + 𝑎2 𝑥 2 + 𝑎3 𝑥 3

• We have 𝑛 = 4 and 𝜔 = 𝑖. Let us assume that the


evaluation at four different points is:
𝑃 1 =2
𝑃 𝑖 =1−𝑖
𝑃 −1 =4
𝑃 −𝑖 =1+𝑖

• We want to calculate the coefficients (𝑎0 , 𝑎1 , 𝑎2 , 𝑎3 ) using


the inverse FFT, i.e.,
1
[a0 , a1 , a2 , a3 ] = FFT( 2, 1 − 𝑖, 4, 1 + 𝑖 , 𝜔−1 )
4
Fast Fourier Transform © Dept. CS, UPC 40
Example: from values to coefficients
FFT( 2,1 − 𝑖, 4,1 + 𝑖 , 𝜔 = −𝑖)

𝑘 = 0 ቊ 𝑟0 = 6 + 2 = 8
𝑟2 = 6 − 2 = 4
𝑟1 = −2 + −𝑖 −2𝑖 = −4
𝑘 = 1 ቊ 𝑟 = −2 − −𝑖 −2𝑖 = 0
3

FFT( 2,4 , 𝜔 = −1) FFT( 1 − 𝑖, 1 + 𝑖 , 𝜔 = −1)

𝑟0 = 2 + 4 = 6 𝑟0 = 1 − 𝑖 + 1 + 𝑖 = 2
𝑟1 = 2 − 4 = −2 𝑟1 = 1 − 𝑖 − 1 − 𝑖 = −2𝑖

FFT( 2 , 𝜔 = 1) FFT( 4 , 𝜔 = 1) FFT( 1 − 𝑖 , 𝜔 = 1) FFT( 1 + 𝑖 , 𝜔 = 1)


2 4 1−𝑖 1+𝑖

1
𝑎0 , 𝑎1 , 𝑎2 , 𝑎3 = [8, −4,4,0] 𝑃 𝑥 = 𝑥2 − 𝑥 + 2
4
Fast Fourier Transform © Dept. CS, UPC 41
Example: from coefficients to values
𝑃 𝑥 = 𝑥2 − 𝑥 + 2 FFT( 2, −1,1,0 , 𝜔 = 𝑖)

𝑘 = 0 ቊ 𝑟0 = 3 − 1 = 2
𝑟2 = 3 + 1 = 4
𝑟1 = 1 + 𝑖 ⋅ −1 = 1 − 𝑖
𝑘 = 1 ቊ 𝑟 = 1 − 𝑖 ⋅ −1 = 1 + 𝑖
3

FFT( 2,1 , 𝜔 = −1) FFT( −1,0 , 𝜔 = −1)

𝑟0 = 2 + 1 = 3 𝑟0 = −1 + 0 = −1
𝑟1 = 2 − 1 = 1 𝑟1 = −1 − 0 = −1

FFT( 2 , 𝜔 = 1) FFT( 1 , 𝜔 = 1) FFT( −1 , 𝜔 = 1) FFT( 0 , 𝜔 = 1)


2 1 −1 0

𝑦0 , 𝑦1 , 𝑦2 , 𝑦3 = FFT( 𝑎0 , 𝑎1 , 𝑎2 , 𝑎3 , 𝜔 = 𝑖) 𝑦0 , 𝑦1 , 𝑦2 , 𝑦3 = [2,1 − 𝑖, 4,1 + 𝑖]

Fast Fourier Transform © Dept. CS, UPC 42


Exercises
1. Consider the polynomials 1 + 𝑥 − 2𝑥 2 + 𝑥 3 and −1 + 𝑥 2 :
– Choose an appropriate power of two to execute the FFT for the
polynomial multiplication. Find the value of 𝜔.
– Give the result of the FFT for 𝑥 2 − 1
(no need to execute the FFT).

2. Consider the polynomials −1 + 2𝑥 + 𝑥 2 and 1 + 2𝑥:


– Choose an appropriate power of two to execute the FFT.
Find the value of 𝜔.
– Calculate their point-value representation using the FFT
(execute the FFT algorithm manually).
– Calculate the product of the point-value representations.
– Execute the inverse FFT to obtain the coefficients of the
product.

Fast Fourier Transform © Dept. CS, UPC 43

You might also like