Spectral leakage happens when we try to find frequencies in a signal but get extra unwanted parts mixed in. It makes it hard to see the true frequencies clearly.
Spectral leakage concept in Signal Processing
No specific code syntax because spectral leakage is a concept seen in frequency analysis results, especially using the Fourier Transform.
Spectral leakage appears when the signal is not perfectly periodic in the sample window.
It causes energy from one frequency to spread into others, making peaks wider or less clear.
import numpy as np import matplotlib.pyplot as plt fs = 1000 # Sampling frequency T = 1 # Duration in seconds N = fs * T # Number of samples t = np.linspace(0, T, N, endpoint=False) # Signal with frequency 50.5 Hz (not an integer multiple of frequency bins) signal = np.sin(2 * np.pi * 50.5 * t) # Compute FFT fft_vals = np.fft.fft(signal) fft_freq = np.fft.fftfreq(N, 1/fs) # Plot magnitude spectrum plt.stem(fft_freq[:N//2], np.abs(fft_vals)[:N//2], use_line_collection=True) plt.title('Spectral Leakage Example') plt.xlabel('Frequency (Hz)') plt.ylabel('Magnitude') plt.show()
import numpy as np import matplotlib.pyplot as plt fs = 1000 T = 1 N = fs * T t = np.linspace(0, T, N, endpoint=False) # Signal with frequency 50 Hz (integer multiple of frequency bins) signal = np.sin(2 * np.pi * 50 * t) fft_vals = np.fft.fft(signal) fft_freq = np.fft.fftfreq(N, 1/fs) plt.stem(fft_freq[:N//2], np.abs(fft_vals)[:N//2], use_line_collection=True) plt.title('No Spectral Leakage Example') plt.xlabel('Frequency (Hz)') plt.ylabel('Magnitude') plt.show()
This program creates a sine wave at 50.5 Hz, which does not match the FFT frequency bins exactly. When we look at the FFT magnitude, the peak spreads to nearby frequencies, showing spectral leakage.
import numpy as np import matplotlib.pyplot as plt # Sampling parameters fs = 1000 # samples per second T = 1 # seconds N = fs * T # total samples t = np.linspace(0, T, N, endpoint=False) # Signal with frequency not matching FFT bins freq = 50.5 signal = np.sin(2 * np.pi * freq * t) # Compute FFT fft_vals = np.fft.fft(signal) fft_freq = np.fft.fftfreq(N, 1/fs) # Take positive frequencies pos_mask = fft_freq >= 0 freqs = fft_freq[pos_mask] magnitude = np.abs(fft_vals[pos_mask]) # Print main peak frequency and magnitude peak_index = np.argmax(magnitude) peak_freq = freqs[peak_index] peak_mag = magnitude[peak_index] print(f"Peak frequency: {peak_freq:.2f} Hz") print(f"Peak magnitude: {peak_mag:.2f}") # Plot spectrum plt.stem(freqs, magnitude, use_line_collection=True) plt.title('Spectral Leakage Demonstration') plt.xlabel('Frequency (Hz)') plt.ylabel('Magnitude') plt.show()
Spectral leakage can be reduced by using window functions like Hamming or Hann before FFT.
Longer signal duration (more samples) reduces leakage by making frequency bins closer.
Understanding spectral leakage helps in correctly interpreting frequency analysis results.
Spectral leakage happens when signal frequencies don't match FFT bins exactly.
It causes energy to spread to nearby frequencies, making peaks less sharp.
Using windows and longer signals helps reduce spectral leakage.