scipy.signal.butter and its usage in Python

This method of Python programming is based on scipy.signal.butter(). Here we will focus on filtering the signals with a significant amount of noise using the bandpass Butterworth filter. This method of scipy.signal.butter() is very useful in rectifying the noise in any signal.

SciPy And Its Usage:

SciPy is an abbreviation for scientific Python. The reason for using SciPy is that it provides stats and signal processing and solves various scientific problems related to all types of signals with the help of Python programming. SciPy is an organized form of the computational library which is considered open source like the NumPy library in Python.

Requirements:

Other than SciPy we require other libraries like Pandas, NumPy, and Matplotlib. In case your system does not have these libraries you may install these libraries using the following syntax.

# For SciPy
pip install scipy
# For Pandas:
pip install pandas
# For NumPy:
pip install numpy
# For Matplotlib:
pip install matplotlib

Algorithm:

We have explained each step with proper code snippets.

Step 1:

Step one is to import the required libraries to the coding terminal as follows:

from scipy.signal import filtfilt
from scipy import stats
import CSV
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import scipy

Step 2:

In this step, we have to introduce the data of the signal, we want to filter using the following code of function. In the case below we have used the data stored in a CSV file.

SciPy And Its Usage

# Code To Insert The Raw Data
def plot():
    data=pd.read_csv('path of the file') 
    signal_data=data[['data']]
    signal_data=np.array(signal_data)
    time=np.linespace(0, 0.0002, 4000)
    plt.plot(time, signal_data)
    plt.show()

plt()

plt.plot(time, signal_data)

This is the signal with a sufficient amount of noise.

 

Step 3:

In step 3 we will write the code to remove the noise in the signal as follows, along with the specifications we want in our Butterworth bandpass filter.

def bandpass_filter(signal):
    fs = 4000.0
    lowcut= 20.0
    highcut=50.0

    nyqs=0.5*fs
    low= lowcut/nyqs
    high=highcut/nyqs
    
    order=2

    b,a =scipy.signal.butter(order,[low,high], 'bandpass', analog=False)
    y=scipy.signal.filtfilt(b,a,signal,axis=0)
    return(y)

Step 4:

The fourth and final step is to merge the syntax in steps two and three to create one single program to filter the signal. The entire program for which is given below:

#import required libraries
from scipy.signal import filtfilt
from scipy import stats
import CSV
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import scipy

def plot():
    data = pd.read_csv('path of the file')
    signal_data = data[['data']]
    signal_data = np.array(signal_data)


    time = np.linespace(0, 0.0002, 4000)
    plt.plot(time, signal_data)
    plt.show()

    filtered_signal= bandPassFilter(signal_data)

    plt.plot(time, filtered_signal)
    plt.show()

#syntax for bandpass filter
def bandPass_filter(signal):

    fs = 4000.0
    lowcut = 20.0
    highcut = 50.0

    nyqs = 0.5* fs
    low = lowcut / nyqs
    high = highcut/ nyqs
    
    order=2
    
    b, a = scipy.signal.butter(order, [low, high], 'bandpass', analog = False)
    y = scipy.signal.filtfilt(b, a, signal, axis=0)
    return(y)

plot()


Output:

scipy.signal.butter

This is a filtered signal using scipy.signal.butter().

Leave a Reply

Your email address will not be published. Required fields are marked *