Digital Band Reject Butterworth Filter in Python


A Band Reject filter is the filter which rejects or blocks all the frequencies within the range and passes the frequencies outside the range. The Butterworth is the type of a filter designed to filter the frequencies as flat as possible in the pass band. The following are the main features of the digital band reject butter worth filter.

  • The sampling rate of the filter is about 12 kHz.

  • The pass band edge frequencies are in the range of 2100 Hz to 4500 Hz.

  • The stop band edge frequencies are within the range of 2700 Hz to 3900 Hz.

  • The ripple point of the pass band is 0.6 decibels.

  • The minimum attenuation of the stop band is 45 decibels.

Implementing the band reject Butterworth filter

The Band reject Butterworth filter is opposite to the functionality of the band pass Butterworth filter. There few steps to be followed to implement the digital band reject Butterworth filter. Let’s see them one by one.

Step 1 − At first we have to define the sampling frequency fs of the given input signal along with the lower cutoff frequency f1, upper cutoff frequency f2 and the order of the filter. To avoid the steeper roll-off, leads to the phase distortion we have to determine the order of the filter as low as possible.

Step 2 − In this step we will calculate the normalized frequencies of the defined lower and higher cutoff frequencies f1 and f2 as wn1 and wn2 respectively using the below formula.

wn = 2 ∗ fn / fs

Step 3 − 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 bandstop.

Step 4 − Now we will apply the filter for the given input signal by using the scipy.signal.filtfilt() function to perform zero phase filtering.

Example

In the following example, we are implementing the band reject Butterworth filter using the functions available in python and the above mentioned steps.

import numpy as np
import matplotlib.pyplot as plt
from scipy.signal import butter, filtfilt
def butter_bandstop(lowcut, highcut, fs, order=5):
   nyq = 0.5 * fs
   low = lowcut / nyq
   high = highcut / nyq
   b, a = butter(order, [low, high], btype='bandstop')
   return b, a
def butter_bandstop_filter(data, lowcut, highcut, fs, order=5):
   b, a = butter_bandstop(lowcut, highcut, fs, order=order)
   y = filtfilt(b, a, data)
   return y
t = np.linspace(0, 1, 1000, endpoint=False)
data = np.sin(2*np.pi*10*t) + np.sin(2*np.pi*100*t)
fs = 1000 
lowcut = 45  
highcut = 55 
order = 4
filtered_data = butter_bandstop_filter(data, lowcut, highcut, fs, order)
print("The length of the filtered_data:",len(filtered_data))
print("The first 60 frequencies output of the bandstop filter:",filtered_data[:60])
plt.plot(t, data, 'b-', label='data')
plt.plot(t, filtered_data, '.', linewidth=0.75, label='filtered data', c = ("orange"))
plt.xlabel('Time [sec]')
plt.grid()
plt.legend()
plt.show()

Output

The length of the filtered_data: 1000
The first 60 frequencies output of the bandstop filter: [ 0.0538999   0.68581044  1.08999445  1.12963747  0.80670599  0.26172752
 -0.27940415 -0.59133519 -0.53509911 -0.11114146  0.54072315  1.19441551
  1.62350117  1.68713138  1.38324023  0.8487329   0.30664654 -0.01948728
  0.00866697  0.3912569   0.99019459  1.58206033  1.94372501  1.9379504
  1.56622785  0.96862788  0.37067549 -0.00249295 -0.01192794  0.34213815
  0.9203407   1.49727013  1.8473413   1.83067626  1.44623031  0.83190402
  0.21163048 -0.19032582 -0.23510605  0.07772592  0.61013477  1.13854044
  1.43948479  1.37529278  0.94698224  0.29418691 -0.35789988 -0.78431234
 -0.84607258 -0.54316965 -0.01451725  0.51510369  0.82086563  0.76371439
  0.34342535 -0.30135441 -0.94608162 -1.36606715 -1.42224433 -1.11419611]

Updated on: 02-Nov-2023

79 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements