利用Python scipy.signal.filtfilt() 实现信号滤波
https://blog.csdn.net/weixin_37996604/article/details/82864680
https://www.cnblogs.com/xiaosongshine/p/10831931.html
from scipy import signal
'''
(1).低通滤波
这里假设采样频率为1000hz,信号本身最大的频率为500hz,要滤除400hz以上频率成分,
即截至频率为400hz,则wn=2*400/1000=0.8 ==> Wn=0.8
'''
b, a = signal.butter(8, 0.8, 'lowpass') #配置滤波器 8 表示滤波器的阶数
filtedData = signal.filtfilt(b, a, data) #data为要过滤的信号
'''
2).高通滤波
这里假设采样频率为1000hz, 信号本身最大的频率为500hz,要滤除100hz以下频率成分,
即截至频率为100hz, 则wn = 2 * 100 / 1000 = 0.2。Wn = 0.2
'''
b, a = signal.butter(8, 0.2, 'highpass') # 配置滤波器 8 表示滤波器的阶数
filtedData = signal.filtfilt(b, a, data) # data为要过滤的信号
'''
3).带通滤波
这里假设采样频率为1000hz, 信号本身最大的频率为500hz,要滤除100hz以下,400hz以上频率成分,
即截至频率为100,400hz, 则wn1 = 2 * 100 / 1000 = 0.2,Wn1 = 0.2; wn2 = 2 * 400 / 1000 = 0.8,Wn2 = 0.8。Wn = [0.02, 0.8]
'''
b, a = signal.butter(8, [0.2, 0.8], 'bandpass') # 配置滤波器 8 表示滤波器的阶数
filtedData = signal.filtfilt(b, a, data) # data为要过滤的信号
'''
4).带阻滤波
这里假设采样频率为1000hz, 信号本身最大的频率为500hz,要滤除100hz以上,400hz以下频率成分,
即截至频率为100,400hz, 则wn1 = 2 * 100 / 1000 = 0.2,Wn1 = 0.2; wn2 = 2 * 400 / 1000 = 0.8,Wn2 = 0.8。Wn = [0.02, 0.8],
和带通相似,但是带通是保留中间,而带阻是去除。
'''
b, a = signal.butter(8, [0.2, 0.8], 'bandstop') # 配置滤波器 8 表示滤波器的阶数
filtedData = signal.filtfilt(b, a, data) # data为要过滤的信号
Notch Filter
https://stackoverflow.com/questions/35565540/designing-an-fir-notch-filter-with-python
Required input defintions are as follows;
time: Time between samples
band: The bandwidth around the centerline freqency that you wish to filter
freq: The centerline frequency to be filtered
ripple: The maximum passband ripple that is allowed in db
order: The filter order. For FIR notch filters this is best set to 2 or 3, IIR filters are best suited for high values of order. This algorithm is hard coded to FIR filters
filter_type: 'butter', 'bessel', 'cheby1', 'cheby2', 'ellip'
data: the data to be filtered
# Required input defintions are as follows;
# time: Time between samples
# band: The bandwidth around the centerline freqency that you wish to filter
# freq: The centerline frequency to be filtered
# ripple: The maximum passband ripple that is allowed in db
# order: The filter order. For FIR notch filters this is best set to 2 or 3,
# IIR filters are best suited for high values of order. This algorithm
# is hard coded to FIR filters
# filter_type: 'butter', 'bessel', 'cheby1', 'cheby2', 'ellip'
# data: the data to be filtered
def Implement_Notch_Filter(time, band, freq, ripple, order, filter_type, data):
from scipy.signal import iirfilter
fs = 1/time
nyq = fs/2.0
low = freq - band/2.0
high = freq + band/2.0
low = low/nyq
high = high/nyq
b, a = iirfilter(order, [low, high], rp=ripple, btype='bandstop',
analog=False, ftype=filter_type)
filtered_data = lfilter(b, a, data)
return filtered_data
用python设计FIR陷波滤波器
https://docs.scipy.org/doc/scipy/reference/generated/scipy.signal.iirnotch.html
https://www.cnpython.com/qa/85882
https://vimsky.com/examples/usage/python-scipy.signal.iirnotch.html
https://github.com/scipy/scipy/blob/master/scipy/signal/filter_design.py
https://blog.csdn.net/qq_41978536/article/details/90736793
from scipy import signal
import matplotlib.pyplot as plt
fs = 200.0 # Sample frequency (Hz)
f0 = 60.0 # Frequency to be removed from signal (Hz)
Q = 30.0 # Quality factor
# Design notch filter
b, a = signal.iirnotch(f0, Q, fs)
# Frequency response
freq, h = signal.freqz(b, a, fs=fs)
# Plot
fig, ax = plt.subplots(2, 1, figsize=(8, 6))
ax[0].plot(freq, 20*np.log10(abs(h)), color='blue')
ax[0].set_title("Frequency Response")
ax[0].set_ylabel("Amplitude (dB)", color='blue')
ax[0].set_xlim([0, 100])
ax[0].set_ylim([-25, 10])
ax[0].grid()
ax[1].plot(freq, np.unwrap(np.angle(h))*180/np.pi, color='green')
ax[1].set_ylabel("Angle (degrees)", color='green')
ax[1].set_xlabel("Frequency (Hz)")
ax[1].set_xlim([0, 100])
ax[1].set_yticks([-90, -60, -30, 0, 30, 60, 90])
ax[1].set_ylim([-90, 90])
ax[1].grid()
plt.show()
网友评论