我在python中使用最小二乘滤波器函数 firwin 并且它在信号库中,当我调用函数时它会引发值错误。
我的代码片段:
Fs = 100
epochs = n/Fs
nyquist = Fs/2
lower_filter_bound = 7;
upper_filter_bound = 13;
transition_width = 0.7;
filter_order = np.round((Fs/lower_filter_bound))
idealresponse = [ 0, 0, 1, 1, 0, 0 ];
filterName = 'Band pass filtered';
F = [0, (((1-transition_width)*lower_filter_bound)/nyquist),
(lower_filter_bound/nyquist),(upper_filter_bound/nyquist),(((1+transition_width)*upper_filter_bound)/nyquist), nyquist/nyquist];
filterweights = sig.firwin(filter_order, F, idealresponse)
错误:
Traceback (most recent call last):
File "File.py", line 34, in <module>
filterweights = sig.firwin(filter_order, F, idealresponse)
raise ValueError("Invalid cutoff frequency: frequencies must be "
ValueError: Invalid cutoff frequency: frequencies must be greater than 0 and less than nyq.
答案 0 :(得分:1)
scipy中的最小二乘FIR滤波器设计函数为scipy.signal.firls
(不是scipy.signal.firwin
)。
firls
需要奇数次点击,因此您必须确保filter_order
为奇数。
如果firwin
实际上是您打算使用的功能,那么
再看一下docstring。特别是:
firwin
没有对理想的回答进行论证。它仅在cutoff
参数中给出了波段边缘。cutoff
参数的描述明确指出此参数不得包含0和奈奎斯特频率。您似乎正在创建带通滤波器。 docstring中有一个例子:
Band-pass:
>>> f1, f2 = 0.1, 0.2
>>> signal.firwin(numtaps, [f1, f2], pass_zero=False)
array([ 0.06301614, 0.88770441, 0.06301614])
firwin
的第一个参数必须是整数,而不是浮点数。
以下是使用firwin
实现过滤器的方式:
lower = lower_filter_bound/nyquist
upper = upper_filter_bound/nyquist
filterweights = sig.firwin(int(filter_order), [lower, upper], pass_zero=False)
如果您需要更灵活地设计FIR滤波器,请查看scipy.signal.firwin2
。