What is FFT (Cooley Tukey Algorithm)?

Fourier transformFourier transforms tries to represents a function using sinusoides. is used in almost every signal processing and data compression application. The algorithm used to calculate the discrete version of the Fourier transform is the Fast Fourier Transform (FFT).

It is used in data analysis, audio and visual data compression, and signal denoising. It is also used in calculating derivatives which in turn is used to solve PDEs and other scientific computing. Before diving into the details of the FFT, we shall take a look at Discrete Fourier Transform (DFT) as the FFT is just a faster way to calculate DFT.

Discrete Fourier Transform (DFT)

We know that Fourier transform tries to approximate a continuous function into the sum of sines and cosines. But computers work with discrete data. For example, a continuous signal will be stored in a computer by taking samples from the signal at some sampling rate. These samples form the discrete data that computers work with. These samples can also be approximated as a sum of sines and cosines. DFT returns a series of transforms. It can be calculated by the following equation:

Here, y^k\hat{y}_k is the kthk^{th} transform and xjx_j represents the jthj^{th} element in nn discrete values. The complex term ei2πjk/ne^{-i2\pi jk/n} tells us the type of sines and cosines that will be used in approximation using jj and kk, which are integers. This term is generalized as:

This is called fundamental frequency for nn discrete values.

The inverse of DFT is pretty similar to the equation above. It is given as:

Matrix formula

We are multiplying nn terms and summing them for one transform. This can be simplified by using matrix multiplication as follows:

The big matrix in the middle is just a simplified version of the equations for every kthk^{th} transform. It is represented by FnF_n. The same can be applied to the inverse of DFT.

For nn transforms, we will have n2n^2 multiplications. This simple calculation will be of the order O(n2)O(n^2), which is really expensive for large values of nn. This is where FFT comes to play its role.

Fast Fourier Transform (FFT)

As mentioned earlier, FFT is just a fast way of calculating DFT. There are many implementations for this, but the most used one is the Cooley Tukey algorithm. FFT allows us to compute DFT in O(nlogn)O(n\log n) using divide-and-conquer.

For simplicity, we assume that nn is a number in powers of two.

Pseudocode

This divide-and-conquer algorithm recursively divides the matrix multiplication in half until it reaches the base case, which is n=1n = 1. The algorithm is given as follows:

FFT(x):
n = length(x)
if n = 1
return x
x_e = [x_0, x_2, ..., x_n-2]
x_o = [x_1, x_3, ..., x_n-1]
y_e = FFT(x_e)
y_o = FFT(x_o)
ω_n = exp^(2*pi*i*n)
y = initialize an array on n elements
for j = 0:n/2 - 1
y[j] = y_e[j] + ω_n^j * y_o[j]
y[j + n/2] = y_e[j] + ω_n^(j + n/2) * y_e[j]
return y

The algorithm divides the vector into two smaller vectors. One contains all the elements from the even index and the other from the odd. This division is continued until every element reaches the base case.

The real magic happens in the combining of these results. In lines 17 and 18, we are using the periodic property of sinusoids which simplifies our calculations.

Note: Even if nn is not in powers of two, we can pad zeros for the calculations.

Example

Implementing this algorithm from scratch can be tedious and prone to errors. Luckily, SciPy provides a good implementation for its calculation.

import numpy as np
from scipy.fft import fft, ifft
x = np.array([3, -9, 6, -1, -2, 4, 2])
y = fft(x)
x_ = ifft(y)
print("Original values:", x)
print("-"*75)
print("DFT:", y)
print("-"*75)
print("inverse of DFT:", x_)

Explanation

  • Line 5: We create a NumPy array which will be the discrete values of which we would like to calculate FFT.

  • Line 7: Here, we used the fft() function to calculate the FFT. One required parameter is the x which is an array-like object. It can also have complex values.

  • Line 8: The ifft() function implements the inverse of FFT. It has the same required parameter.

Free Resources

Copyright ©2025 Educative, Inc. All rights reserved