Table of Contents
ToggleIn 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.
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.
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 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.
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 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.
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 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.
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)
