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

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

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

首先对电容的几个公式做一下补充;
电容大小 C C 满足;
C = q V C=\cfrac{q}{V} \cdots①

根据①,②可以得到电容大小 C C 和电容的电流 i i 以及两端电压 V V 的关系;
i ( t ) = C d v ( t ) d t i(t) = C\cfrac{dv(t)}{dt}

HPF的推导

由以上电路可知,假设电流为 i ( t ) i(t) ,则可知
{ V o u t = R i ( t ) i ( t ) = C d Q c ( t ) d t \begin{cases} V_{out} = Ri(t) \cdots③ \\ i(t) = C\cfrac{dQ_c(t)}{dt} \cdots④\\ \end{cases}

所以结合①,③,④可以得到;
Q c ( t ) = C ( V i n ( t ) V o u t ( t ) ) Q_c(t) = C( V_{in}(t) - V_{out}(t)) \cdots ⑤

根据 ③,④,⑤ 可以得到以下关系;
V o u t = C ( d V i n d t d V o u t d t ) I ( t ) R = R C ( d V i n d t d V o u t d t ) V_{out} = \overbrace{ C( \cfrac{dV_{in}}{dt} - \cfrac{dV_{out}}{dt}) }^{I(t)} R = RC(\cfrac{dV_{in}}{dt} - \cfrac{dV_{out}}{dt}) \cdots ⑥

将方程进行离散化,如果输入 V i n V_{in}

根据⑥式可以进行离散化,因此最终滤波输出的序列 y i y_{i}

y i = α y i 1 + α ( x i x i 1 ) y_{i} = \alpha y_{i-1} + \alpha(x_i - x_{i-1})

其中 α = R C R C + T \alpha = \cfrac{RC}{RC+\bigtriangleup_{T}}

所以换成得到;
R C = T ( α 1 α ) RC = \bigtriangleup_{T}(\cfrac{\alpha}{1-\alpha}) \cdots⑧

另外截止频率和低通滤波器的相同;
f c = 1 2 π R C f_c = \cfrac{1}{2\pi RC}

将⑧式代入可以得到截止频率和 α \alpha 的关系;
f c = 1 α 2 π α T f_c = \cfrac{1-\alpha}{2\pi \alpha \bigtriangleup_{T}}

simulink 仿真

这里根据公式⑥构建 simulink 的子模块 subsystem
V o u t = R C ( d V i n d t d V o u t d t ) V_{out} = RC(\cfrac{dV_{in}}{dt} - \cfrac{dV_{out}}{dt})

其中 Sine Wave1 频率设置 为2*pi*4 ,频率为 4 赫兹;

所以这里需要使得 2*pi*4 的信号衰减,所以根据,截止频率 f c f_c

simulink 运行结果

matlab 实现

matlab 根据以下这个公式进行数字滤波器的设计;
y i = α y i 1 + α ( x i x i 1 ) y_{i} = \alpha y_{i-1} + \alpha(x_i - x_{i-1})

Serial = 0:0.1:100;
Fs = 1;
Phase = 0;
Amp = 1;
N0 = 2*pi*Fs*Serial - Phase;
X0 = Amp*sin(N0);
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);
len = length(X2);
X3=X2;
p=0.75;
for i=2:len
    X3(i) = p*X3(i-1)+p*(X2(i)- X2(i-1))
subplot(4,1,4);
plot(X3);

简单地分析一下,代码中的X1X2X3

  • X1频率为1
  • X2频率为0.02
  • {α=p=0.75T=0.1fc=1α2παT \begin{cases} \alpha = p=0.75 \\ \bigtriangleup_{T} = 0.1 \\ f_c = \cfrac{1-\alpha}{2\pi \alpha \bigtriangleup_{T}} \end{cases}

    因此可以得到截止频率如下;
    fc=0.252π0.750.10.53f_c=\cfrac{0.25}{2\pi *0.75* 0.1} \approx 0.53

    matlab 运行结果

    C语言实现

    typedef struct
         int16_t  Input[2];
         int16_t  Output[2];
         int32_t  FilterTf;		
         int32_t  FilterTs;
         int32_t  Ky;
    } high_filter;
    void high_filter_init(high_filter *v);
    int16_t high_filter_calc(high_filter *v);
    
  • FilterTs为采样时间 T\bigtriangleup_{T}
  • FilterTfRC时间常数;
  • Input[0]表示 xix_i
  • Input[1]表示 xi1x_{i-1}
  • Output[0]表示 yiy_i
  • Output[1]表示 yi1y_{i-1}
  • Ky表示RCRC+T\cfrac{RC}{RC+\bigtriangleup_{T}}
  • 参考公式如下所示;

    void high_filter_init(high_filter *v){	
         v->Ky = v->FilterTf*1024/(v->FilterTs + v->FilterTf);
    int16_t high_filter_calc(high_filter *v){
    	int32_t tmp = 0;
    	tmp = ((int32_t)v->Ky*v->Output[1] + v->Ky*(v->Input[0] - v->Input[1]))/1024;
    	if(tmp>32767){
    		tmp = 32767;
    	if( tmp < -32768){
    		tmp = -32768;
        v->Output[0] = (int16_t)tmp;
        v->Output[1] = v->Output[0];
        v->Input[1] = v->Input[0];
    	return v->Output[0];