Sunday, March 23, 2025

Signal Processing with Butterworth(Low Pass) Filter

 Example Signal:

       



Figure 1: Signal of 10Hz and 20Hz

    The frequency formula in terms of time is given as: f = 1/T where, f is the frequency in hertz, and T is the time to complete one cycle in seconds. Can see that a higher frequency signal complete a cycle faster in time than a lower frequency signal.

A low-pass filter (LPF) below allows signals with frequencies below 50Hz cutoff frequency to pass through while attenuating (reducing) signals with frequencies above that cutoff.


Coding 

from scipy.signal import butter, lfilter
    import numpy as np
    import matplotlib.pyplot as plt
    
    # Generate a sample signal
    fs = 200  # Sampling frequency in Hz
    t = np.linspace(0, 1, fs, endpoint=False)  # 1 second of data
    signal1 = np.sin(2 * np.pi * 80 * t) 
    
    # Design a low-pass filter (cutoff at 50 Hz)
    cutoff = 50 # Cutoff frequency in Hz
    nyquist = fs / 2  # Nyquist frequency
    Wn = cutoff / nyquist  # Normalized cutoff frequency
    b, a = butter(5, Wn, btype='low')  # 5th-order low-pass filter
    
    # Apply the filter
    filtered_signal = lfilter(b, a, signal1)
    
    # Plot the original and filtered signal
    plt.figure(figsize=(10, 6))
    plt.plot(t, signal1, label='Original Signal')
    plt.plot(t, filtered_signal, label='Filtered Signal')
    plt.title('Low-Pass Filter (50 Hz Cutoff)')
    plt.xlabel('Time [s]')
    plt.legend()
    plt.grid()
    plt.show()

As shown in graph, the original signal of 80 Hz is filter out, as the allowed signal is just below 50Hz.


References

1. https://www.linkedin.com/pulse/signal-processing-python-part-1-generate-signals-basic-hampiholi/