Scipy bandpass filter returns infinite or nan values

957 Views Asked by At

I want to implement a bandpass filter to retrieve all values with a frequency between 150 kHz and 500 kHz. The sampling rate is 1 MHz. Since the function butter from scipy doesn't allow 500 kHz, I replaced it by 499.999 kHz. The input is a pandas.Series with 2.388 million entries. There are no nan values.

print(series)
Out:
0          0.354182
1          0.345122
2          0.361368
3          0.353870
4          0.351058

2387995    0.362617
2387996    0.363867
2387997    0.374490
2387998    0.375739
2387999    0.362617
Name: Input, Length: 2388000, dtype: float64

Input Series

Here is the code:

from scipy.signal import butter, lfilter

def butter_bandpass_filter(data, lowcut, highcut, fs, order=5):
    nyq = 0.5 * fs
    low = lowcut / nyq
    high = highcut / nyq

    b, a = butter(order, [low, high], btype='band')
    y = lfilter(b, a, data)
    return y

fs = 1000000.0
lowcut = 150000.0
highcut = 499999.0

y = butter_bandpass_filter(series.values, lowcut, highcut, fs, order=6)

The values of y grow exponentially until the are infinite (after 477350 values) and afterwards they are only nan.

print(y)
Out:
[ 0.05232346 -0.13843946  0.05059037 ...         nan         nan
         nan]

print(y[477340:477356])
Out:
[ 4.89094265e+307 -4.89827252e+307  4.90561347e+307 -4.91296550e+307
  4.92032864e+307 -4.92770291e+307  4.93508831e+307 -4.94248488e+307
  4.94989261e+307 -4.95731154e+307             -inf              inf
              nan              nan              nan              nan]

Does anyone have any idea what I am doing wrong?

0

There are 0 best solutions below