使用 python 设计 FIR 陷波滤波器

Designing an FIR notch filter with python

我正在用 Python 编写一些代码,使用 scipy.signal 库来过滤电磁数据,这些数据与我想要过滤掉的各种不需要的签名混合在一起。例如,我有各种频率(即 60、120 Hz 等......)的电源线谐波,宽度只有几赫兹,我想使用陷波滤波器从数据中去除这些谐波。 python 中是否已经存在一个函数,我只能在其中告知代码我希望用于滤波器的数据点数、我希望移除的中心线频率以及过渡带的宽度,或者我需要从头开始设计过滤器吗?如果是后者,我将非常感谢 Python 中的陷波滤波器设计示例,其中包含 window 实现以最小化混叠。

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