NumPy - Fast Fourier Transform



NumPy Fast Fourier Transform

The Fast Fourier Transform (FFT) is a quick way to compute the Discrete Fourier Transform (DFT) and its inverse. It speeds up the process by reducing the time it takes from O(n2) to O(nlogn), making it much faster, especially when working with large datasets.

In NumPy, you can calculate the FFT using the fft module, which has functions for both one-dimensional and multi-dimensional FFT calculations.

Computing the FFT Using fft() Function

The numpy.fft.fft() function computes the one-dimensional n-point FFT of an array. It transforms a signal from the time domain into the frequency domain.

fft(x) = Fast Fourier Transform of the input array x

Example: Computing the FFT

In the following example, we compute the Fast Fourier Transform of a one-dimensional array (signal) using the fft() function −

import numpy as np

# Define a simple signal
signal = np.array([1, 2, 3, 4])

# Compute the Fast Fourier Transform of the signal
fft_signal = np.fft.fft(signal)

print("FFT of the signal:", fft_signal)

We get the output as shown below −

FFT of the signal: [10.+0.j -2.+2.j -2.+0.j -2.-2.j]

Computing the Inverse FFT

The numpy.fft.ifft() function computes the inverse of the Fast Fourier Transform, converting frequency components back into the original time-domain signal. This is useful when you want to reconstruct the signal from its frequency representation.

ifft(x) = Inverse Fast Fourier Transform of the input array x

Example: Inverse FFT

In the following example, we compute the inverse FFT of the FFT computed earlier using NumPy's ifft() function −

import numpy as np

# Define the FFT of a signal (computed previously)
fft_signal = np.array([10+0j, -2+2j, -2+0j, -2-2j])

# Compute the Inverse Fast Fourier Transform
reconstructed_signal = np.fft.ifft(fft_signal)

print("Reconstructed signal:", reconstructed_signal)

The result produced is as follows −

Reconstructed signal: [1.+0.j 2.+0.j 3.+0.j 4.+0.j]

Computing the FFT for a Real Signal

The numpy.fft.rfft() function is optimized for computing the FFT of real input signals. This function returns only the non-negative frequency terms, as the negative frequency terms are redundant for real-valued signals.

rfft(x) = FFT of a real-valued input array x

Example: FFT of a Real Signal

In the following example, we compute the Fast Fourier Transform of a real-valued signal using NumPy's rfft() function −

import numpy as np

# Define a real-valued signal
signal = np.array([1, 2, 3, 4])

# Compute the Fast Fourier Transform of the real-valued signal
fft_real_signal = np.fft.rfft(signal)

print("FFT of the real-valued signal:", fft_real_signal)

After executing the above code, we get the following output −

FFT of the real-valued signal: [10.+0.j -2.+2.j -2.+0.j]

Computing Inverse FFT for a Real Signal

The numpy.fft.irfft() function computes the inverse of the FFT for real-valued signals, reconstructing the original time-domain signal from its non-negative frequency components.

irfft(x) = Inverse FFT of a real-valued input array x

Example: Inverse FFT of a Real Signal

In the following example, we compute the inverse FFT of a real-valued signal −

import numpy as np

# Define the FFT of a real-valued signal (computed previously)
fft_real_signal = np.array([10+0j, -2+2j])

# Compute the Inverse FFT of the real-valued FFT
reconstructed_real_signal = np.fft.irfft(fft_real_signal)

print("Reconstructed real-valued signal:", reconstructed_real_signal)

The output obtained is as shown below −

Reconstructed real-valued signal: [4. 6.]

Frequency Binning Using fftfreq() Function

The numpy.fft.fftfreq() function is used to generate an array of sample frequencies corresponding to the components of the FFT. This is useful for plotting the frequency components of a signal or analyzing its spectral content.

fftfreq(n, d=1) = frequencies corresponding to the FFT of an array with n points, spaced by d

Example: Frequency Binning

In the following example, we generate the frequencies corresponding to the FFT of a signal −

import numpy as np

# Define the number of points in the FFT and the sample spacing
n_points = 4
spacing = 1

# Generate the frequencies corresponding to the FFT
frequencies = np.fft.fftfreq(n_points, spacing)

print("Frequencies corresponding to the FFT:", frequencies)

Following is the output of the above code −

Frequencies corresponding to the FFT: [ 0.    0.25 -0.5  -0.25]

Using FFT in Multi-dimensional Arrays

NumPy's fft module also supports multi-dimensional arrays, allowing you to compute the FFT along specific axes of the array. The numpy.fft.fftn() function computes the n-dimensional FFT, while numpy.fft.ifftn() function computes the inverse of the n-dimensional FFT.

fftn(x) = N-dimensional Fast Fourier Transform of an array x

Example: 2D FFT

In the following example, we compute the 2-dimensional FFT of a 2D array −

import numpy as np

# Define a 2D signal
signal_2d = np.array([[1, 2], [3, 4]])

# Compute the 2D Fast Fourier Transform
fft_2d_signal = np.fft.fftn(signal_2d)

print("2D FFT of the signal:", fft_2d_signal)

This will produce the following result −

2D FFT of the signal: 
[[10.+0.j -2.+0.j]
 [-4.+0.j  0.+0.j]]
Advertisements