简体   繁体   English

SciPy“lfilter”仅返回NaN

[英]SciPy “lfilter” returns only NaNs

All - 全部 -

I am trying to use SciPy's signal.lfilter function to filter a vector of samples - unfortunately, all that is returned is a vector of NaN . 我正在尝试使用SciPy的signal.lfilter函数来过滤样本矢量 - 不幸的是,返回的所有内容都是NaN的矢量。

I have plotted the frequency response of the filter, and the filter coefficients look correct; 我绘制了滤波器的频率响应,滤波器系数看起来正确; I'm fairly certain the issue is with the actual call to lfilter . 我很确定问题在于对lfilter的实际调用。

It's a high-pass Chebychev I filter, which I'm creating with: 这是一个高通的Chebychev I过滤器,我正在创建:

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

I am then filtering data with: 然后我用以下方法过滤数据:

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

Below, I am printing a selection of 20 samples from the pre-filtered data, and then the filtered data. 下面,我正在从预过滤数据中打印20个样本,然后是过滤后的数据。 You can clearly see the issue: 您可以清楚地看到问题:

### 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]

Like I said before, the coefficients of the filter look good. 就像我之前说的那样,滤波器的系数看起来很好。 They are: 他们是:

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]

So why would lfilter return only NaN? 那么为什么lfilter只返回NaN? How am I using this function incorrectly? 我怎么不正确地使用这个功能?

Thanks in advance for your help! 在此先感谢您的帮助!

Edit: 编辑:

Okay, I solved it. 好的,我解决了。

For anyone that encounters this in the future: 对于将来遇到此事的任何人:

For whatever reason, even though the returned coefficients for the filter looked good, when I then used those coefficients in SciPy's lfilter function, the filtered values were unbounded. 无论出于何种原因,即使返回的滤波器系数看起来很好,当我在SciPy的lfilter函数中使用这些系数时,滤波后的值也是无界的。 Simply changing the passband edge to ANY number other than 0.11 fixed the problem. 只需将通带边缘更改为0.11以外的任何数字即可解决问题。 Even this works: 即便如此:

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

Other than manually grepping through the poles and zeros of the filter, I'm not sure how you would detect instability of the filter. 除了手动点击滤波器的极点和零点之外,我不确定如何检测滤波器的不稳定性。 Bizarre. 离奇。

An IIR filter is stable if the absolute values of the roots of the denominator of the discrete transfer function a(z) are all less than one. 如果离散传递函数a(z)的分母的根的绝对值都小于1,则IIR滤波器是稳定的。 So, you can detect the instability by following code: 因此,您可以通过以下代码检测不稳定性:

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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM