signal.butter bandpass error: Digital filter critical frequencies must be 0 < Wn < 1

1.8k views Asked by At

I have been trying to use scipy's butter function and it has been working fine when I am trying to get the lowpass coefficients:

from scipy import signal
[b, a] = signal.butter(10, 0.3)

I strictly want to create a bandpass for 0.5Hz - 5Hz, but when I try to get the bandpass coefficients in the following code it gives me an error: ValueError: Digital filter critical frequencies must be 0 < Wn < 1

from scipy import signal

[b, a] = signal.butter(2, [0.5,5], btype='bandpass')

I did find the following stackoverflow question, but I am still unsure about the issue and I followed the requirements for using the signal.butter function from the documentation.

1

There are 1 answers

0
Warren Weckesser On BEST ANSWER

By default, for a digital filter, the values given for Wn must be expressed as fractions of the Nyquist frequency, which is half the samping rate. Either scale the values yourself, or specify the sample rate by also giving the fs argument, as in the following example

In [36]: from scipy import signal

In [37]: order = 2

In [38]: fs = 128.0  # Sample rate, in Hz                                             

In [39]: Wn = [0.5, 5.0]  # Filter cutoff frequencies, in Hz                                      

In [40]: b, a = signal.butter(order, Wn, btype='bandpass', fs=fs)                                     

In [41]: b                                                                                                           
Out[41]: array([ 0.01051921,  0.        , -0.02103843,  0.        ,  0.01051921])

In [42]: a                                                                                                           
Out[42]: array([ 1.        , -3.67848002,  5.09032297, -3.14352142,  0.73170972])