C语言编写FIR数字低通滤波器

C/C++ 专栏收录该内容
1 篇文章 0 订阅

主要是获取滤波器参数和卷积算法,参数根据自己的滤波器特性用matlab上面的一个fdatool工具配置生成,然后将这些参数写入程序里面即可(参数可适当的乘以一个系数,对滤波没任何影响),我这里Fs = 2k,Fpass= 1k,Fstop = 2k,至于输入数据你可以自己产生几个不同频率正弦波的叠加。我这里随便给的一个输入例子:

f1=1k,f2=6k,fs=20k
采样数据:200
30 12 -3 5 -2 -10 9 11 -14 -18 -10 -19 -12 13 7 -10 -1 4 -3 15 30 10 -3 5 -4 -9 11 9 -16 -17 -10 -20 -9 14 4 -11 1 3 -3 17 29 7 -2 5 -5 -8 12 7 -17 -16 -11 -20 -7 15 2 -10 2 2 -2 20 28 5 -2 5 -7 -6 14 4 -19 -15 -12 -20 -4 15 0 -10 3 1 -1 22 26 3 -1 5 -8 -4 14 2 -20 -13 -12 -20 -1 15 -2 -9 4 0 1 25 25 1 0 4 -9 -2 15 -1 -20 -12 -13 -20 2 14 -4 -8 5 -1 3 26 22 -1 1 3 -10 0 15 -4 -20 -12 -15 -19 4 14 -6 -7 5 -2 5 28 20 -2 2 2 -10 2 15 -7 -20 -11 -16 -17 7 12 -8 -5 5 -2 7 29 17 -3 3 1 -11 4 14 -9 -20 -10 -17 -16 9 11 -9 -4 5 -3 10 30 15 -3 4 -1 -10 7 13 -12 -19 -10 -18 -14 11 9 -10 -2 5 -3 12 30
39阶参数:
1 1 0 -2 -5 -9 -13 -18 -22 -23 -21 -13 0 17 39 64 88 109 124 133 133 124 109 88 64 39 17 0 -13 -21 -23 -22 -18 -13 -9 -5 -2 0 1 1


#include <stdlib.h>
#include <stdio.h>


#define samples 200
#define taps 40
int input[samples] = {30,12,-3,5,-2,-10,9,11,-14,-18,-10,-19,-12,13,7,-10,-1,4,-3,15,30,10,-3,5,-4,-9,11,9,-16,-17,-10,-20,-9,14,4,-11,1,3,-3,17,29,7,-2,5,-5,-8,12,7,-17,-16,-11,-20,-7,15,2,-10,2,2,-2,20,28,5,-2,5,-7,-6,14,4,-19,-15,-12,-20,-4,15,0,-10,3,1,-1,22,26,3,-1,5,-8,-4,14,2,-20,-13,-12,-20,-1,15,-2,-9,4,0,1,25,25,1,0,4,-9,-2,15,-1,-20,-12,-13,-20,2,14,-4,-8,5,-1,3,26,22,-1,1,3,-10,0,15,-4,-20,-12,-15,-19,4,14,-6,-7,5,-2,5,28,20,-2,2,2,-10,2,15,-7,-20,-11,-16,-17,7,12,-8,-5,5,-2,7,29,17,-3,3,1,-11,4,14,-9,-20,-10,-17,-16,9,11,-9,-4,5,-3,10,30,15,-3,4,-1,-10,7,13,-12,-19,-10,-18,-14,11,9,-10,-2,5,-3,12,30};
int output[samples];
int coefficients[taps] = {1,1,0,-2,-5,-9,-13,-18,-22,-23,-21,-13,0,17,39,64,88,109,124,133,133,124,109,88,64,39,17,0,-13,-21,-23,-22,-18,-13,-9,-5,-2,0,1,1};
int state[taps+1]={0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
main()
{    
   int temp;   
   int i; 
   int j;
   int k;
   for (k = 0; k < samples; k++)
   {
      state[0] = input[k];


 for (i = 0, temp = 0; i < taps; i++)
         temp += coefficients[i] * state[i];
         output[k] = temp;
      for (j = taps-1; j > -1 ; j--)
         state[j+1] = state[j];
   }
   for( i = 0; i<200;i++){
printf("%d,",&output[i]);
   }
    exit(0);
}

下面使用matlab仿真的结果

fs =20000;
f1 = 1000;
f2 = 3000;
f3 = 6000;
 
T=10*10^(-3);
t=linspace(0,T,fs*T);
y =round(10*cos(2*pi*f1*t)+10*cos(2*pi*f2.*t)+10*cos(2*pi*f3.*t));
lpf=[1 1 0 -2 -5 -9 -13 -18 -22 -23 -21 -13 0 17 39 64 88 109 124 133 133 124 109 88 64 39 17 0 -13 -21 -23 -22 -18 -13 -9 -5 -2 0 1 1]
 
y1 =conv(y,lpf);
 
 
figure;
plot(y);
figure;
plot(y1);


  • 4
    点赞
  • 7
    评论
  • 13
    收藏
  • 一键三连
    一键三连
  • 扫一扫,分享海报

©️2020 CSDN 皮肤主题: 技术黑板 设计师:CSDN官方博客 返回首页
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、C币套餐、付费专栏及课程。

余额充值