Simon
Simon

Reputation: 10160

python bandpass filter - singular matrix error

I've been trying to design a bandpass filter using scipy but I keep getting a LinAlg Singular Matrix error. I read that a singular matrix is one that is not invertable, but I'm not sure how that error is coming up and what I can do to fix it

The code takes in an EEG signal (which, in the code below, I have just replaced with an int array for testing) and filters out frequencies < 8Hz and > 12Hz (alpha band)

Can anyone shed some light on where the singular matrix error is coming from? Or alternatively, if you know of a better way to filter a signal like this I'd love to test out other options too

from scipy import signal
from scipy.signal import filter_design as fd
import matplotlib.pylab as plt

#bandpass
Wp = [8, 12]   # Cutoff frequency
Ws = [7.5, 12.5]   # Stop frequency
Rp = 1             # passband maximum loss (gpass)
As = 100              # stoppand min attenuation (gstop)

b,a = fd.iirdesign(Wp,Ws,Rp,As,ftype='butter')
w,H = signal.freqz(b,a)  # filter response
plt.plot(w,H)

t = np.linspace(1,256,256)
x = np.arange(256)
plt.plot(t,x)

y = signal.filtfilt(b,a,x)
plt.plot(t,y)

Upvotes: 1

Views: 1862

Answers (1)

SleuthEye
SleuthEye

Reputation: 14577

As indicated in iirdesign documentation, Wp and Ws are "are normalized from 0 to 1, where 1 is the Nyquist frequency".

If your sampling rate is Fs (for example 100Hz) you can normalize the cutoff and stop frequencies using:

Wp = [x / (Fs/2.0) for x in Wp]
Ws = [x / (Fs/2.0) for x in Ws]

Upvotes: 1

Related Questions