声振论坛

 找回密码
 我要加入

QQ登录

只需一步,快速开始

查看: 751|回复: 1

[综合讨论] 求救

[复制链接]
发表于 2007-4-14 18:13 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?我要加入

x
谁有基于MATLAB的FIR数字滤波器的实例程序啊
回复
分享到:

使用道具 举报

发表于 2007-4-14 20:09 | 显示全部楼层
西安电子第二版的第四个实验

  1. %实验4:用窗函数法设计FIR数字滤波器
  2. clc;
  3. N=input('输入窗函数长度(默认值15,选择范围[10-64]):\nN=','s');

  4. N=str2num(N);
  5. if N<1
  6.     N=15;
  7. else
  8.     if N<10 | N>64
  9.         N=15;
  10.     end
  11. end
  12. w=input('输入截止频率(默认值pi/4,选择范围[0.5-pi]):\nwc=','s');

  13. if w<1
  14.     w=pi/4;
  15. else
  16.     if w<0.5 | w>pi
  17.         w=pi/4;
  18.     end
  19. end
  20. b=1;
  21. close all;

  22. i=0;
  23. while(b)
  24.     n=[0:(N-1)];

  25. %产生理想低通滤波器单位脉冲响应hd(n)
  26. alpha=(N-1)/2;
  27. n=[0:(N-1)];
  28. Q=n-alpha+eps;
  29. hd=sin(w*Q)./(pi*Q);

  30.     k=input('选择窗口类型(默认1):\n[1]矩形窗\n[2]Hamming窗\n[3]Hanning窗\n[4]Blackman\n选择:','s');
  31.     switch(k)
  32.         case{'1','2','3','4'}
  33.             k=str2num(k);
  34.         otherwise
  35.             k=1;
  36.     end
  37.     switch(k)
  38.         case 1
  39.             B=boxcar(N);
  40.             string='Boxcar';
  41.         case 2
  42.             B=hamming(N);
  43.             string='Hamming';
  44.         case 3
  45.             B=hanning(N);
  46.             string='Hanning';
  47.         case 4
  48.             B=blackman(N);
  49.             string='Blackman';
  50.     end
  51.     string=[string,' N=',num2str(N)];
  52.    
  53.     h=hd.*(B)';
  54.     [H,m]=freqz(h,[1],1024,'whole');
  55.     mag=abs(H);
  56.     db=20*log10((mag+eps)/max(mag));
  57.     pha=angle(H);
  58.     i=i+1;
  59.    
  60.     figure(i);
  61.     subplot(2,2,1);
  62.     n=0:N-1;
  63.     stem(n,h,'.');
  64.     axis([0 N-1 -0.1 0.3]);
  65.     hold on;
  66.     n=0:N-1;
  67.     x=zeros(N);
  68.     plot(n,x,'-');
  69.     xlabel('n');
  70.     ylabel('h(n)');
  71.     title('实际低通滤波器的h(n)');
  72.     text((0.3*N),0.27,string);
  73.     hold off;
  74.    
  75.     subplot(2,2,2);
  76.     plot(m/pi,db);
  77.     axis([0 1 -100 0]);
  78.     xlabel('w/pi');
  79.     ylabel('20lg|H(e^j^w)|');
  80.     title('衰减特性/dB');
  81.     grid;
  82.    
  83.     subplot(2,2,3);
  84.     plot(m,pha);
  85.     hold on;
  86.     n=0:7;
  87.     x=zeros(8);
  88.     plot(n,x,'-');
  89.     axis([0 3.15 -4 4]);
  90.     xlabel('w');
  91.     ylabel('相位/rad');
  92.     title('相频特性');
  93.    
  94.     subplot(2,2,4);
  95.     plot(m,mag);
  96.     axis([0 3.15 0 1.5]);
  97.     xlabel('w');
  98.     ylabel('|H(e^j^w)|');
  99.     title('幅频特性');
  100.     text(0.9,1.2,string);
  101.    
  102.     b=input('继续实验吗?(默认1)\n[1]继续\n[0]退出\n选择:','s');
  103.     switch(b)
  104.         case{'0','1'}
  105.             b=str2num(b);
  106.         otherwise
  107.             b=1;
  108.     end
  109.     if b==1
  110.         clc;
  111.         N=input('输入窗函数长度(默认值15,选择范围[10-64]):\nN=','s');
  112.         N=str2num(N);
  113.         if length(N)<1
  114.             N=15;
  115.         else
  116.             if N<10 | N>64
  117.                 N=15;
  118.             end
  119.         end
  120.         w=input('输入截止频率(默认值pi/4,选择范围[0.5-pi]):\nwc=','s');
  121.         w=str2num(w);
  122.         if length(w)<1
  123.             w=pi/4;
  124.         else
  125.             if w<0.5 | w>pi
  126.                 w=pi/4;
  127.             end
  128.         end
  129.     end
  130. end
复制代码

评分

1

查看全部评分

您需要登录后才可以回帖 登录 | 我要加入

本版积分规则

QQ|小黑屋|Archiver|手机版|联系我们|声振论坛

GMT+8, 2024-9-24 15:27 , Processed in 0.055498 second(s), 18 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表