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

📄 带通fir.cpp

📁 数个关于滤波器的产生的C程序
💻 CPP
字号:


 #include <math.h>
 #include <stdio.h>
 #define M 67
 #define pi 3.141592653589793
   /////////////////////////////////////////////////////
   // w1,w2为通带的下上3dB截频;n为滤波器的阶数        //
   // n返回滤波器的n+1个抽头系数                      //
   // type整型变量标志位用于确定所选的窗函数          //
   //   type: 1:   巴特利特(Bartlett)窗               //
   //         2:   汉宁(Hanning)窗                    //
   //         3:   哈明(Hamming)窗                    //
   //         4:   布莱克曼(Blackman)窗               //
   //         其他:矩形窗                             //
   // 函数返回0为正常返回-1为异常返回异常原因        //
   //    0<w1<w2<1 不满足;1对应于采样频率的一般       //
   /////////////////////////////////////////////////////
 int fir2(float w1,float w2,int n,float b[],int type)
 { int i;
   float alpha,m;

   if(w1>=w2||w2>=1||w1<=0)
    { printf("\n  w1 and w2 must be as follow:");
      printf("\n     0<=w1<w2<1\n");
      return(-1);
      }
   alpha=n/2.0;
   for(i=0; i<=(n+1)/2; i++)
    { //m=pi*(i-alpha);
      //if(m==0) b[i]=w2-w1;
      //else b[i]=(sin(w2*m)-sin(w1*m))/m;
      m=pi*(i-alpha+1e-20);
      b[i]=(sin(w2*m)-sin(w1*m))/m;
      }
   alpha=pi/alpha;
   switch(type)
     { case 1 : for(i=0; i<=(n+1)/2; i++)
                  if(i<=n/2) b[i]*=i*alpha/pi;
                  else       b[i]*=2-i*alpha/pi;
                break;
       case 2 : for(i=0; i<=(n+1)/2; i++)
                  b[i]*=0.5*(1-cos(i*alpha));
                break;
       case 3 : for(i=0; i<=(n+1)/2; i++)
                  b[i]*=0.54-0.46*cos(i*alpha);
                break;
       case 4 : for(i=0; i<=(n+1)/2; i++)
                  b[i]*=0.42-0.50*cos(i*alpha)
                            +0.08*cos(2*i*alpha);
                break;
       default: {   };
       }
   for(i=(n+1)/2+1; i<=n; i++)
     b[i]=b[n-i];
   return(0);
   }


   void main( )
   { float b[M+1],w1,w2;
     int rtn,i;

     w1=0.25/pi;
     w2=0.5/pi;
     rtn=fir2(w1,w2,M,b,3);
     if(rtn==0)
       for(i=0; i<=M; i++)
         printf("%.4f\t",b[i]);
         
     scanf("%f",&w1);
     }

⌨️ 快捷键说明

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