Reputation: 231
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.
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.
Upvotes: 0
Views: 2511
Reputation: 114811
The least squares FIR filter design function in scipy is scipy.signal.firls
(not scipy.signal.firwin
).
firls
requires an odd number of taps, so you'll have to ensure that filter_order
is odd.
If firwin
is actually the function that you meant to use, then
take another look at the docstring. In particular:
firwin
does not take an argument for the ideal response. It is only given the band edges in the cutoff
argument.cutoff
argument specifically says this argument must not contain 0 and the Nyquist frequency.You appear to be creating a bandpass filter. There is an example of this in the 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.
Here's how you implement your filter using 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
.
Upvotes: 1