Hi Farhad,
This code should do what is asked for. Since, you asked for FIR filter, a will be a scalar with value 1.
function y = filterbax(b,a,x)
M = length(b);
N = length(a);
xbuffer = [];
ybuffer = [];
for i = 1:length(x)
xbuffer = [x(i);xbuffer];
if length(xbuffer) < M
index = length(xbuffer);
else
index = M;
end
if length(ybuffer) < N-1
yindex = length(ybuffer);
else
yindex = N-1;
end
y(i) = (1/a(1)).*(sum(b(1:index).*xbuffer(1:index)) - sum(a(2:yindex+1).*ybuffer(1:yindex)));
ybuffer = [y(i);ybuffer];
end
y = y(:);
end
Note that more optimizations can be done, but this should work.
>> b = [2,3];
>> a = [1,0.2];
>> x = randn(10000,1);
>> y = filterbax(b,a,x);
>> y1 = filter(b,a,x);
>> max(abs(y-y1).^2)
Hope this helps.
Regards,
Sriram