# How to design aFIR filter without filter function

22 views (last 30 days)
farhad vaseghi on 13 Mar 2020
Commented: farhad vaseghi on 13 Mar 2020 farhad vaseghi on 13 Mar 2020
I wanna impeliment this whith a seprate function
and without,without any use of filter() function!!!!!!!!!!!

Sriram Tadavarty on 13 Mar 2020
Edited: Sriram Tadavarty on 13 Mar 2020
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)
% The function expects inputs to be column vectors
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(:); % Return as column vector
end
Note that more optimizations can be done, but this should work.
% In command prompt,
% try the following
>> b = [2,3];
>> a = [1,0.2];
>> x = randn(10000,1);
>> y = filterbax(b,a,x); % Custom function
% Compare the output from custom written function and MATLAB function
>> y1 = filter(b,a,x); % MATLAB function
>> max(abs(y-y1).^2)
Hope this helps.
Regards,
Sriram
farhad vaseghi on 13 Mar 2020
Tanks Sriram
you helped me this day i wish god bless you
tanks bro