一階RC低通濾波器詳解(仿真+matlab+C語言實現)


如果本文幫到了你,幫忙點個贊;
如果本文幫到了你,幫忙點個贊;
如果本文幫到了你,幫忙點個贊;

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)
所以電容的 Q c ( t ) Q_{c}(t) 的充電時間為 t t 因此滿足以下條件;
{ Q c ( t ) = C V o u t ( t ) i ( t ) = d Q c d t \begin{cases} Q_{c}(t) = CV_{out}(t) \cdots ①\\ \\ i(t) = \cfrac{dQ_{c}}{dt} \cdots ② \end{cases}
所以由①,②可得:
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} \cdots ③
將方程進行離散化,如果輸入 V i n V_{in} 和輸出輸入 V o u t V_{out} 按照 T \bigtriangleup_{T} 的時間采樣,那么可以將輸入和輸出序列化,則
V i n V_{in} 序列化為:
( x 1 , x 2 , x 3 , x n 1 , x n ) (x_{1},x_{2},x_{3}\cdots,x_{n-1},x_{n})
V o u t V_{out} 序列化為:
( y 1 , y 2 , y 3 , y n 1 , y n ) (y_{1},y_{2},y_{3}\cdots,y_{n-1},y_{n})

因此可以將③式轉化為:
x i y i = R C y i y i 1 T x_{i} - y_{i} = RC\cfrac{y_{i}-y_{i-1}}{\bigtriangleup_{T}}\cdots④

因此最終濾波輸出的序列 y i y_{i} 如下所示;
在這里插入圖片描述
同樣進行簡化之后可以得到;
y i = α x i + ( 1 α ) y i 1 y_{i} = \alpha*x_{i} + (1-\alpha)*y_{i-1}

后面可以根據這個公式進行程序設計;
另外,截止頻率 f c f_{c} 滿足;
f c = 1 2 π R C f_{c} = \cfrac{1}{2\pi RC} \cdots ⑤ \\

2 simulink 仿真

這里直接根據公式③構建一搞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}
Subsystem
在這里插入圖片描述
整體的仿真圖如下:
在這里插入圖片描述
其中Sine Wave頻率設置為2*pi*50
在這里插入圖片描述
其中Sine Wave1頻率設置為2*pi
在這里插入圖片描述
所以這里需要使得2*pi*50的信號衰減,所以根據,截止頻率 f c f_{c} 的計算公式,可以改變增益的值,具體如下所示;
在這里插入圖片描述

3 simulink 運行結果

最終的仿真的運行結果如下圖所示;
Gain Value0.005
在這里插入圖片描述
Gain Value0.0318
在這里插入圖片描述

4 matlab實現

根據公式 y i = α x i + ( 1 α ) y i 1 y_{i} = \alpha*x_{i} + (1-\alpha)*y_{i-1}
實現數字一階RC低通濾波器,具體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);

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語言運行結果

實際測試結果
在這里插入圖片描述
在這里插入圖片描述


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM