FDATool设计FIR低通滤波器

(一) 设计一个低通滤波器

           采样频率 Fs = 50Hz   Fpass = 10Hz   Fstop = 20Hz

打开matlab,键入fdatool后,会弹出该工具的对话框。然后按照下图设置相关参数,参数列表中的Fs应该是采样频率,Fc是截止频率,即幅频特性曲线-3dB对应的频率。点击“Design Filter”后,幅频特性曲线会被更新。

Targets -> generate C Header  生成的头文件

/*
 * Filter Coefficients (C Source) generated by the Filter Design and Analysis Tool
 * Generated by MATLAB(R) 9.0 and the Signal Processing Toolbox 7.2.
 * Generated on: 26-Dec-2018 09:54:34
 */

/*
 * Discrete-Time FIR Filter (real)
 * -------------------------------
 * Filter Structure  : Direct-Form FIR
 * Filter Length     : 6
 * Stable            : Yes
 * Linear Phase      : Yes (Type 2)
 */

/* General type conversion for MATLAB generated C-code  */
#include "tmwtypes.h"
/* 
 * Expected path to tmwtypes.h 
 * C:\Program Files\MATLAB\R2016a\extern\include\tmwtypes.h 
 */
const int BL = 6;
const real64_T B[6] = {
   -0.08631845386171,   0.0581511407323,   0.5053647362323,   0.5053647362323,
     0.0581511407323, -0.08631845386171
};   //Fir 滤波器参数   

(二 )Matlab 脚本测试

clear all;

Ts = 0.02;      %采样时间   
%FDAtool设计的参数  Target -> generate C header 
b = [ -0.08631845386171    0.0581511407323    0.5053647362323    0.5053647362323  0.0581511407323  -0.08631845386171];  %滤波器参数
ss = [0 0 0 0 0 0];
for i = 1:1:100
    time(i) = i * Ts;
    x(i) = 2*sin(10 * pi * time(i)) + cos(40 * pi * time(i)) + 0.1 * rands(1);        %5Hz 20Hz 原信号
    
    %代数方法求滤波后的波形
    ss(6) = ss(5);
    ss(5) = ss(4); 
    ss(4) = ss(3);
    ss(3) = ss(2);
    ss(2) = ss(1);
    ss(1) = x(i);
    zz(i) = b(1) * ss(1) +  b(2) * ss(2) +  b(3) * ss(3) +  b(4) * ss(4) +  b(5) * ss(5) +  b(6) * ss(6);
end

yy = filter(b,1,x);         %使用 filter 函数执行滤波器
figure(1);
plot(time,x,'b',time,yy,'r',time,zz,'y+');
xlabel('秒');

%% 傅里叶变换,左半,检查滤波效果
figure(2)
fy = fft(x);
fyy = fft(yy);
fzz = fft(zz);
i=1:1:100;
plot(i,abs(fy),'y',i,abs(fyy),'g+',i,abs(fzz),'r-');
xlabel('1/2 Hz');
ylabel('幅度乘以采样频率')

 (三)仿真结果

20Hz的信号被过滤掉了

猜你喜欢

转载自blog.csdn.net/Terrys0518/article/details/85258786