. Advertisement .
. Advertisement .
Creating a low pass filter offers you a filter with lower-frequency signals than the cut-off. The filter also attenuates all the signals with higher frequencies.
The following article will discuss it thoroughly and recommend some methods to implement low pass filter in Python.
What Is A Low Pass Filter?
A low pass filter comes in the form of a circuit offering simple access to lower-frequency signals and complicated access to higher-frequency ones.
In Python, this filter is often useful in removing all high frequencies in a data signal. In a low pass filter, fc cutoff frequency is considered as the sample rate fraction.
On the other hand, b works as a sampling rate’s function and a transition band. N should always be an odd number in your calculation.
Using this type of pass filter allows users to get basic signal processing and filter all disturbing signals to get accurate final results.
A low pass filter works by passing a signal having a lower frequency than the cut-off frequency. This signal often contains a certain value determined by the users.
This way, all signals with higher frequencies than the cut-off will be nervated.
How To Implement Low Pass Filter In Python
Scipy, which is a shorthand of Scientific Python, is one of the most widely used scientific computation libraries. This library offers users various utility functions for stats, signal processing, and optimization.
Like Numpy, you can use Scipy as an open and free source. In Python, Scipy allows users to utilize it to create a low pass filter. You will need to use the Numpy library as well.
Several low pass filters exist in the real world. But in this case with Scipy, let’s create a Butterworth low pass filter. This one has an optimally flat frequency with no ripples in the passband.
To create this type of filter with this method, you need to import Scipy, Numpy, and Matplotlib modules first. Here is the code:
import numpy as np from scipy.signal import butter, lfilter, freqz import matplotlib.pyplot as plt
Let’s execute the following command to create a Butterworth Python filter with the Scipy module.
def butter_lowpass(cutoff, fs, order=5): nyq = 0.5 * fs normal_cutoff = cutoff / nyq b, a = butter(order, normal_cutoff, btype='low', analog=False) return b, a def butter_lowpass_filter(data, cutoff, fs, order=5): b, a = butter_lowpass(cutoff, fs, order=order) y = lfilter(b, a, data) return y # Setting standard filter requirements. order = 6 fs = 30.0 cutoff = 3.667 b, a = butter_lowpass(cutoff, fs, order)
# Plotting the frequency response. w, h = freqz(b, a, worN=8000) plt.subplot(2, 1, 1) plt.plot(0.5*fs*w/np.pi, np.abs(h), 'b') plt.plot(cutoff, 0.5*np.sqrt(2), 'ko') plt.axvline(cutoff, color='k') plt.xlim(0, 0.5*fs) plt.title("Lowpass Filter Frequency Response") plt.xlabel('Frequency [Hz]') plt.grid() # Creating the data for filteration T = 5.0 # value taken in seconds n = int(T * fs) # indicates total samples t = np.linspace(0, T, n, endpoint=False) data = np.sin(1.2*2*np.pi*t) + 1.5*np.cos(9*2*np.pi*t) + 0.5*np.sin(12.0*2*np.pi*t) # Filtering and plotting y = butter_lowpass_filter(data, cutoff, fs, order) plt.subplot(2, 1, 2) plt.plot(t, data, 'b-', label='data') plt.plot(t, y, 'g-', linewidth=2, label='filtered data') plt.xlabel('Time [sec]') plt.grid() plt.legend() plt.subplots_adjust(hspace=0.35) plt.show()
The open-source Scipy library helps users solve all scientific, technical, mathematical, and engineering problems. It also allows you to implement low pass filter in Python.
Check the article above to get the basics of the method and solve your case.