声振论坛

 找回密码
 我要加入

QQ登录

只需一步,快速开始

查看: 2944|回复: 1

[Fortran] 用频率抽样法设计FIR滤波器

[复制链接]
发表于 2006-8-8 07:14 | 显示全部楼层 |阅读模式

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

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

x
主程序一

  1. C----------------------------------------------------------------------
  2. c  main program H1DEFIR2: to test subroutine DEFIR2
  3. C  To design FIR low-pass filter by frequency sampling method
  4. c  Please link subroutine DEFIR2,FIRRES,AMPRES,PHARES,UNWRAP
  5. C----------------------------------------------------------------------
  6.         dimension b(0:30),amp(0:255),phase(0:255)
  7.         complex h(0:255),bcmplx(0:30)
  8.         data l/30/,n/256/,iband/1/,iamp/0/
  9.         data fs/1.0/,fl/0.1/,trans/0.5/
  10.         call defir2(l,iband,fl,fh,bcmplx,trans,fs,ierror)
  11.         write(*,*)'     ierror=',ierror
  12.         if(ierror.ne.0)stop
  13.         do 10 i=0,l
  14.            b(i)=real(bcmplx(i))
  15.            write(*,*)i,b(i)
  16. 10      continue
  17.         call firres(b,l,n,h)
  18.         call ampres(h,amp,n,fs,iamp)
  19.         call phares(h,phase,n,fs)
  20.         stop
  21.         end
复制代码


主程序二
  1. C----------------------------------------------------------------------
  2. c  main program H4DEFIR2: to test subroutine DEFIR2
  3. C  To design FIR band-stop filter by frequency sampling method
  4. c  Please link subroutine DEFIR2,FIRRES,AMPRES,PHARES,UNWRAP
  5. C----------------------------------------------------------------------
  6.         dimension b(0:40),amp(0:255),phase(0:255)
  7.         complex h(0:255),bcmplx(0:40)
  8.         data l/40/,n/256/,iband/4/,iamp/0/
  9.         data fs/1.0/,fl/0.2/,fh/0.3/,trans/0.5/
  10.         call defir2(l,iband,fl,fh,bcmplx,trans,fs,ierror)
  11.         write(*,*)'    ierror=',ierror
  12.         if(ierror.ne.0)stop
  13.         do 10 i=0,l
  14.            b(i)=real(bcmplx(i))
  15.            write(*,*)i,b(i)
  16. 10      continue
  17.         open(3,file='h4hdfir2.dat',status='new')
  18.         do 20 k=0,l
  19.            write(3,*)k,b(k)
  20. 20      CONTINUE
  21.         close(3)
  22.         call firres(b,l,n,h)
  23.         call ampres(h,amp,n,fs,iamp)
  24.         call phares(h,phase,n,fs)
  25.         stop
  26.         end
复制代码


子程序:
  1.       subroutine defir2(l,iband,fl,fh,b,trans,fs,ierror)
  2. C-----------------------------------------------------------------------
  3. C  Routine DEFIR2: To design FIR filter by frequency sampling method.
  4. C  Fl:low cut-off frequency. Fh:high cut-off(For BP,BS). fl,fh,fs in Hz
  5. c          |---        |   ---      |   ---           |--      --
  6. c          |   |       |  |         |  |   |          |  |    |
  7. c          |   |       |  |         |  |   |          |  |    |
  8. c        --|------    -|--------   -|-----------    --|--------------
  9. c          0   fl      0  fl       0   fl  fh       0    fl   fh
  10. C
  11. C   Digital filter coefficients are returned in b(l)
  12. c                H(z)=b(0)+b(1)z^(-1)+ ... +b(L)z^(-L)
  13. c  Input parameters:
  14. c   L+1  : the length of FIR filter. L<200 and (L+1) must be the odd.
  15. c   iband:  iband=1 low  pass FIR filter.
  16. c           iband=2 high pass FIR filter.
  17. c           iband=3 band pass FIR filter.
  18. c           iband=4 band stop FIR filter.
  19. c   trans:   0<= trans <1.0 , it is the transition point.
  20. c  Output parameters:
  21. c   b: (L+1) dimensioned real array.the result is in b(0) to b(L).
  22. c
  23. c                                      in Chapter 8
  24. c-----------------------------------------------------------------------
  25.         complex b(0:l),h(0:200)
  26.         npass=0
  27.         pi=4.*atan(1.)
  28.         fln=fl/fs
  29.         fhn=fh/fs
  30.         do 5 i=0,l
  31.            h(i)=0.
  32. 5       continue
  33.         ierror=0
  34.         l=l+1
  35.         dly=float(l)/2.
  36.         lim=l/2
  37.         if(dly.eq.float(lim)) ierror=1
  38.         if(l.ge.201) ierror=2
  39.         if(iband.ne.4) goto 6
  40.         band=(fhn-fln)*l
  41.         if(band.ge.4.) goto 6
  42.         write(*,*)'  Please increse the length L for Band-Stop FILTER'
  43.         ierror=3
  44. 6       if(iband.lt.1.or.iband.gt.4) ierror=4
  45.         if(fln.le.0..or.fln.gt.0.5) ierror=5
  46.         if(iband.ge.3.and.fln.ge.fhn) ierror=6
  47.         if(trans.lt.0.and.trans.ge.1.)ierror=7
  48.         if(ierror.ne.0) return
  49.         s=-(l-1)*pi/l
  50.         go to(10,20,30,10) iband
  51. 10      nlow=1
  52.         nhigh=fln*l-1
  53.         h(0)=1.0
  54.         do 15 i=nlow,nhigh
  55.            h(i)=cexp(cmplx(0.0,s*i))
  56.            h(l-i)=conjg(h(i))
  57. 15      continue
  58.         h(nhigh+1)=trans*cexp(cmplx(0.0,s*(nhigh+1)))
  59.         h(l-nhigh-1)=conjg(h(nhigh+1))
  60.         if(iband.eq.1)goto 100
  61.         nlow=fhn*l
  62.         goto 21
  63. c
  64. 20      h(0)=0.0
  65.         nlow=fln*l
  66. 21      nhigh=lim
  67.         do 25 i=nlow,nhigh
  68.            h(i)=cexp(cmplx(0.0,s*i))
  69.            h(l-i)=conjg(h(i))
  70. 25      continue
  71.            h(nlow-1)=trans*cexp(cmplx(0.0,s*(nlow-1)))
  72.            h(l-nlow+1)=conjg(h(nlow-1))
  73.         goto 100
  74. c
  75. 30      nlow=fln*l
  76.         nhigh=fhn*l
  77.         h(0)=0.0
  78.         do 35 i=nlow,nhigh
  79.            h(i)=cexp(cmplx(0.0,s*i))
  80.            h(l-i)=conjg(h(i))
  81. 35      continue
  82.         h(nhigh+1)=trans*cexp(cmplx(0.0,s*(nhigh+1)))
  83.         h(l-nhigh-1)=conjg(h(nhigh+1))
  84.         h(nlow-1)=trans*cexp(cmplx(0.0,s*(nlow-1)))
  85.         h(l-nlow+1)=conjg(h(nlow-1))
  86. c
  87. 100     call cmpdft(h,b,l,1)
  88.         l=l-1
  89.         return
  90.         end
复制代码

[ 本帖最后由 VibInfo 于 2006-8-8 07:17 编辑 ]
回复
分享到:

使用道具 举报

发表于 2006-8-22 19:56 | 显示全部楼层
谢谢
您需要登录后才可以回帖 登录 | 我要加入

本版积分规则

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

GMT+8, 2024-9-21 02:47 , Processed in 0.052941 second(s), 18 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

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