Digital Low Pass Butterworth Filter in Python


The low pass filter is the electronic filter which passes the frequency of signals lesser than the defined cutoff frequency and the frequency of the signals higher than the cutoff will be attenuated. The High pass Butterworth filter has some specialized features defined as follows.

  • The sampling rate of the given input signal is given as 40 kHz

  • The edge frequency of the pass band is 4 kHz

  • The edge frequency of the stop band is 8 kHz

  • The ripple of the pass band is 0.5 dB

  • The minimum attenuation of the stop band is 40 dB and the attenuation of each frequency is based on the filter design.

Implementing a Low band pass Butterworth filter

Numpy library provides a function namely, butter() of the scipy.signal module which is used to create the low pass Butterworth filter by defining the btype parameter as low. Following are the steps to be followed to implement the low pass Butterworth filter using python.

Step 1 − In this step we will define the cutoff frequency to pass the lower frequencies f, sampling frequency fs and order of the filter.

Step 2 − In python we have the library scipy, which have a function namely, scipy.signal.butter() which is used to design the Butterworth filter with the defined order and normalized frequencies, passing the btype parameter as low to implement the low band pass Butterworth filter.

Step 3 − In this step, we will create the filter for the given input signal frequency by using the scipy.signal.filtfilt() function to perform zero phase filtering.

Step 4 − Now we will plot the output frequencies of the Low band pass Butterworth filter and filtered output frequencies.

Example

In the following example, we are implementing a Low band pass Butterworth filter by combining all the mentioned steps.

import numpy as np
import matplotlib.pyplot as plt
from scipy.signal import butter, filtfilt
t = np.linspace(0, 1, 1000, False)  
signal = np.sin(2*np.pi*10*t) + np.sin(2*np.pi*100*t)
cutoff_freq = 50  
nyquist_freq = 0.5 * 1000 
order = 4
b, a = butter(order, cutoff_freq/nyquist_freq, btype='low')
print("The output of the Low band pass Butterworth filter:",b,a)
filtered_signal = filtfilt(b, a, signal)
print("The output of the filtered Low band pass Butterworth filter:",filtered_signal[:60])
fig, (ax1, ax2) = plt.subplots(2, 1, sharex=True)
ax1.plot(t, signal)
ax1.set(title='Original signal')
ax2.plot(t, filtered_signal)
ax2.set(title='Filtered signal')
plt.show()

Output

The output of the Low band pass Butterworth filter: [0.0004166 0.0016664 0.0024996 0.0016664 0.0004166] [ 1.         -3.18063855  3.86119435 -2.11215536  0.43826514]
The output of the filtered Low band pass Butterworth filter: [ 2.57011434e-02  8.61005841e-02  1.44535721e-01  2.01004914e-01
  2.55827118e-01  3.09620894e-01  3.63131838e-01  4.16969733e-01
  4.71355911e-01  5.25981148e-01  5.80034880e-01  6.32402662e-01
  6.81965142e-01  7.27892755e-01  7.69831093e-01  8.07912298e-01
  8.42592726e-01  8.74381837e-01  9.03567319e-01  9.30041579e-01
  9.53294974e-01  9.72576767e-01  9.87160288e-01  9.96608860e-01
  1.00093881e+00  1.00061547e+00  9.96382240e-01  9.88986904e-01
  9.78909138e-01  9.66193004e-01  9.50448481e-01  9.31021733e-01
  9.07269534e-01  8.78833540e-01  8.45810150e-01  8.08751539e-01
  7.68497795e-01  7.25904493e-01  6.81569842e-01  6.35665536e-01
  5.87935734e-01  5.37864244e-01  4.84945669e-01  4.28956498e-01
  3.70122109e-01  3.09115413e-01  2.46887151e-01  1.84392199e-01
  1.22315971e-01  6.09049996e-02 -3.39902671e-05 -6.09665336e-02
 -1.22358749e-01 -1.84405645e-01 -2.46863503e-01 -3.09050695e-01
 -3.70016926e-01 -4.28816577e-01 -4.84782072e-01 -5.37693155e-01]

Updated on: 02-Nov-2023

245 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements