NumPy - Discrete Fourier Transform



NumPy Discrete Fourier Transform

The Discrete Fourier Transform (DFT) is a mathematical technique used to convert a sequence of values into components of different frequencies. It is widely used in signal processing, image analysis, and audio processing.

In NumPy, the DFT can be computed using the fft (Fast Fourier Transform) module, which provides implementations for computing the DFT and its inverse.

The DFT helps analyze the frequency content of a signal, making it useful in many applications, including filtering, signal compression, and spectral analysis.

Computing DFT Using fft() Function

The numpy.fft.fft() function computes the one-dimensional n-point Discrete Fourier Transform (DFT) of an array. The function returns the frequency components of the input signal, with the zero frequency component at the beginning of the output array.

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

Example: Computing the DFT

In the following example, we compute the Discrete Fourier Transform of a one-dimensional array using NumPy's fft() function −

import numpy as np

# Define an array of sample data (signal)
signal = np.array([1, 2, 3, 4])

# Compute the Discrete Fourier Transform of the signal
dft_signal = np.fft.fft(signal)

print("DFT of the signal:", dft_signal)

The result of the above code is −

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

Computing the Inverse DFt

The numpy.fft.ifft() function computes the inverse of the Discrete 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 Discrete Fourier Transform of the input array x

Example: Inverse DFT

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

import numpy as np

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

# Compute the Inverse Discrete Fourier Transform of the DFT
reconstructed_signal = np.fft.ifft(dft_signal)

print("Reconstructed signal:", reconstructed_signal)

Following is the output obtained −

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

Computing the DFT for a Real Signal

The numpy.fft.rfft() function is optimized for computing the DFT 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) = DFT of a real-valued input array x

Example: DFT of a Real Signal

In the following example, we compute the Discrete 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 Discrete Fourier Transform of the real-valued signal
dft_real_signal = np.fft.rfft(signal)

print("DFT of the real-valued signal:", dft_real_signal)

This will produce the following result −

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

Computing the Inverse DFT for Real Signal

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

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

Example: Inverse DFT of a Real Signal

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

import numpy as np

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

# Compute the Inverse Discrete Fourier Transform of the real-valued DFT
reconstructed_real_signal = np.fft.irfft(dft_real_signal)

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

Following is the output of the above code −

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 DFT. This is useful for plotting the frequency components of a signal or analyzing its spectral content.

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

Example: Frequency Binning

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

import numpy as np

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

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

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

The output obtained is as shown below −

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