Author Topic: FIR Filter code in MATLAB  (Read 3313 times)

Offline vahub

  • Newbie
  • *
  • Posts: 2
  • Matlab Forum
    • View Profile
FIR Filter code in MATLAB
« on: December 14, 2010, 11:12:58 PM »
Hi,

I want to implement FIR filter on AVR microcontroller with C programming language.
I decided test my code in MATLAB and after that implement it on C.(Codevision Compiler)

My code is here. I think it don't work!!!

I generated sine wave with 50Hz and added noise to it.
After that, I designed a simple FIR filter with order=51 and I apply this filter to my signal with ""filter"" function in MATLAB(Build in function).

Next, I applied my algorithm to 51 sample of signal and compared it with filter function output.

Please let me know am I right?

Thanks in Advance.

Vahub



clc
clf
close all

format longG
Fs=1e3;
t=0:1/Fs:1;
f=30;
ts=t(1:51);


signal=sin(2*pi*t*f)+randn(size(t));
x=signal(1:51);

coeff=fir1(51,0.12);
out1=filter(coeff,1,signal);
out2=filter(coeff,1,x);

%plot(t,out1,'r',ts,out2,'g',t,signal,'b')



%coeff=coeff';
%sample=sample';
%save('coeff.txt','coeff','-ascii');
%save('sample.txt','sample','-ascii');


N=51;

a=zeros(1,51);

for i=1:N
xx=0;
for j=1:N
kk=i-j;
if kk<1
kk=kk+N;
end

yy=coeff(j)*x(kk);
xx=yy+xx;
end
zz(i,j)=xx;
yyy(i)=xx;
end




subplot(211)
plot(yyy);
subplot(212);
plot(out2)
figure
plot(ts,yyy,'b',ts,out2,'g',t,out1,'r')

Matlab and SimuLink Development Forum

FIR Filter code in MATLAB
« on: December 14, 2010, 11:12:58 PM »