NumPy – Fourier Series and Transforms
Fourier analysis is one of the most important concepts in mathematics, engineering, and data science.
It allows us to represent complex signals as combinations of simple sine and cosine waves.
Using NumPy, we can efficiently perform Fourier analysis using built-in FFT (Fast Fourier Transform) functions.
Fourier methods are widely used in:
- Signal processing
- Audio analysis
- Image processing
- Telecommunications
- Machine learning
- Scientific computing
What is a Fourier Series?
A Fourier Series represents a periodic signal as a sum of sine and cosine waves.
For example:
- Square waves
- Triangle waves
- Sawtooth waves
can all be represented using multiple sine waves of different frequencies.
Fourier Series Concept
A periodic signal can be written as:
Where:
- a₀ = constant term
- aₙ = cosine coefficients
- bₙ = sine coefficients
- n = harmonic number
What is a Fourier Transform?
While Fourier Series works on periodic signals, Fourier Transform works on general signals.
It converts:
Time Domain
↓
Fourier Transform
↓
Frequency Domain
This helps identify:
- Dominant frequencies
- Noise components
- Signal patterns
Import NumPy
import numpy as np
1. Creating a Simple Sine Wave
import numpy as np
import matplotlib.pyplot as plt
t = np.linspace(0, 1, 1000)
signal = np.sin(2 * np.pi * 5 * t)
plt.plot(t, signal)
plt.title("5 Hz Sine Wave")
plt.show()
Explanation
This creates a sine wave with:
- Frequency = 5 Hz
- Duration = 1 second
- 1000 samples
2. Applying Fourier Transform
import numpy as np
t = np.linspace(0, 1, 1000)
signal = np.sin(2 * np.pi * 5 * t)
fft_result = np.fft.fft(signal)
print(fft_result[:10])
Explanation
FFT converts the signal into frequency components.
The output contains complex values representing:
- Magnitude
- Phase
of each frequency.
3. Computing Frequency Bins
import numpy as np
N = 1000
freq = np.fft.fftfreq(N)
print(freq[:10])
Explanation
Frequency bins tell us which frequencies correspond to FFT output values.
4. Visualizing the Frequency Spectrum
import numpy as np
import matplotlib.pyplot as plt
t = np.linspace(0, 1, 1000)
signal = np.sin(2 * np.pi * 5 * t)
fft_data = np.fft.fft(signal)
magnitude = np.abs(fft_data)
plt.plot(magnitude)
plt.title("Frequency Spectrum")
plt.show()
Explanation
The frequency spectrum shows peaks at dominant frequencies.
In this example:
Peak Frequency ≈ 5 Hz
5. Multiple Frequencies Example
import numpy as np
import matplotlib.pyplot as plt
t = np.linspace(0, 1, 1000)
signal = (
np.sin(2 * np.pi * 5 * t)
+ 0.5 * np.sin(2 * np.pi * 20 * t)
)
fft_data = np.fft.fft(signal)
plt.plot(np.abs(fft_data))
plt.show()
Explanation
The FFT reveals two major frequencies:
- 5 Hz
- 20 Hz
Even though they are combined into one signal.
6. Fourier Series Approximation of a Square Wave
import numpy as np
import matplotlib.pyplot as plt
x = np.linspace(-np.pi, np.pi, 1000)
square_wave = np.zeros_like(x)
for n in range(1, 20, 2):
square_wave += (4 / (np.pi * n)) * np.sin(n * x)
plt.plot(x, square_wave)
plt.title("Square Wave Approximation")
plt.show()
Explanation
This demonstrates Fourier Series.
A square wave is built using:
- 1st harmonic
- 3rd harmonic
- 5th harmonic
- Higher odd harmonics
FFT vs Fourier Series
| Feature | Fourier Series | FFT |
|---|---|---|
| Works on periodic signals | Yes | Yes |
| Signal decomposition | Yes | Yes |
| Computational speed | Slow | Very Fast |
| Practical applications | Theory | Real-world analysis |
| Frequency analysis | Limited | Excellent |
Common FFT Functions
np.fft.fft()
1D Fast Fourier Transform
np.fft.ifft()
Inverse FFT
np.fft.fftfreq()
Frequency bins
np.fft.fft2()
2D FFT for images
np.fft.ifft2()
Inverse 2D FFT
Real-World Applications
1. Audio Processing
- Noise reduction
- Music analysis
- Speech recognition
2. Image Processing
- JPEG compression
- Edge detection
- Image enhancement
3. Telecommunications
- Wireless communication
- OFDM systems
- Signal modulation
4. Medical Imaging
- MRI reconstruction
- CT scan analysis
- EEG signal processing
5. Machine Learning
- Feature extraction
- Time-series analysis
- Anomaly detection
Why Use NumPy for Fourier Analysis?
Using NumPy provides:
- High-performance FFT algorithms
- Optimized numerical computation
- Fast signal processing
- Easy integration with visualization libraries
Combined with Python, it becomes a powerful toolkit for engineers, researchers, and data scientists.
Summary
Important NumPy Fourier functions:
np.fft.fft()
np.fft.ifft()
np.fft.fftfreq()
np.fft.fft2()
np.fft.ifft2()
These functions allow efficient frequency-domain analysis and signal reconstruction.
Conclusion
Fourier Series and Fourier Transforms are fundamental tools for understanding signals and frequencies. NumPy's FFT module makes these advanced mathematical concepts easy to apply in real-world applications such as audio processing, image analysis, communications, machine learning, and scientific research.


0 Comments