For those wondering where those magical formulas from the other answers come from, here's a derivation following this example.
Starting with the transfer function for the Butterworth filter
G(s) = wc^2 / (s^2 + s*sqrt(2)*wc + wc^2)
where wc
is the cutoff frequency, apply the bilinear z-transform, i.e. substitute s = 2/T*(1-z^-1)/(1+z^-1)
:
G(z) = wc^2 / ((2/T*(1-z^-1)/(1+z^-1))^2 + (2/T*(1-z^-1)/(1+z^-1))*sqrt(2)*wc + wc^2)
T
is the sampling period [s].
The cutoff frequency needs to be pre-warped to compensate for the nonlinear
relation between analog and digital frequency introduced by the z-transform:
wc = 2/T * tan(wd*T/2)
where wd
is the desired cutoff frequency [rad/s].
Let C = tan(wd*T/2)
, for convenience, so that wc = 2/T*C
.
Substituting this into the equation, the 2/T
factors drop out:
G(z) = C^2 / ((1-z^-1)/(1+z^-1))^2 + (1-z^-1)/(1+z^-1)*sqrt(2)*C + C^2)
Multiply the numerator and denominator by (1+z^-1)^2
and expand, which yields:
G(z) = C^2*(1 + 2*z^-1 + z^-2) / (1 + sqrt(2)*C + C^2 + 2*(C^2-1)*z^-1 + (1-sqrt(2)*C+C^2)*z^-2')
Now, divide both numerator and denominator by the constant term from the denominator. For convenience, let D = 1 + sqrt(2)*C + C^2
:
G(z) = C^2/D*(1 + 2*z^-1 + z^-2) / (1 + 2*(C^2-1)/D*z^-1 + (1-sqrt(2)*C+C^2)/D*z^-2')
This form is equivalent to the one we are looking for:
G(z) = (b0 + b1*z^-1 + b2*z^-1) / (1 + a1*z^-1 +a2*z^-2)
So we get the coefficients by equating them:
a0 = 1
a1 = 2*(C^2-1)/D
a2 = (1-sqrt(2)*C+C^2)/D
b0 = C^2/D
b1 = 2*b0
b2 = b0
where, again, D = 1 + sqrt(2)*C + C^2
, C = tan(wd*T/2)
, wd
is the desired cutoff frequency [rad/s], T
is the sampling period [s].