本帖最后由 hanl 于 2011-7-21 10:07 编辑
我用C语言实现一个butterworth低通滤波器,现在遇到问题,please help me!
实现过程:
1、采样频率10KHz,通带边界频率2KHz,通带波纹小于1dB,阻带边界频率3KHz,阻带衰减大于40dB。
2、用
matlab求出滤波器系数b,a
- >> Fp = 2000;
- >> Fs = 3000;
- >> F = 10000;
- >> T = 1/F;
- >> Ap = 1;
- >> As = 40;
- >> Wp = Fp*T*2;
- >> Ws = Fs*T*2;
- >> [n,Wc] = buttord(Wp,Ws,Ap,As);
- >> [b,a] = butter(n,Wc)
- b =
- 0.0021 0.0186 0.0745 0.1739 0.2609 0.2609 0.1739 0.0745 0.0186 0.0021
- a =
- 1.0000 -1.0893 1.6925 -1.0804 0.7329 -0.2722 0.0916 -0.0174 0.0024 -0.0001
- >>
复制代码3、差分方程: y(0) = b(0)*x(0) + b(1)*x(1) +...+ b(9)*x(9) - {a(1)*y(1) + a(2)*y(2) +...+ a(9)*y(9)}
4、程序:
- double a[L] = {1.0000, -1.0893, 1.6925, -1.0804, 0.7329, -0.2722, 0.0916, -0.0174, 0.0024, -0.0001};
- double b[L] = {0.0021, 0.0186, 0.0745, 0.1739, 0.2609, 0.2609, 0.1739, 0.0745, 0.0186, 0.0021};
- read(&input); //输入范围:-10V~+10V ,量化范围 0~65535
- x[0] = input-32767;
- for (i=0; i<=L-1; i++)
- {
- b_sum = b[i]*x[i] + b_sum;
- a_sum = a[i+1]*y[i+1] + a_sum;
- }
- y[0] = b_sum - a_sum;
- output = y[0];
- for (i=L-1; i>=1; i--)
- {
- x[i] = x[i-1];
- y[i] = y[i-1];
- }
- write(output);
复制代码5、测试结果
a、无输入
(单位:0.1ms)
b、输入1KHz正弦波,峰峰值 4V
(单位:0.1ms)
到此,不知道哪里出了问题,请高人赐教
H(z) = ----------------------
1 - 0.8*z^-1
1. b = 0.15 ;
2. a = [1 -0.8];
3.
4. n = [0:100];
5. x = 2*sin(0.05*pi*n) + 2*randn(1, 101); %滤波前序列
6.
7. imp = [1; zeros(100, 1)];
8. h = filter(b, a, imp); % h为冲激函数的响应(时域)
9. w = conv(x, h); % 做卷积
10. yc = w(1:101); %滤波后序列
11. z= filter(b, a, x); % 进行滤波
12. xlabel('n');
13. ylabel('x y yc');
14. plot(n, x,'r',n,z ,'b', n,yc,'m');
15. grid;
思想很简单,首先是用冲击函数 Imp = [1 ; zeros(100,1)] 取求出系统的冲激响应,然后再与输入进行卷积
运算结果:
一周热门 更多>