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

2009-10-25 13:38 3446 4 4 分类: 处理器与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);


 


 


 


 


 


 


 

文章评论0条评论)

登录后参与讨论
相关推荐阅读
adofu2008_922131534 2009-10-25 13:38
全能的IIR滤波器设计程序(matlab)
Ni=500;             % Number of impulses required for Impulse responseFMAT=16;            % 16 bit r...
adofu2008_922131534 2009-10-22 09:34
FIR与IIR的区别与联系及实现方法
本篇文章纯属个人对数字信号的粗浅理解,如有不对的地方,还望高手指点。FIR:有限脉冲响应滤波器。有限说明其脉冲响应是有限的。与IIR相比,它具有线性相位、容易设计的优点。这也就说明,IIR滤波器具有相...
adofu2008_922131534 2009-10-16 12:36
DSP学习
半年时间学会DSP!!!...
我要评论
0
4
1
2
3
4
5
6
7
8
9
0
关闭 热点推荐上一条 /4 下一条