Advanced Algorithms & Data Structures: Lecturer: Karimzhan Nurlan Berlibekuly
Advanced Algorithms & Data Structures: Lecturer: Karimzhan Nurlan Berlibekuly
Advanced Algorithms & Data Structures: Lecturer: Karimzhan Nurlan Berlibekuly
ALGORITHMS &
DATA STRUCTURES
Lecture-05
Fast Fourier Transform (FFT)
The degree of A is n – 1.
Fast Fourier Transform
Fast Fourier Transformation for polynomial
multiplication
O(n2)
Fast Fourier Transform
Example:
B(x) = -2x3 + 4x – 5
Coefficient representation of A(x) = (9, -10, 7, 6)
Coefficient representation of B(x) = (-5, 4, 0, -2)
Fast Fourier Transform
Input :
A[] = {9, -10, 7, 6}
B[] = {-5, 4, 0, -2}
Output :
Fast Fourier Transform
We can do better, if we represent the polynomial in
another form.
A(x) = x3 - 2x + 1
xi : 0, 1, 2, 3
A(xi) : 1, 0, 5, 22
Fast Fourier Transform
Point-value representation of above polynomial is { (0, 1), (1, 0), (2, 5), (3, 22) }.
Using Horner’s method, n-point evaluation takes time O(n2). It’s just calculation
of values of A(x) at some x for n different points, so time complexity is O(n 2).
Now that the polynomial is converted into point value, it can be easily calculated
C(x) = A(x)*B(x) again using horner’s method. This takes O(n) time. An
important point here is C(x) has degree bound 2n, then n points will give only n
points of C(x), so for that case we need 2n different values of x to calculate 2n
different values of y. Now that the product is calculated, the answer can to be
converted back into coefficient vector form. To get back to coefficient vector
form we use inverse of this evaluation. The inverse of evaluation is called
interpolation. Interpolation using Lagrange’s formula gives point value-form to
coefficient vector form of the polynomial.
Fast Fourier Transform
Lagrange’s formula is –
Fast Fourier Transform
O(n2)
Discrete Fourier transform
(DFT)
This idea still solves the problem in O(n2) time complexity. We can
use any points we want as evaluation points, but by choosing the
evaluation points carefully, we can convert between
representations in only O(n log n) time. If we choose “complex
roots of unity” as the evaluation points, we can produce a point-
value representation by taking the discrete Fourier transform
(DFT) of a coefficient vector. We can perform the inverse
operation, interpolation, by taking the “inverse DFT” of point-value
pairs, yielding a coefficient vector. Fast Fourier Transform (FFT) can
perform DFT and inverse DFT in time O(nlogn).
Discrete Fourier transform
(DFT)
DFT is evaluating values of polynomial at n complex nth roots of
unity
So, for
k = 0, 1, 2, …, n-1, y = (y0, y1, y2, …, yn-1) is Discrete fourier
Transformation (DFT) of given polynomial.
The product of two polynomials of degree-bound n is a polynomial
of degree-bound 2n. Before evaluating the input polynomials A
and B, therefore, we first double their degree-bounds to 2n by
adding n high-order coefficients of 0. Because the vectors have 2n
elements, we use “complex 2nth roots of unity, ” which are
denoted by the W2n (omega 2n). We assume that n is a power of
2; we can always meet this requirement by adding high-order zero
coefficients.
Fast Fourier Transform
Here is the Divide-and-conquer strategy to solve this problem.
Define two new polynomials of degree-bound n/2, using even-index and odd-index
coefficients of A(x) separately
Fast Fourier Transform
The problem of evaluating A(x) at
reduces to evaluating the degree-bound n/2 polynomials A0(x) and A1(x) at the
points
does not contain n distinct values, but n/2 complex n/2th roots of unity.
Polynomials A0 and A1 are recursively evaluated at the n complex nth roots of
unity. Subproblems have exactly the same form as the original problem, but are
half the size. So recurrence formed is T(n) = 2T(n/2) + O(n), i.e complexity
O(nlogn).
Fast Fourier Transform
Algorithm
1. Add n higher-order zero coefficients to A(x) and B(x)
2. Evaluate A(x) and B(x) using FFT for 2n points
3. Pointwise multiplication of point-value forms
4. Interpolate C(x) using FFT to compute inverse DFT
Pseudo code of recursive FFT
Recursive_FFT(a){ y0 = Recursive_FFT(A0) // local array
n = length(a) // a is the input coefficient y1 = Recursive-FFT(A1) // local array
vector
if n = 1 for k = 0 to n/2 - 1
then return a
// y array stores values of the DFT
// wn is principle complex nth root of unity. // of given polynomial.
wn = e^(2*pi*i/n) do y[k] = y0[k] + w*y1[k]
w=1 y[k+(n/2)] = y0[k] - w*y1[k]
w = w*wn
// even indexed coefficients return y
A0 = (a0, a2, ..., an-2 ) }
since,
Thus, the vector y returned by Recursive-FFT is indeed the DFT of the input vector a.
FFT - Source Code (1)
#include <bits/stdc++.h> vector<cd> A0(n / 2), A1(n / 2);
using namespace std; for (int i = 0; i < n / 2; i++) {
Interpolation
Switch the roles of a and y.
Replace wn by wn^-1.
Divide each element of the result by n.
Time Complexity : O(nlogn).
Fast Fourier Transform
Applications
Fourier (frequency) space many applications. The polynomial A∗ = FFT(A) is
complex, and the amplitude |a∗ k| represents the amplitude of the frequency-k signal,
while arg(ak ∗) (the angle of the 2D vector) represents the phase shift of that signal.
For example, this perspective is particularly useful for audio processing, as used by
Adobe Audition, Audacity, etc.:
https://e-maxx.ru/algo/fft_multiply
https://habr.com/ru/company/otus/blog/449996/