设计带阻滤波器(SciPy firwin)

2024-10-01 13:29:01 发布

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

我正在尝试使用kaiserord窗口为我的数据设计一组过滤器,firwin。我已经设法创建了一个低通滤波器和一个基于互联网上提供的信息的带通滤波器。然而,我不能创建一个带阻滤波器和高通滤波器。我使用firwin的pass_zero输入来选择滤波器是带通/低通滤波器还是带阻/高通滤波器(as indicated in firwin documentation

我对高通滤波器使用以下代码:

from scipy.signal import kaiserord, firwin, freqz
from pylab import figure, clf, plot, xlabel, ylabel, xlim, ylim, title, grid, axes, show

# The Nyquist rate of the signal.
nyq_rate = sample_rate / 2.0

# The desired width of the transition from pass to stop,
# relative to the Nyquist rate.
width = 5/nyq_rate

# The desired attenuation in the stop band, in dB.
ripple_db = 60.0

# Compute the order and Kaiser parameter for the FIR filter.
N, beta = kaiserord(ripple_db, width)
print('Filter order: ', N)

# Use firwin with a Kaiser window to create the high-pass FIR filter.
taps = firwin(N, 15/nyq_rate, window=('kaiser', beta), pass_zero=False)

figure(1)
clf()
w, h = freqz(taps, worN=8000)
plot((w/pi)*nyq_rate, absolute(h), linewidth=2)
xlabel('Frequency (Hz)')
ylabel('Gain')
title('Frequency Response')
ylim(-0.05, 1.05)
grid(True)

这将引发以下错误:

^{pr2}$

有人能帮我用凯瑟罗设计高通和带阻滤波器吗?在

非常感谢! 日期


Tags: thetoinfromimportsignalratepass
1条回答
网友
1楼 · 发布于 2024-10-01 13:29:01

在调用firwin之前,添加语句

N |= 1

这将确保N是奇数。该语句相当于N=N | 1,|是位or运算符。语句将N的最低位设置为1。在

引用函数的docstring

This function computes the coefficients of a finite impulse response filter. The filter will have linear phase; it will be Type I if numtaps is odd and Type II if numtaps is even.

Type II filters always have zero response at the Nyquist frequency, so a ValueError exception is raised if firwin is called with numtaps even and having a passband whose right end is at the Nyquist frequency.

换言之:firwin返回的系数数组始终具有偶数对称性,并且从数学上讲,具有偶数对称性和偶数个抽头的滤波器在Nyquist频率下自动具有零响应,因此,设计一个抽头数为偶数的高通或带阻滤波器是没有意义的。在

看一下这个问答,了解更多关于FIR滤波器类型的背景知识:https://dsp.stackexchange.com/questions/9408/fir-filter-with-linear-phase-4-types

相关问题 更多 >