How FFT Works: Fast Fourier Transform Explained Simply
The
FFT (Fast Fourier Transform) is an algorithm that quickly computes the Discrete Fourier Transform (DFT) of a signal, converting it from time domain to frequency domain. It breaks down a signal into its sine and cosine components, revealing the frequencies present and their strengths.Syntax
The basic syntax of FFT depends on the programming language or library used. Generally, it takes a sequence of numbers (signal samples) and returns complex numbers representing frequency components.
For example, in Python's NumPy library:
numpy.fft.fft(x): Computes the FFT of input arrayx.x: Input signal array (time domain samples).- Returns an array of complex numbers representing amplitude and phase of frequencies.
python
import numpy as np # x is the input signal array X = np.fft.fft(x)
Example
This example shows how FFT converts a simple signal made of two sine waves into frequency components.
python
import numpy as np import matplotlib.pyplot as plt # Sampling parameters fs = 1000 # samples per second T = 1 # seconds N = fs * T # total samples # Time array t = np.linspace(0, T, N, endpoint=False) # Create a signal with two frequencies: 50Hz and 120Hz signal = 0.7 * np.sin(2 * np.pi * 50 * t) + 0.3 * np.sin(2 * np.pi * 120 * t) # Compute FFT fft_result = np.fft.fft(signal) # Frequency array freq = np.fft.fftfreq(N, 1/fs) # Take only positive frequencies pos_mask = freq >= 0 freq = freq[pos_mask] magnitude = np.abs(fft_result[pos_mask]) # Plot frequency spectrum plt.plot(freq, magnitude) plt.title('Frequency Spectrum') plt.xlabel('Frequency (Hz)') plt.ylabel('Magnitude') plt.grid(True) plt.show()
Output
A plot showing two peaks at 50 Hz and 120 Hz frequencies representing the signal's frequency components.
Common Pitfalls
- Not using power of two length: FFT runs fastest when input length is a power of two. Padding with zeros can help.
- Ignoring complex output: FFT returns complex numbers; magnitude and phase need to be extracted properly.
- Misinterpreting frequency bins: Frequency values depend on sampling rate and number of samples.
- Not windowing the signal: Without windowing, FFT can show spectral leakage causing blurred frequency peaks.
python
import numpy as np # Wrong: FFT on non-power-of-two length without padding x = np.random.rand(1500) # length 1500 is not power of two fft_wrong = np.fft.fft(x) # Right: Pad signal to next power of two n = len(x) n_padded = 2**int(np.ceil(np.log2(n))) x_padded = np.pad(x, (0, n_padded - n)) fft_right = np.fft.fft(x_padded)
Key Takeaways
FFT quickly converts time signals into frequency components using an efficient algorithm.
Input length as a power of two improves FFT speed and accuracy.
FFT output is complex; use magnitude to find strength of frequencies.
Sampling rate and signal length determine frequency resolution.
Windowing signals before FFT reduces spectral leakage and improves results.