Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

FIR Filter code in MATLAB

Status
Not open for further replies.

vahubl

Newbie level 5
Joined
Dec 13, 2010
Messages
9
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Activity points
1,358
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')
 

Hi, the code in matlab seems fine to me. But what is your question exactly? To implement it on the mciroctonroller you must implement all the built in functions of the FIR filter (as matlab does). and i think that to implement a 51 order on a microcontroller will be far alot of processing!

regards
 

Not to mention that your FIR filter may suffer greatly from the finite wordlength (8 and 16 bit might give you proplems)
 

Hi,

At first thanks to your replays.
I want to know why my own code hasn't initial delay on filter. after a period filter works perfect but before that I haven't filter delay.

Please refer to images in attached.

Best Regards,
Vahub
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top