低通滤波器总结
原理介绍 [ 1 ] ^ {[1]} [1]
一阶连续滤波器
y
(
s
)
x
(
s
)
=
a
s
+
a
(
1
)
\frac{y\left( s \right)}{x\left( s \right)}=\frac{a}{s+a}(1)
x(s)y(s)=s+aa(1)
一阶离散滤波器
y
˙
(
t
)
+
a
y
(
t
)
=
a
x
(
t
)
(
2
)
\dot{y}\left( t \right)+ay\left( t \right)=ax\left( t \right)(2)
y˙(t)+ay(t)=ax(t)(2)
采取一阶前向差分
y
˙
(
t
)
=
y
[
(
k
+
1
)
T
]
−
y
[
k
T
]
T
(
3
)
\dot{y}\left( t \right)=\frac{y \left[ \left( k+1 \right) T \right]-y\left[ kT \right]}{T} (3)
y˙(t)=Ty[(k+1)T]−y[kT](3)
对式(3)进行推导,则可得:
y [ ( k + 1 ) T ] − y ( k T ) T + a y ( k T ) = a x ( k T ) y [ ( k + 1 ) T ] − y ( k T ) + T a y ( k T ) = T a x ( k T ) y [ ( k + 1 ) T ] = ( 1 − a T ) y ( k T ) + T a x ( k T ) \begin{aligned} &\frac{y\left[ \left( k+1 \right) T \right] -y\left( kT \right)}{T}+ay\left( kT \right) =ax\left( kT \right) \\ &y\left[ \left( k+1 \right) T \right] -y\left( kT \right) +Tay\left( kT \right) =Tax\left( kT \right) \\ &y\left[ \left( k+1 \right) T \right] =\left( 1-aT \right) y\left( kT \right) +Tax\left( kT \right) \end{aligned} Ty[(k+1)T]−y(kT)+ay(kT)=ax(kT)y[(k+1)T]−y(kT)+Tay(kT)=Tax(kT)y[(k+1)T]=(1−aT)y(kT)+Tax(kT)
对上述最后一步结果进行整理,可得下方通式
y
[
(
k
+
1
)
T
]
=
(
1
−
A
)
y
(
k
T
)
+
A
x
(
k
T
)
(
4
)
\color{red} y\left[ \left( k+1 \right) T \right] =\left( 1-A \right) y\left( kT \right) +Ax\left( kT \right)(4)
y[(k+1)T]=(1−A)y(kT)+Ax(kT)(4)
式中:
k
+
1
k+1
k+1表示当前时刻,
k
k
k表示上一时刻,
A
A
A为滤波系数,
A
A
A越大,表明滤波器越灵敏,当前测量值在滤波结果中占据的权重越大,同时表明滤波器的滤波效果差(此点可结合频率计算公式理解,
A
A
A越大,滤波截至频率越大),
A
A
A越小(
1
−
A
1-A
1−A越大),表明滤波器不灵敏,上一时刻滤波结果占据的权重越大,即滤波器在延续之前的滤波效果,表明滤波之后的数据非常稳定,由此可说明滤波器的滤波效果好。
滤波器的截止频率计算式为
f
c
=
a
2
π
T
s
(
5
)
f_c=\frac{a}{2\pi T_s}(5)
fc=2πTsa(5)
式中:
f
c
f_c
fc表示截止频率,单位
H
z
\rm Hz
Hz,
T
s
T_s
Ts为采样时间。实际使用过程中,为了获得良好的滤波效果,滤波系数A的设置值不宜过大,考虑香农定理,采样频率至少为截止频率的2倍,才可有效的实现数字滤波器性能。
Simulink实现源码
方法一
采用单位延时环节记录上一时间步的滤波结果
%-----------------
%this code is used as low pass filter(method 1)
%made by PEZHANG
%2021.12.28
%-----------------
function y_now = fcn(y_last,x_now,Ts,fc)
wc = fc*2*3.141592654;
y_now = y_last+wc*Ts*(x_now - y_last);
方法二
采用静态变量记录上一时间步的滤波结果
%-----------------
%this code is used as low pass filter(method 2)
%made by PEZHANG
%2021.12.28
%-----------------
function y_now = fcn(x_now,Ts,fc)
persistent y_last
if isempty(y_last)
y_last=0;
end
wc = fc*2*3.141592654;
y_now = y_last+wc*Ts*(x_now - y_last);
y_last = y_now;
源码效果演示
输入波形成分表达式如下:
y
=
sin
(
2
π
t
)
+
0.25
sin
(
2
π
100
t
)
y=\sin \left( 2\pi t \right) +0.25\sin \left( 2\pi 100t \right)
y=sin(2πt)+0.25sin(2π100t)
在Simulink中按照图示结构搭建模型,仿真验证后导出.xlsx格式数据。仿真图片如下:
绘图代码 [ 2 ] ^ {[2]} [2]
图片一
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
data = pd.read_excel('123456.xlsx')
print(data)
AA = data['time']
BB = data['raw data']
CC = data['filter data(method 1)']
DD = data['filter data(method 2)']
EE = data['y1']
FF = data['y2']
fig, (ax1, ax2, ax3) = plt.subplots(3, 1,figsize=(7.5,6.5))
plt.rcParams['axes.autolimit_mode'] = 'round_numbers'
ax1.plot(AA,EE,linewidth = 1,label='$y_1 = sin(2 \pi t)$')
ax1.grid(True, linestyle='--')
ax1.set_xlim(0,2.5)
ax2.plot(AA,FF,linewidth = 1,label='$y_2 = 0.25sin(2 \pi 100t)$')
ax2.grid(True, linestyle='--')
ax2.set_xlim(0,2.5)
ax2.set_ylim(-0.3,0.3)
ax3.plot(AA,BB,linewidth = 1,label='$y = sin ( 2\pi t ) +0.25sin ( 2\pi 100t ) $')
ax3.grid(True, linestyle='--')
ax3.set_xlim(0,2.5)
ax3.set_xlabel('Time (s)')
plt.grid(True, linestyle='--')
ax1.legend(loc='best')
ax2.legend(loc='best')
ax3.legend(loc='best')
plt.show()
图片二
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
data = pd.read_excel('123456.xlsx')
print(data)
AA = data['time']
BB = data['raw data']
CC = data['filter data(method 1)']
DD = data['filter data(method 2)']
EE = data['y1']
FF = data['y2']
fig, (ax4, ax5) = plt.subplots(2, 1)
ax4.plot(AA,BB,linewidth = 1,label='$raw\ data$')
ax4.plot(AA,CC,linewidth = 1,label='$filter\ data\ method\ 1$')
ax5.plot(AA,BB,linewidth = 1,label='$raw\ data$')
ax5.plot(AA,DD,linewidth = 1,label='$filter\ data\ method\ 2$')
ax4.legend(loc='best')
ax5.legend(loc='best')
plt.show()
参考文献
[1] https://blog.csdn.net/qq_27334499/article/details/52186336?spm=1001.2101.3001.6650.10&utm_medium=distribute.pc_relevant.none-task-blog-2%7Edefault%7EBlogCommendFromBaidu%7Edefault-10.fixedcolumn&depth_1-utm_source=distribute.pc_relevant.none-task-blog-2%7Edefault%7EBlogCommendFromBaidu%7Edefault-10.fixedcolumn
[2] https://blog.csdn.net/qq_50632468/article/details/121981925