pythonfilterbutterworth

Convert 2nd order Butterworth to 1st order


I'm trying to convert my working 2nd-order Butterworth low pass filter to 1st-order in python, but it gives me very big numbers. Here's my 2nd-order Butterworth:

import math
import numpy as np

def bw_2nd(x=100, y, fc=200, fs=1000):
    filtered_y = np.zeros(len(x))

    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(x)):
        filtered_y[i] = np.int(a0*y[i]+a1*y[i-1]+a2*y[i-2]-(b1*filtered_y[i-1]+b2*filtered_y[i-2]))

    return filtered_y

Here's my non-working 1st-order Butterworth:

def bw_1st(x=100, y, fc=200, fs=1000):
    filtered_y = np.zeros(len(x))

    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)

    filtered_y[0] = y[0]

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

    return filtered_y

... I removed all [i-2]s, which were a feed-forward and a feed-back. I thought it would work, but it doesn't. Could you tell me how to fix this? Thank you.


Solution

  • Both should be doing fine if you remove the np.int(...). I get no problems when calling it (after removing that).