全部 -
我正在尝试使用SciPy的signal.lfilter
函数过滤样本向量-不幸的是,返回的只是NaN向量。
我已经画出了滤波器的频率响应,滤波器系数看起来是正确的。我相当确定问题出在对lfilter
的实际调用中。
这是我要创建的高通Chebychev滤波器,它是通过以下方式创建的:
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
以外的任何数字即可解决此问题。即使这样有效:b,a = signal.iirdesign(wp = 0.119, ws= 0.1, gstop= 60, gpass=1, ftype='cheby1')
除了手动过滤滤波器的零点和零点外,我不确定如何检测滤波器的不稳定性。怪异的
最佳答案
如果离散传递函数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)
关于python - SciPy "lfilter"仅返回NaN,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/8811518/