Hướng dẫn low pass filter python
Recently while I was working on processing a very high frequency signal of 12.5 Khz , i.e. 12500 samples per second or a sample every 80 microsecond. What was more interesting is that I had to derive various data points into this data set. This is how my data in a single cycle looked like You can see the noise when I zoom in the data So now consider, if had to determine the point where the curve starts it rise. With so much of noise there is a very high probability of getting false positive data point. Also imagine the performance of the algorithm with so much fluctuation in the data. I would start with some signal processing basics , which are essential to understand before we jump into code. Basics : Band Pass FiltersThe four common filters.
Now lets see a sample data ,which would be ideal to work with As you can see the distortion caused by a lot of noise has deformed actual data which is a sin wave data.
This means we need a filter that would pass the signal with at most frequency of 1.2 Hz , However in real life the signal frequency may fluctuate , hence it would be good if we choose a slightly higher number than the ideally calculated frequency. You can also try using FFT (Fast Fourier Transform) to find investigate the frequencies and amplitudes of the Signal vs the noise components, more details along with code can be found here Butterworth FilterThe frequency response of the Butterworth filter is maximally flat (i.e. has no ripples) in the passband and rolls off towards zero in the stopband, hence its one of the most popular low pass filter. Nyquist FrequencyThe term Nyquist is often used to describe the Nyquist sampling rate or the Nyquist frequency. The Nyquist rate or frequency is the minimum rate at which a finite bandwidth signal needs to be sampled to retain all of the information. If a time series is sampled at regular time intervals dt, then the Nyquist rate is just 1/(2 dt ). Lets jump into CodeStep 1 : Define the filter requirements
import numpy as np Step 2 : Create some sample data with noise # sin wave Step 3 : Filter implementation using scipy def butter_lowpass_filter(data, cutoff, fs, order): Step 4 : Filter and plot the data # Filter the data, and plot both the original and filtered signals. It’s surprising how smoothly the filtered signal aligns to the data, feels like ‘butter’. And that’s it for today ! Happy Filtering Check out my other articles |