相关文章推荐
不拘小节的排球  ·  IllegalStateException: ...·  5 月前    · 

如果本文帮到了你,帮忙点个赞;
如果本文帮到了你,帮忙点个赞;
如果本文帮到了你,帮忙点个赞;

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

1 预备知识

低通滤波器( LPF )可以滤除频率高于截止频率的信号,类似的还有高通滤波器,带通滤波器,带阻滤波器。一阶RC低通滤波器的电路如下图所示;

参考了Wiki了,然后推导了一遍;首先输入输出的关系如下;
V i n ( t ) V o u t ( t ) = R i ( t ) V_{in} (t)- V_{out}(t) = Ri(t)

这里直接根据公式③构建一搞 Subsystem
V i n ( t ) V o u t ( t ) = R C d V o u t d t V_{in} (t)- V_{out}(t) = RC\cfrac{dV_{out}}{dt}

3 simulink 运行结果

最终的仿真的运行结果如下图所示;
Gain Value 0.005
Gain Value 0.0318

4 matlab实现

根据公式 y i = α x i + ( 1 α ) y i 1 y_{i} = \alpha*x_{i} + (1-\alpha)*y_{i-1}

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 ) ; subplot ( 4 , 1 , 4 ) ; plot ( X3 ) ;

5 matlab运行结果

运行结果如下所示;

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为采样时间;
  • FilterTfRC时间常数
  • 具体参考下图;
    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];
    

    7 C语言运行结果

    实际测试结果

    2 simulink 仿真