SciPy“lfilter”仅返回NaN

时间:2012-01-10 22:32:43

标签: python numpy signal-processing scipy

全部 -

我正在尝试使用SciPy的signal.lfilter函数来过滤样本向量 - 遗憾的是,返回的所有内容都是 NaN 的向量。

我绘制了滤波器的频率响应,滤波器系数看起来正确;我很确定问题在于对lfilter的实际调用。

这是一个高通的Chebychev I过滤器,我正在创建:

b,a = signal.iirdesign(wp = 0.11, ws= 0.1, gstop= 60, gpass=1, ftype='cheby1')

然后我用以下内容过滤数据:

filtered_data = signal.lfilter(b, a, data)

下面,我正在从预过滤数据中打印20个样本,然后是过滤后的数据。您可以清楚地看到问题:

### Printing a small selection of the data before it is filtered:

((-0.003070347011089325+0.0073614344000816345j), (-0.003162827342748642+0.007342938333749771j), (-0.003310795873403549+0.0073614344000816345j), (-0.0031813234090805054+0.007342938333749771j), (-0.003255307674407959+0.007398426532745361j), (-0.003162827342748642+0.007287450134754181j), (-0.003125835210084915+0.007509402930736542j), (-0.003162827342748642+0.007342938333749771j), (-0.0031073391437530518+0.007287450134754181j), (-0.0032368116080760956+0.007398426532745361j), (-0.0030888430774211884+0.007342938333749771j))


### Printing a small selection of the filtered data:

[ nan nanj  nan nanj  nan nanj  nan nanj  nan nanj  nan nanj  nan nanj
  nan nanj  nan nanj  nan nanj  nan nanj  nan nanj  nan nanj  nan nanj
  nan nanj  nan nanj  nan nanj  nan nanj  nan nanj  nan nanj]

就像我之前说的那样,滤波器的系数看起来很好。他们是:

b = [  4.06886235e-02  -7.73083846e-01   6.95775461e+00  -3.94272761e+01
   1.57709105e+02  -4.73127314e+02   1.10396373e+03  -2.05021836e+03
   3.07532754e+03  -3.75873366e+03   3.75873366e+03  -3.07532754e+03
   2.05021836e+03  -1.10396373e+03   4.73127314e+02  -1.57709105e+02
   3.94272761e+01  -6.95775461e+00   7.73083846e-01  -4.06886235e-02]
a = [  1.00000000e+00  -1.27730099e+01   7.81201390e+01  -3.03738394e+02
   8.40827723e+02  -1.75902089e+03   2.88045462e+03  -3.77173152e+03
   3.99609428e+03  -3.43732844e+03   2.38415171e+03  -1.30118368e+03
   5.21654119e+02  -1.18026566e+02  -1.85597824e+01   3.24205235e+01
  -1.65545917e+01   5.02665439e+00  -9.09697811e-01   7.68172820e-02]

那么为什么lfilter只返回NaN?我是如何错误地使用此功能的?

提前感谢您的帮助!

修改

好的,我解决了。

对于将来遇到此事的任何人:

无论出于何种原因,即使返回的滤波器系数看起来很好,当我在SciPy的lfilter函数中使用这些系数时,滤波后的值也是无界的。只需将通带边缘更改为除0.11以外的 ANY 编号,即可解决问题。即便如此:

b,a = signal.iirdesign(wp = 0.119, ws= 0.1, gstop= 60, gpass=1, ftype='cheby1')

除了手动点击过滤器的极点和零点外,我不确定如何检测过滤器的不稳定性。奇异。

1 个答案:

答案 0 :(得分:9)

如果离散传递函数a(z)的分母的根的绝对值都小于1,则IIR滤波器是稳定的。因此,您可以通过以下代码检测不稳定性:

from scipy import signal
import numpy as np
b1, a1 = signal.iirdesign(wp = 0.11, ws= 0.1, gstop= 60, gpass=1, ftype='cheby1')
b2, a2 = signal.iirdesign(wp = 0.119, ws= 0.1, gstop= 60, gpass=1, ftype='cheby1')

print "filter1", np.all(np.abs(np.roots(a1))<1)
print "filter2", np.all(np.abs(np.roots(a2))<1)