⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ezfir16.m

📁 FIR滤波器系数生成
💻 M
字号:
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);
%B=fir1(order,fc,'dc-0');
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);













⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -