Personal website –> www.yansongsong.cn
GitHub homepage –> github.com/xiaosongshi…
1. Background
In deep learning, Matlab is sometimes used for filtering, and then the processed data is fed into the neural network. This is the general processing method, but the processing is a little cumbersome, and sometimes the system is difficult to run Matlab. Python, as a very powerful language, supports signal filtering.
This paper will use Python to realize simple filtering based on SCIPY module in the form of actual combat, including 1. Low pass filter, 2. High pass filter, 3. Band pass filter, 4. You can refer to college courses, signals and systems. A simple understanding is that low-pass filtering refers to the removal of signals higher than a certain threshold frequency; High-pass filtering removes signals below a certain frequency; Band-pass filtering is similar to the combination of low-pass and high-pass to retain intermediate frequency signals; Band stop filtering is also a combination of low pass and high pass but it filters out the middle part. The above mentioned content will be introduced in the actual combat part, can be compared to understand.
How does ** work? ** My understanding is to convert the time domain into the frequency domain, remove the corresponding frequency domain signal in the frequency domain signal, and finally revert to the time domain model in the reverse transformation. For details, see university courses, Signals and systems. They learn very generally do not teach fish to suck eggs.
What does ** do? **My Opinions can eliminate some interfering signals. Take low-pass filtering as an example. For example, if we only count the pulse signal waveform, it should be about 1Hz, but we find that there are many noises on the waveform signal, which are hundreds or thousands of Hz, and these noises are useless for the pulse signal waveform. We can filter out signals beyond a certain threshold through a low-pass filter, and the resulting waveform will be smoother.
2. Practice
First we use the scipy module, which can be installed using the following command :(I use Python==3.6)
pip install scipy
Copy the code
1). Low-pass filtering
Here, it is assumed that the sampling frequency is 1000Hz and the maximum frequency of the signal itself is 500Hz. To filter out the frequency components above 400Hz, that is, the cut-off frequency is 400Hz, wn=2*400/1000=0.8. Wn = 0.8
B, a = signal.butter(8, 0.8, 'lowpass') filtedData = signal.filtfilt(b, a, data)Copy the code
2). High-pass filtering
Here, it is assumed that the sampling frequency is 1000Hz and the maximum frequency of the signal itself is 500Hz. To filter out the frequency components below 100Hz, that is, the cut-off frequency is 100Hz, wn=2*100/1000=0.2. Wn = 0.2
B, a = signal.butter(8, 0.2, 'highpass') filtedData = signal.filtfilt(b, a, data)Copy the code
3). Band pass filtering
Here, it is assumed that the sampling frequency is 1000Hz, and the maximum frequency of the signal itself is 500Hz. To filter the frequency components below 100Hz and above 400Hz, that is, the cut-off frequency is 100,400 Hz, then WN1 =2100/1000=0.2, Wn1=0.2; Wn2 = 2400/1000 = 0.8, wn2 = 0.8. Wn = [0.02, 0.8]
B, a = signal.butter(8, [0.2,0.8], 'bandPass ') filtedData = signal.filtfilt(b, a, data)Copy the code
4) band stop filtering
Here, it is assumed that the sampling frequency is 1000Hz, and the maximum frequency of the signal itself is 500Hz. To filter out the frequency components above 100Hz and below 400Hz, that is, the cut-off frequency is 100,400 Hz, then WN1 =2100/1000=0.2, Wn1=0.2; Wn2 = 2400/1000 = 0.8, wn2 = 0.8. Wn=[0.02,0.8], similar to band pass, but band pass is reserved in the middle, while band stop is removed.
B, a = signal.butter(8, [0.2,0.8], 'bandStop ') filtedData = signal.filtfilt(b, a, data)Copy the code
3. Function introduction
- Introduction to functions
(1). Filtering function
scipy.signal.filtfilt(b, a, x, axis=-1, padtype=’odd’, padlen=None, method=’pad’, irlen=None)
Input parameters:
B: molecular coefficient vector of the filter
A: The filter denominator coefficient vector
X: The array of data to filter. (array)
Axis: Specifies the axis of the data array X to be filtered
Padtype: Must be odd, even, constant, or none. This determines the type of extension used to fill the signal for filter applications. {‘ odd ‘, ‘even’, ‘constant’, None}
Padlen: The number of elements extending X at each end of the axis before applying the filter. This value must be less than the number of elements to filter -1. (int or None)
Method: Determines the method for processing signal edges. When method is “pad”, the signal is filled. The padding type padtype and padlen are determined, and irlen is ignored. When Method is “gust”, use the Gustafson method and omit padType and padlen. {” pad “, “gust”}
Irlen: When Method is “gust”, irlen specifies the length of the filter’s pulse response. If irlen is None, any part of the impulse response is ignored. For long signals, specifying IRLEN can significantly improve the performance of the filter. (int or None)
Output parameters:
Y: filtered data array
(2). Filter constructor (Butterworth filter only)
scipy.signal.butter(N, Wn, btype=’low’, analog=False, output=’ba’)
Input parameters:
N: the order of the filter
Wn: Normalized cutoff frequency. Wn=2 * cutoff frequency/sampling frequency. (Note: according to the sampling theorem, the sampling frequency must be greater than twice the maximum frequency of the signal itself in order to restore the signal. The cutoff frequency must be less than the maximum frequency of the signal itself, so Wn must be between 0 and 1). When constructing a band-pass filter or band-stop filter, Wn is a list of length 2.
Btype: filter type {‘ lowpass ‘, ‘highpass’, ‘bandPass’, ‘bandStop’},
Output: Output type {‘ ba ‘, ‘ZPK’, ‘SOS’}
Output parameters:
B, A: polynomial coefficient vectors for the numerator (b) and denominator (a) of the IIR filter. output=’ba’
Z, P,k: Zero, pole, and system gain of the IIR filter transfer function.
SOS: Second order cross section representation of IIR filter. output= ‘sos’
- reference
Blog.csdn.net/weixin_3799…
Docs.scipy.org/doc/scipy-0…
Docs.scipy.org/doc/scipy-0…