Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

NumPy - Fourier Series and Transforms



NumPy Fourier Series and Transforms

In mathematics, a Fourier series breaks down periodic functions into sums of simpler sine and cosine waves. It is used to analyze functions or signals that repeat over time, such as sound waves or electrical signals.

Fourier transforms, on the other hand, extend this concept to non-periodic functions, converting them from the time domain to the frequency domain. This helps in understanding the frequency components of a signal, which is useful in signal processing and data analysis.

NumPy provides a range of functions to handle different types of Fourier transforms, both for real and complex data.

Fourier Series

The Fourier series represents a periodic function as a sum of sines and cosines. It is useful for analyzing periodic signals, where the function can be broken down into a series of sinusoidal components with different frequencies.

f(t) = a_0 + Σ (a_n cos(nωt) + b_n sin(nωt))

where ω is the fundamental angular frequency, and a_n and b_n are the Fourier coefficients.

Example: Computing Fourier Coefficients

Although NumPy does not provide a direct function to compute Fourier series coefficients, we can use the fft() function to approximate them. In the following example, we compute the Fourier coefficients for a simple periodic signal −

import numpy as np

# Define a periodic signal
t = np.linspace(0, 1, 500, endpoint=False)
signal = np.sin(2 * np.pi * 5 * t) + 0.5 * np.sin(2 * np.pi * 10 * t)

# Compute the Fourier coefficients using FFT
fourier_coeffs = np.fft.fft(signal)

print("Fourier coefficients:", fourier_coeffs)

The result shows the Fourier coefficients of the signal −

Fourier coefficients: [-3.87326339e-14+0.00000000e+00j  4.12725409e-14+2.68673972e-14j...]

Fast Fourier Transform (FFT)

The Fast Fourier Transform (FFT) is an algorithm to compute the Discrete Fourier Transform (DFT) and its inverse. The FFT decomposes a signal into its frequency components, providing a frequency-domain representation.

NumPy provides several functions for computing the FFT, including numpy.fft.fft() for one-dimensional transforms and numpy.fft.fftn() for multi-dimensional transforms.

Example: Computing the FFT

In the following example, we compute the FFT of a one-dimensional signal −

import numpy as np

# Define a signal
t = np.linspace(0, 1, 500, endpoint=False)
signal = np.sin(2 * np.pi * 5 * t)

# Compute the FFT
fft_values = np.fft.fft(signal)

print("FFT values:", fft_values)

The result shows the FFT values of the signal −

FFT values: [-2.37154386e-14+0.00000000e+00j  6.32657050e-15+6.43929354e-15j-4.51716973e-14+3.82357443e-14j ...]

Inverse Fast Fourier Transform (IFFT)

The Inverse Fast Fourier Transform (IFFT) converts a frequency-domain representation of a signal back to the time domain. NumPy provides the numpy.fft.ifft() function for one-dimensional IFFT and numpy.fft.ifftn() for multi-dimensional IFFT.

Example: Computing the IFFT

In the following example, we compute the IFFT of a frequency-domain signal to reconstruct the original time-domain signal −

import numpy as np

# Define a frequency-domain signal (computed via FFT previously)
fft_values = np.array([10+0j, -2+2j, -2+0j, -2-2j])

# Compute the IFFT
time_signal = np.fft.ifft(fft_values)

print("Reconstructed time-domain signal:", time_signal)

The result shows the reconstructed time-domain signal −

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

Real-valued FFT (rFFT)

The Real-valued FFT (rFFT) is optimized for real-valued input signals. NumPy provides the numpy.fft.rfft() function for computing the FFT of real-valued signals and numpy.fft.irfft() function for computing the IFFT of real-valued signals.

Example: Computing the rFFT and irFFT

In the following example, we compute the rFFT of a real-valued signal and then reconstruct the signal using irFFT −

import numpy as np

# Define a real-valued signal
t = np.linspace(0, 1, 500, endpoint=False)
real_signal = np.sin(2 * np.pi * 5 * t)

# Compute the rFFT
rfft_values = np.fft.rfft(real_signal)

# Compute the inverse rFFT
reconstructed_signal = np.fft.irfft(rfft_values)

print("Reconstructed real-valued time-domain signal:", reconstructed_signal)

The result shows the reconstructed real-valued time-domain signal −

Reconstructed real-valued time-domain signal: [ 5.04870979e-32  6.27905195e-02  1.25333234e-01  1.87381315e-01 ...]

Applications of Fourier Transforms

Fourier transforms are widely used in various fields, such as −

  • Signal processing: Analyzing and filtering signals in communications, audio processing, and image processing.
  • Physics and engineering: Studying wave-forms, vibrations, and oscillations.
  • Data compression: Reducing the size of data by transforming and truncating frequency components.
  • Financial analysis: Analyzing time-series data in stock market and economic forecasting.
Advertisements