SciPy Fourier Transforms

The scipy.fft module provides functionality for fast Fourier transforms (FFT), inverse FFT, and related transforms like DCT (Discrete Cosine Transform). Fourier analysis is key in signal processing, image analysis, and even computational physics.

Key Topics

FFT and IFFT

scipy.fft.fft computes the discrete Fourier transform of a 1D signal, while ifft returns the inverse transform for reconstructing the original signal in the time domain.

Example: FFT of a Signal

import numpy as np
from scipy.fft import fft, ifft
import matplotlib.pyplot as plt

# Create a signal with two frequency components
fs = 500  # sampling frequency
t = np.linspace(0, 1, fs, endpoint=False)
sig = np.sin(2*np.pi*30*t) + 0.5*np.sin(2*np.pi*80*t)

# Compute FFT
f_transform = fft(sig)

# Get frequencies
freqs = np.fft.fftfreq(len(sig), 1/fs)

# Plot magnitude spectrum
plt.plot(freqs, np.abs(f_transform))
plt.title('FFT Magnitude')
plt.xlabel('Frequency (Hz)')
plt.show()

Output

A plot showing peaks near 30 Hz and 80 Hz.

Explanation: The FFT decomposes the signal into its constituent frequencies. Peaks at 30 Hz and 80 Hz reflect the two sine components used to generate the signal.

Example: IFFT to Reconstruct Signal

import numpy as np
from scipy.fft import fft, ifft
import matplotlib.pyplot as plt

# Create a signal with two frequency components
fs = 500  # sampling frequency
t = np.linspace(0, 1, fs, endpoint=False)
sig = np.sin(2*np.pi*30*t) + 0.5*np.sin(2*np.pi*80*t)

# Compute FFT
f_transform = fft(sig)

# Compute IFFT
reconstructed_sig = ifft(f_transform)

# Plot original and reconstructed signals
plt.plot(t, sig, label='Original Signal')
plt.plot(t, reconstructed_sig.real, '--', label='Reconstructed Signal')
plt.legend()
plt.show()

Output

A plot comparing the original signal and the reconstructed signal.

Explanation: The IFFT reconstructs the original signal from its frequency components. The plot shows that the reconstructed signal closely matches the original signal.

2D Transforms

For 2D data (e.g., images), SciPy supports fft2 and ifft2, enabling frequency analysis of two-dimensional signals.

Example: 2D FFT of an Image

import numpy as np
from scipy.fft import fft2, ifft2
import matplotlib.pyplot as plt
from scipy import misc

# Load an example image
image = misc.face(gray=True)

# Compute 2D FFT
f_transform = fft2(image)

# Compute magnitude spectrum
magnitude_spectrum = np.abs(f_transform)

# Plot original image and magnitude spectrum
plt.subplot(121)
plt.imshow(image, cmap='gray')
plt.title('Original Image')
plt.subplot(122)
plt.imshow(np.log(magnitude_spectrum), cmap='gray')
plt.title('Magnitude Spectrum')
plt.show()

Output

A plot showing the original image and its magnitude spectrum.

Explanation: The 2D FFT decomposes the image into its frequency components. The magnitude spectrum shows the intensity of different frequency components in the image.

Discrete Cosine Transforms

dct and idct are also available for tasks like image compression, where the DCT is used in JPEG compression.

Example: DCT of a Signal

import numpy as np
from scipy.fft import dct, idct
import matplotlib.pyplot as plt

# Create a signal
x = np.linspace(0, 2*np.pi, 100)
y = np.cos(x) + 0.5*np.cos(2*x)

# Compute DCT
dct_transform = dct(y)

# Compute IDCT
reconstructed_y = idct(dct_transform, norm='ortho')

# Plot original and reconstructed signals
plt.plot(x, y, label='Original Signal')
plt.plot(x, reconstructed_y, '--', label='Reconstructed Signal')
plt.legend()
plt.show()

Output

A plot comparing the original signal and the reconstructed signal using DCT and IDCT.

Explanation: The DCT decomposes the signal into cosine components, and the IDCT reconstructs the original signal. The plot shows that the reconstructed signal closely matches the original signal.

Key Takeaways

  • Frequency Analysis: Easily decompose signals into frequency components.
  • Various Transforms: FFT, IFFT, 2D FFT, DCT, etc.
  • Applications: Signal processing, image processing, data compression.
  • Performance: Highly optimized routines for large arrays.