# FIR filter implementation to ECG

7 views (last 30 days)
John on 14 Nov 2013
Edited: Patrik Ek on 10 Dec 2013
I am trying to write a Matlab function that implements an FIR filter with impulse response in the form value myFIR(value, coefficients),where both the value argument adn return value are scalars
Does anyone understand the function output = myFIR(input,hn)? Can explain it to me?

Patrik Ek on 14 Nov 2013
Hi,
If I have understood correctly you have a FIR filter which you want to implement on a signal input in matlab. MATLAB have already a function that can do that. It is called filter and the syntax is
y = filter(b,a,x)
b is a vector with the coefficients of the nominator in an IIR filter and a is the denominator. The filter is a direct form II transpose type IIR filter. If you want you can create a filter object using dfilt to create an IIR filter of another kind. Then implement it as
y = filter(h,x)
h is the filter object. This should not be necessary for you though since your filter is a FIR type. The first coefficient in a need to be set to one. Otherwise MATLAB normalizes it. For you a should be a scalar with value 1.
BR/ Patrik
##### 2 CommentsShowHide 1 older comment
Patrik Ek on 9 Dec 2013
Sorry for the late answer, you have most likely already got an answer by now. Regarding your question, A FIR filter is only a polynom. There are no real shortcuts for that. A tip is to find the zeros. They amplification is high far from the zeros and low close to the zeros.Regarding the implementation it is just basic matlab. The FIR filter has the form:
y(t) = a0*x(t)+a1*x(t-1)+a2*x(t-2)+...+an*x(t-n)
where each where t is the time sample vector. So simply, create a time vector t = 1:m, where m => n should apply. By calling (pseudo code):
t = 1:m+n;
x = randn(m,1); % Or rather your input.
y = a0*x(t)+a1*x(t-1)+a2*x(t-2)+...+x(t-n);
The only things needed to be adjusted by you is the input (x values and m), the length of the filter (n) and the coefficients a0,a1,...,an.
Especcially, try
t = 3:1000;
x = randn(1000,1); % Or rather your input.
y = x(t)-0.99*2*cos(pi/4)*x(t-1)+0.99^2*x(t-2);
w = fft(y);
figure;
plot(abs(w(1:length(w)/2)));
which you can see is a simple band-stop filter. Notice though that the first two elements are removed from t and that last elements of t is removed. These valuse should be there as well for a proper FIR filter implementation, which require a special treatment of these values to avoid a crash. If you try to plot the complete w vector you will see the aliasing effects as well.