Using the butter function to create the coefficients in a low pass filter
11 views (last 30 days)
Show older comments
When using the butter function, I use the following code to obtain the coefficients required for a zero lag 2nd order low pass filter (to enter in the filtfilt function) where f_cut is the desired cutoff frequency and fs is the sample frequency of the data:
[b_filter,a_filter] = butter(2,2*f_cut/fs);
This outputs 2 vectors that are both 1x3 in size. However, I would have expected for a second order low pass filter that there were 2 entries for the b coefficients (b0 and b1) and 3 entries for the a coefficients (a0, a1 and a2) according to the following butterworth filter equation:
y(k) = a0*x(k) + a1*x(k-1) + a2*x(k-2) + b1*y(k-1) + b2*y(k-2)
where the coefficients for a butterworth filter are calculated by:
wc = tan((PI*f_cut)/fs);
k1 = sqrt(2)*wc;
k2 = wc*wc;
a0 = k2/(1 + k1 + k2);
a1 = 2*a0;
a2 = a0;
k3 = (2*a0)/k2;
b0 = -2*a0 + k3;
b1 = 1 - (2*a0) - k3;
However when using 1.2Hz as f_cut and 500Hz as fs for both equations, I get the the following output from the butter function:
a_filter = [1 -1.97867495733125 0.978899949722877]
b_filter = [5.62480979075797e-05 0.000112496195815159 5.62480979075797e-05]
and the following output from my function:
my_a_filter = [5.62480979079425e-05 0.000112496195815885 5.62480979079425e-05]
my_b_filter = [1.97867495733118 -0.978899949722809]
Can anyone please explain why the values are the same (or almost the same), but the butter function's a and b coefficients are swapped around and there is an extra entry equal to 1 in the a coefficient vector? Also the signs are reversed on the values in my_b_filter. This might be a misunderstanding in my knowledge of low pass filters, but any help would be much appreciated!
Thank you.
0 Comments
Answers (0)
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!