我正在使用scipy.signal库编写一些Python代码,以过滤与我想要过滤掉的各种不需要的签名混合的电磁数据。例如,我有各种频率(即60,120 Hz等)的电力线谐波,宽度只有几Hz,我想用陷波滤波器从数据中去掉。在python中是否已经有一个现有的函数,我只能告诉代码我希望用于滤波器的数据点数,我希望删除的中心线频率和过渡带的宽度,还是我需要设计从头开始过滤?如果是后者,我将非常感谢Python中的陷波滤波器设计示例,其中包括窗口实现以最小化混叠。
答案 0 :(得分:2)
scipy.signal网站上的解决方案有几个选项,但它们会引入分配振铃,这将转换为卷积信号中的伪像。经过多次尝试后,我发现以下功能最适合实现FIR陷波滤波器。
# 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