### 文章目录

• 1 预备知识
• 4 matlab实现
• 5 matlab运行结果
• 6 C语言实现
• 7 C语言运行结果

HPF 一阶RC高通滤波器详解（仿真+matlab+C语言实现）LPF 一阶RC低通滤波器详解（仿真+matlab+C语言实现）

## 1 预备知识

`Subsystem`

`Gain Value``0.005`

`Gain Value``0.0318`

## 4 matlab实现

``````Serial = 0:0.1:100;
Fs = 1;
Phase = 0;
Amp = 1;

% 高频信号
N0 = 2*pi*Fs*Serial - Phase;
X0 = Amp*sin(N);
subplot(4,1,1);
plot(X0);

% 低频信号
Fs = 0.02;
N1 = 2*pi*Fs*Serial - Phase;
X1 = Amp*sin(N1);
subplot(4,1,2);
plot(X1);

% 高频低频叠加的信号
X2=X0+X1;
subplot(4,1,3);
plot(X2);

%Xi-Yi=RC*(Yi - Yi-1)/DetalT
len = length(X2);
X3=X2;
p=0.05;

% 一阶RC滤波得到X3
for i=2:len
X3(i) = p*X2(i)+(1-p)*X3(i-1);
end

subplot(4,1,4);
plot(X3);``````

## 6 C语言实现

`low_filter.h`

``````typedef struct
{
int16_t  Input;
int16_t  Output[2];
int32_t  FilterTf;
int32_t  FilterTs;
int32_t  Kr;
int32_t  Ky;

} low_filter;

void low_filter_init(low_filter *v);
int16_t low_filter_calc(low_filter *v);``````

• `FilterTs`为采样时间；
• `FilterTf``RC`时间常数

`low_filter.c`

``````void low_filter_init(low_filter *v){

v->Kr = v->FilterTs*1024/(v->FilterTs + v->FilterTf);
v->Ky = v->FilterTf*1024/(v->FilterTs + v->FilterTf);
}

int16_t low_filter_calc(low_filter *v){

int32_t tmp = 0;

tmp = ((int32_t)v->Kr*v->Input + v->Ky*v->Output[1])/1024;

if(tmp>32767){
tmp = 32767;
}

if( tmp < -32768){
tmp = -32768;
}

v->Output[0] = (int16_t)tmp;
v->Output[1] = v->Output[0];
return v->Output[0];
}``````