I've been playing with Second-Order Butterworth high-pass filter for a while.
I've calculated the coefficients using Matlab function and I wanted to use them to filter simple sine wave generated by Matlab.
Unfortunately the results are not correct/consistent, verified by Matlab filter(b,a, signal)
function.
My Matlab code is as following (ommiting plotting part):
clear all;
fp = 10000; %sampling frequency
ts = 0:1/fp:0.05; %number of samples, length
squareF=100; %frequency of signal
squareWave = sin(2*pi*squareF*ts); %generate the sine
fc = 500;
[b,a] = butter(2 ,fc/(fp/2), 'high'); %numenator, denumenator
for n = 3:501
filteredWave(n) = (b(1)+b(2)*squareWave(n-1)+b(3)*squareWave(n-2))/(a(1)+a(2)*squareWave(n-1)+a(3)*squareWave(n-2))*squareWave(n);
%Rational Transfer Funcion
end
filteredCorrectly = filter(b, a, squareWave);
Filter plots are below.
Where is the error?