20hz-20000hz Butterworth filtering exploding

房东的猫 提交于 2019-12-07 15:28:27

scipy.signal.butter is generating an unstable filter:

In [17]: z, p, k = signal.tf2zpk(b, a)

In [18]: np.max(np.abs(p))
Out[18]: 1.0005162676670694

For a stable filter, that maximum must be less than 1. Unfortunately, the code doesn't warn you about this.

I suspect the problem is b1, not b2. In the normalized units, you are trying to create a lower cutoff of 2.1e-4, which is pretty small. If, for example, the lower cutoff is 200.0/nyq, the filter is stable:

In [13]: b, a = signal.butter(5, [200.0 / nyq, 20000.0 / nyq], btype='band')

In [14]: z, p, k = signal.tf2zpk(b, a)

In [15]: np.max(np.abs(p))
Out[15]: 0.99601892668982284

Instead of using the (b, a) format for the filter, you can use the more robust sos (second order sections) format, which was added to scipy version 0.16. To use it, change these two lines

b, a = signal.butter(5, [20.0 / nyq, 20000.0 / nyq], btype='band')
x = signal.lfilter(b, a, x)

to

sos = signal.butter(5, [20.0 / nyq, 20000.0 / nyq], btype='band', output='sos')
x = signal.sosfilt(sos, x)

That SOS filter does not suffer from the instability problem.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!