0
0
Signal Processingdata~5 mins

Spectral leakage concept in Signal Processing

Choose your learning style9 modes available
Introduction

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.

When analyzing sound recordings to find different tones or notes.
When checking vibrations in machines to detect problems.
When studying brain waves to understand brain activity.
When measuring radio signals to find different channels.
When working with any signal that is cut into short pieces for analysis.
Syntax
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.

Examples
This example shows spectral leakage because 50.5 Hz is not an exact frequency bin, so the energy spreads to nearby frequencies.
Signal Processing
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()
This example shows no spectral leakage because 50 Hz fits exactly into the frequency bins, so the energy is concentrated at one frequency.
Signal Processing
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()
Sample Program

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.

Signal Processing
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()
OutputSuccess
Important Notes

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.

Summary

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.