用python设计FIR陷波滤波器

2024-03-29 12:07:08 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在用Python编写一些代码,使用scipy.signal库过滤电磁数据,这些数据混合了各种我想要过滤掉的不需要的签名。例如,我有不同频率(即60、120赫兹等)的电力线谐波,宽度只有几赫兹,我想用陷波滤波器从数据中去除。python中是否已经有了一个函数,我可以只通知代码我希望使用多少个数据点作为过滤器,我希望删除的中心线频率和过渡带的宽度,还是需要从头设计一个过滤器?如果是后者,我将非常感谢Python中的notch filter设计示例,其中包括窗口实现以最小化别名。


Tags: 数据函数代码过滤器signal宽度电磁scipy
1条回答
网友
1楼 · 发布于 2024-03-29 12:07:08

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

相关问题 更多 >