C語言編寫FIR數字低通濾波器
主要是獲取濾波器引數和卷積演算法,引數根據自己的濾波器特性用matlab上面的一個fdatool工具配置生成,然後將這些引數寫入程式裡面即可(引數可適當的乘以一個係數,對濾波沒任何影響),我這裡Fs = 2k,Fpass= 1k,Fstop = 2k,至於輸入資料你可以自己產生幾個不同頻率正弦波的疊加。我這裡隨便給的一個輸入例子:
f1=1k,f2=6k,fs=20k
取樣資料:200
3012-35-2
-10911-14-18-10-19-1213
7-10-14-3
153010-35
-4-9119-16
-17-10
-20-9
144-1113
-317297-2
5-5-8127
-17-16
-11-20
-7152-102
2-220285
-25-7-614
4-19-15-12-20-415
0-1031-1
22263-15
-8-4142-20
-13-12
-20-1
15-2-940
1252510
4-9-215-1
-20-12
-13-20
214-4-85
-132622-1
13-10015
-4-20
-12-15
-19414-6-7
5-252820
-222-102
15-7-20-11-16-177
12-8-55-2
72917-33
1-11414-9
-20-10
-17-16
911-9-45
-3103015-3
4-1-10713
-12-19
-10-18
-1411
9-10-25-3
1230
39階引數:
110-2-5
-9-13
-18-22
-23-21
-130173964
88109
124133
133124
10988
6439170-13
-21-23
-22-18
-13-9
-5-2011
#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=[11
0-2-5-9-13
-18-22
-23-21
-130173964
88109
124133
133124
10988
6439170-13
-21-23
-22-18
-13-9
-5-2011]
y1 =conv(y,lpf);
figure;
plot(y);
figure;
plot(y1);