0
0
RosDebug / FixBeginner · 4 min read

How to Prevent Aliasing in Signal Processing: Simple Fixes

To prevent aliasing in signal processing, always apply a low-pass filter before sampling to remove frequencies above half the sampling rate (the Nyquist frequency). Then, sample the signal at a rate at least twice the highest frequency component to avoid overlapping frequency information.
🔍

Why This Happens

Aliasing happens when a signal is sampled too slowly, causing high-frequency parts to appear as lower frequencies. This confuses the signal's true shape and creates errors in analysis or playback.

Imagine taking photos of a spinning wheel with a slow shutter speed; the wheel may look like it's spinning backward. This is aliasing in action.

python
import numpy as np
import matplotlib.pyplot as plt

# Create a high-frequency signal
fs = 50  # Sampling rate (Hz)
t = np.arange(0, 1, 1/fs)
f_signal = 30  # Signal frequency (Hz)
signal = np.sin(2 * np.pi * f_signal * t)

# Sample the signal at 50 Hz (below Nyquist rate for 30 Hz signal)
plt.plot(t, signal, 'o-')
plt.title('Aliased Signal Sampling')
plt.xlabel('Time (seconds)')
plt.ylabel('Amplitude')
plt.show()
Output
A plot showing a distorted sine wave that appears to have a lower frequency than 30 Hz, demonstrating aliasing.
🔧

The Fix

To fix aliasing, first use a low-pass filter to remove frequencies above half the sampling rate (Nyquist frequency). Then, sample the filtered signal at a rate at least twice the highest frequency present.

This ensures the signal's frequency content is preserved without overlap.

python
import numpy as np
import matplotlib.pyplot as plt
from scipy.signal import butter, filtfilt

# Original signal parameters
fs = 100  # New sampling rate (Hz)
t = np.arange(0, 1, 1/fs)
f_signal = 30  # Signal frequency (Hz)
signal = np.sin(2 * np.pi * f_signal * t) + 0.5 * np.sin(2 * np.pi * 60 * t)  # Add high freq noise

# Design low-pass filter to remove frequencies above 40 Hz
nyq = 0.5 * fs
cutoff = 40
b, a = butter(4, cutoff / nyq, btype='low')
filtered_signal = filtfilt(b, a, signal)

# Plot filtered signal
plt.plot(t, filtered_signal, label='Filtered Signal')
plt.plot(t, signal, alpha=0.3, label='Original Signal')
plt.title('Signal After Low-Pass Filtering')
plt.xlabel('Time (seconds)')
plt.ylabel('Amplitude')
plt.legend()
plt.show()
Output
A plot showing the original noisy signal and the filtered signal with high frequencies removed, preventing aliasing.
🛡️

Prevention

To avoid aliasing in future signal processing tasks:

  • Always apply a low-pass filter before sampling to remove frequencies above the Nyquist frequency.
  • Choose a sampling rate at least twice the highest frequency component in your signal.
  • Use anti-aliasing hardware filters when working with real-world signals.
  • Validate your sampling setup by checking the frequency content before and after sampling.
⚠️

Related Errors

Other common signal processing errors related to aliasing include:

  • Under-sampling: Sampling below the Nyquist rate causing loss of information.
  • Quantization noise: Errors from digitizing signals with limited resolution.
  • Clipping: Signal amplitude exceeding system limits causing distortion.

Fixes usually involve increasing sampling rate, improving filter design, or adjusting signal amplitude.

Key Takeaways

Always filter out frequencies above half the sampling rate before sampling to prevent aliasing.
Sample signals at least twice the highest frequency component (Nyquist rate) to preserve signal integrity.
Use low-pass filters (anti-aliasing filters) to clean signals before digitizing.
Check your signal's frequency content to choose the right sampling rate and filter cutoff.
Aliasing causes misleading frequency information and can be avoided with proper filtering and sampling.