将二阶Butterworth转换为第一顺序 - 第二部分 -

时间:2017-07-18 13:21:05

标签: python filter butterworth

我试图将我工作的二阶巴特沃斯低通滤波器转换为python中的一阶,但它给了我很大的数字,比如flt_y_1st [299]:26198491071387576370322954146679741443295686950912.0。这是我的二阶和一阶巴特沃思:

import math
import numpy as np
import matplotlib.pyplot as plt

from scipy.signal import lfilter
from scipy.signal import butter

def butter_lowpass(cutoff, fs, order=1):
    nyq = 0.5 * fs
    normal_cutoff = cutoff / nyq
    b, a = butter(order, normal_cutoff, btype='low', analog=False)
    return b, a

def butter_lowpass_filter(data, cutoff, fs, order=1):
    b, a = butter_lowpass(cutoff, fs, order=order)
    y = lfilter(b, a, data)
    return y

def bw_2nd(y, fc, fs):
    filtered_y = np.zeros(len(y))

    omega_c = math.tan(np.pi*fc/fs)
    k1 = np.sqrt(2)*omega_c
    k2 = omega_c**2
    a0 = k2/(1+k1+k2)
    a1 = 2*a0
    a2 = a0
    k3 = 2*a0/k2
    b1 = -(-2*a0+k3)
    b2 = -(1-2*a0-k3)

    filtered_y[0] = y[0]
    filtered_y[1] = y[1]

    for i in range(2, len(y)):
        filtered_y[i] = a0*y[i]+a1*y[i-1]+a2*y[i-2]-(b1*filtered_y[i-1]+b2*filtered_y[i-2])

    return filtered_y


def bw_1st(y, fc, fs):
    filtered_y = np.zeros(len(y))

    omega_c = math.tan(np.pi*fc/fs)
    k1 = np.sqrt(2)*omega_c
    k2 = omega_c**2
    a0 = k2/(1+k1+k2)
    a1 = 2*a0
    k3 = 2*a0/k2
    b1 = -(-2*a0+k3)
#    b1 = -(-2*a0)  # <= Removing k3 makes better, but still not perfect

    filtered_y[0] = y[0]

    for i in range(1, len(y)):
       filtered_y[i] = a0*y[i]+a1*y[i-1]-(b1*filtered_y[i-1])

    return filtered_y

f = 100
fs = 2000
x = np.arange(300)
y = np.sin(2 * np.pi * f * x / fs)

flt_y_2nd = bw_2nd(y, 120, 2000)
flt_y_scipy = butter_lowpass_filter(y, 120, 2000, 1)
flt_y_1st = bw_1st(y, 120, 2000)

for i in x:
    print('y[%d]: %6.3f flt_y_2nd[%d]: %6.3f flt_y_scipy[%d]: %6.3f flt_y_1st[%d]: %8.5f' % (i, y[i], i, flt_y_2nd[i], i, flt_y_scipy[i], i, flt_y_1st[i]))

plt.subplot(1, 1, 1)
plt.xlabel('Time [ms]')
plt.ylabel('Acceleration [g]')
lines = plt.plot(x, y, x, flt_y_2nd, x, flt_y_scipy, x, flt_y_1st)
l1, l2, l3, l4 = lines
plt.setp(l1, linewidth=1, color='g', linestyle='-')
plt.setp(l2, linewidth=1, color='b', linestyle='-')
plt.setp(l3, linewidth=1, color='y', linestyle='-')
plt.setp(l4, linewidth=1, color='r', linestyle='-')
plt.legend(["y", "flt_y_2nd", "flt_y_scipy", "flt_y_1st"])
plt.grid(True)
plt.xlim(0, 150)
plt.ylim(-1.5, 1.5)
plt.title('flt_y_2nd vs. flt_y_scipy vs. flt_y_1st')
plt.show()

...我删除了所有[i-2],这是一个前馈和反馈。

b1 = -(-2*a0+k3)

然而,似乎还不够。我想我需要改变a0,b1等中的一些方程式。例如,当我删除&#39; + k3&#39;从b1开始,我得到一个这样的情节(看起来更好,不是吗?):

b1 = -(-2*a0)

我并不专注于过滤器,但至少我知道这个第一顺序与scipy.butter不同。所以,请帮我找到正确的系数。提前谢谢。

以下是我的参考:filtering_considerations.pdf

1 个答案:

答案 0 :(得分:0)

让我回答一下自己。

最终系数为:

omega_c = math.tan(np.pi*fc/fs)
k1 = np.sqrt(2)*omega_c
a0 = k1/(math.sqrt(2)+k1)
a1 = a0
b1 = -(1-2*a0)

这是怎么回事。 我从scipy.butter反向设计它们,正如@sizzzzlerz建议的那样(谢谢)。 scipy.butter吐出这些系数:

b:  [ 0.16020035  0.16020035]
a:  [ 1.        -0.6795993]

请注意, b a 与我的参考相反。他们会成为:

a0 = 0.16020035
a1 = 0.16020035
b0 = 1
b1 = -0.6795993

然后,将这些系数应用于我不完整的公式:

a1 = a0 = 0.16020035
b1 = -(1-2*a0) = -{1-2*(0.16020035)} = -(0.6795993)

到目前为止,这么好。 顺便说一下:

k1 = 0.2698
k2 = 0.0364

所以:

a0 = k2/(1+k1+k2) = 0.0364/(1+0.2698+0.0364) = 0.0279

......这远远不是0.16020035。 在这一点上,我淘汰了k2并且这样说:

a0 = k1/(1+k1+x)

当x = 0.4142时,我得到0.16020164。足够接近。

a0 = k1/(1+k1+0.4142) = k1/(1.4142+k1)

...... 1.4142 ......!?我以前见过这个号码......:

= k1/(math.sqrt(2)+k1)

现在情节看起来像这样(flt_y_scipy被flt_y_1st完全覆盖):

k1/(math.sqrt(2)+k1)

您可以搜索关键字“first order”butterworth“low pass filter”“sqrt(2)”等。

这是我周日DIY的结束。 ; - )