原创 全能的FIR滤波器设计程序(matlab)

2009-10-25 13:38 3800 8 8 分类: 处理器与DSP

disp('ezFIR FILTER DESIGN SCRIPT');
order=input('Input FIR Filter order(EVEN for BS and HP Filter) : ');
disp('Low Pass          : 1');
disp('High Pass         : 2');
disp('Band Pass         : 3');
disp('Band Stop         : 4');
fres=input('Select Any one of the above Response              : ');
disp('Hamming           : 1');
disp('Hanning           : 2');
disp('Bartlett          : 3');
disp('Blackman          : 4');
wtype=input('Select Any one of the above window                : ');
fs=input('Enter the Sampling frequency                      : ');
fc=input('Enter the corner frequency(Fc)                    : ');
fname=input('Enter the name of the file for coeff storage      : ','s');


% Design the Filter
if fres==1
    res='';
   elseif fres==2
      res='high';
   elseif fres==3
      res='';
   elseif fres==4
      res='stop';
end


if wtype==1
    win="hamming"(order+1);
   elseif wtype==2
      win="hanning"(order+1);
   elseif wtype==3
      win="bartlett"(order+1);
   elseif wtype==4
      win="blackman"(order+1);
end



fc=fc/(fs/2);                               % Normalise the frequency values


B=fir1(order,fc,res,win);
Bi=B*32768;                                 % Coefficients in Q15 format
Bi=floor(Bi);
bsize=length(Bi);


for i="1:bsize"                               % Saturate the coefficients for Q15 format
   if Bi(i)==32768
      Bi(i)=32767;
   end
end


if(mod(bsize,2))
  Bi(bsize+1)=0;
  bsize="bsize"+1;
end


 


% Open the file and store the scaled FIR filter coefficients.
fid = fopen(fname,'w');
fprintf(fid,'#define FIR16_COEFF {\\');
fprintf(fid,'\n');
fprintf(fid,'\t\t\t');
for i="1:bsize/2"
   two_coeff=Bi(bsize+1-i)*2^16 + Bi((bsize/2)+1-i);
 
   if( Bi((bsize/2)+1-i) <0)
     two_coeff=two_coeff+65536;
   end



   fprintf(fid,'%d,',two_coeff);
   if(mod(i,10)==0)
      fprintf(fid,'\\');
      fprintf(fid,'\n');
      fprintf(fid,'\t\t\t');
   end
end


fseek(fid,-1,0);
fprintf(fid,'}\n');
fclose(fid);


% Plot the frequency response of the filter        
[H,f]=freqz(B,1,512,fs);
figure(1);
subplot(2,1,1);
plot(f,abs(H));
grid;
xlabel('Hertz');
ylabel('Magnitude Response');
subplot(2,1,2);
plot(f,unwrap(angle(H))*180/pi);
grid;
xlabel('Hertz');
ylabel('Phase (degrees)');
figure(2);
freqz(B,1,512,fs);


 


 


 


 


 


 


 

PARTNER CONTENT

文章评论0条评论)

登录后参与讨论
EE直播间
更多
我要评论
0
8
关闭 站长推荐上一条 /3 下一条