调用signal.firwin时在Python函数中获取值错误

时间:2020-11-27 23:13:51

I am using Least Square filter function in python which is firwin and it is in signal library and when i am calling the function it is raising Value Error.

我在python中使用最小二乘滤波器函数,它是firwin,它在信号库中,当我调用函数时,它会引发Value Error。

My Code Snippet:

我的代码片段:

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)

Error:

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.

1 个解决方案

#1


1  

The least squares FIR filter design function in scipy is scipy.signal.firls (not scipy.signal.firwin).

scipy中的最小二乘FIR滤波器设计函数是scipy.signal.firls(不是scipy.signal.firwin)。

firls requires an odd number of taps, so you'll have to ensure that filter_order is odd.

firls需要奇数个抽头,所以你必须确保filter_order是奇数。


If firwin is actually the function that you meant to use, then take another look at the docstring. In particular:

如果firwin实际上是您打算使用的函数,那么再看一下docstring。特别是:

  • firwin does not take an argument for the ideal response. It is only given the band edges in the cutoff argument.
  • firwin没有对理想的反应进行论证。它只在cutoff参数中给出了波段边缘。

  • The description of the cutoff argument specifically says this argument must not contain 0 and the Nyquist frequency.
  • 截止参数的描述明确说明该参数不得包含0和奈奎斯特频率。

  • You appear to be creating a bandpass filter. There is an example of this in the docstring:

    您似乎正在创建带通滤波器。在docstring中有一个这样的例子:

    Band-pass:
    
    >>> f1, f2 = 0.1, 0.2
    >>> signal.firwin(numtaps, [f1, f2], pass_zero=False)
    array([ 0.06301614,  0.88770441,  0.06301614])
    
  • The first argument of firwin must be an integer, not a float.

    firwin的第一个参数必须是整数,而不是浮点数。

Here's how you implement your filter using firwin:

以下是使用firwin实现过滤器的方法:

lower = lower_filter_bound/nyquist
upper = upper_filter_bound/nyquist
filterweights = sig.firwin(int(filter_order), [lower, upper], pass_zero=False)

If you need more flexibility in the design of your FIR filter, take a look at scipy.signal.firwin2.

如果您需要更灵活地设计FIR滤波器,请查看scipy.signal.firwin2。

#1


1  

The least squares FIR filter design function in scipy is scipy.signal.firls (not scipy.signal.firwin).

scipy中的最小二乘FIR滤波器设计函数是scipy.signal.firls(不是scipy.signal.firwin)。

firls requires an odd number of taps, so you'll have to ensure that filter_order is odd.

firls需要奇数个抽头,所以你必须确保filter_order是奇数。


If firwin is actually the function that you meant to use, then take another look at the docstring. In particular:

如果firwin实际上是您打算使用的函数,那么再看一下docstring。特别是:

  • firwin does not take an argument for the ideal response. It is only given the band edges in the cutoff argument.
  • firwin没有对理想的反应进行论证。它只在cutoff参数中给出了波段边缘。

  • The description of the cutoff argument specifically says this argument must not contain 0 and the Nyquist frequency.
  • 截止参数的描述明确说明该参数不得包含0和奈奎斯特频率。

  • You appear to be creating a bandpass filter. There is an example of this in the docstring:

    您似乎正在创建带通滤波器。在docstring中有一个这样的例子:

    Band-pass:
    
    >>> f1, f2 = 0.1, 0.2
    >>> signal.firwin(numtaps, [f1, f2], pass_zero=False)
    array([ 0.06301614,  0.88770441,  0.06301614])
    
  • The first argument of firwin must be an integer, not a float.

    firwin的第一个参数必须是整数,而不是浮点数。

Here's how you implement your filter using firwin:

以下是使用firwin实现过滤器的方法:

lower = lower_filter_bound/nyquist
upper = upper_filter_bound/nyquist
filterweights = sig.firwin(int(filter_order), [lower, upper], pass_zero=False)

If you need more flexibility in the design of your FIR filter, take a look at scipy.signal.firwin2.

如果您需要更灵活地设计FIR滤波器,请查看scipy.signal.firwin2。