Skip to content

通过知识获得解放,通过技术获得自由

Menu
  • 专题目录
  • 液压相关
    • 液压技术
    • 液压相邻技术
    • 液压应用
  • 计算机相关
    • 计算机和软件
    • 网络和网站技术
  • 哲学
  • 关于本站
Menu

基于python3实现的数字滤波器[0]

Posted on 2023年11月14日2025年5月6日 by

本篇是基于部署jupyterhub后实现的python工程计算脚本。

  • 低通滤波器
import numpy as np
from matplotlib import pyplot as plt
f=1 #信号频率Hz
t=np.linspace(0,5*1/f,5*int(1/f*1000)) #时间
x=np.sin(2*np.pi*f*t)  #生成信号曲线


#滤波算法
a0=0.9691
b0=0.01547
b1=0.01547
n=len(t)
y=np.zeros((1,n),dtype=float)

#截至频率5Hz,采样频率1KHz
for i in range(0,n-1):
    y[:,i+1]=b1*x[i+1]+b0*x[i]+a0*y[:,i]

#绘制滤波后信号
plt.plot(t,x,'r')
plt.plot(t,y[0,:],'--')
plt.legend(['input','output'])
plt.grid()
plt.show()

1Hz效果

6Hz效果如下:

  • 高通滤波器
import numpy as np
from matplotlib import pyplot as plt
f=10 #信号频率Hz
t=np.linspace(0,5*1/f,5*int(1/f*1000)) #时间
n=len(t)
x=np.sin(2*np.pi*f*t)  #生成信号曲线


#滤波算法
a0=0.9691
b0=-0.9845
b1=0.9845
y=np.zeros((1,n),dtype=float)

#截至频率5Hz,采样频率1KHz
for i in range(0,n-1):
    y[:,i+1]=b1*x[i+1]+b0*x[i]+a0*y[:,i]

#绘制滤波后信号
plt.plot(t,x,'r')
plt.plot(t,y[0,:],'--')
plt.legend(['input','output'])
plt.grid()
plt.show()

10Hz

1Hz效果如下:

  • 带通滤波器
import numpy as np
from matplotlib import pyplot as plt
f=1 #信号频率Hz,1 10 60
t=np.linspace(0,5*1/f,5*int(1/f*1000)) #时间
n=len(t)
x=np.sin(2*np.pi*f*t)  #生成信号曲线


#滤波算法
a0=-0.7164
a1=1.712
b0=-0.1346
b1=0
b2=0.1346
y=np.zeros((1,n),dtype=float)

#频率2.6-50Hz带通滤波器,采样频率1KHz
for i in range(0,n-2):
    y[:,i+2]=b2*x[i+2]+b0*x[i]+a1*y[:,i+1]+a0*y[:,i]

#绘制滤波后信号
plt.plot(t,x,'r')
plt.plot(t,y[0,:],'--')
plt.legend(['input','output'])
plt.grid()
plt.show()

1Hz效果如下:

10Hz效果

60Hz效果

  • butterworth滤波器
import numpy as np
from matplotlib import pyplot as plt
f=1 #信号频率Hz ,1,20
t=np.linspace(0,5*1/f,5*int(1/f*1000)) #时间
n=len(t)
x=10*np.sin(2*np.pi*f*t)  #生成信号曲线

#滤波算法
a0=-0.84860184
a1=3.5336803
a2=-5.5209723
a3=3.8358795
b0=0.89732853/pow(10,6)
b1=3.5893141/pow(10,6)
b2=5.3839712/pow(10,6)
b3=-3.58931411/pow(10,6)
b4=0.89732853/pow(10,6)

y=np.zeros((1,n),dtype=float)

#10hz频率进行滤波,采样频率1KHz
#为啥需要2倍?
for i in range(0,n-4):
    y[:,i+4]=2*(b4*x[i+4]+b3*x[i+3]+b2*x[i+2]+b1*x[i+1]+b0*x[i]) \
              +a3*y[:,i+3]+a2*y[:,i+2]+a1*y[:,i+1]+a0*y[:,i]


#绘制滤波后信号
plt.plot(t,x,'r')
plt.plot(t,y[0,:],'--')
plt.legend(['input','output'])
plt.grid()
plt.show()

1Hz效果:

20Hz效果

  • 贝塞尔滤波器
import numpy as np
from matplotlib import pyplot as plt
f=1 #信号频率Hz ,1,10
t=np.linspace(0,10*1/f,10*int(1/f*1000)) #时间
n=len(t)
x=10*np.sin(2*np.pi*f*t)  #生成信号曲线

#滤波算法
a0=0.9759648
a1=-2.9516865
a2=2.9757207
b0=0.12349019/pow(10,6)
b1=0.37047056/pow(10,6)
b2=0.37047056/pow(10,6)
b3=0.1234909/pow(10,6)

y=np.zeros((1,n),dtype=float)

#10hz频率进行滤波,采样频率1KHz
for i in range(0,n-3):
    y[:,i+3]=(b3*x[i+3]+b2*x[i+2]+b1*x[i+1]+b0*x[i]) \
              +a2*y[:,i+2]+a1*y[:,i+1]+a0*y[:,i]


#绘制滤波后信号
plt.plot(t,x,'r')
plt.plot(t,y[0,:],'--')
plt.legend(['input','output'])
plt.grid()
plt.show()

1Hz效果

20Hz效果

 

 

欢迎回来

希望本站对你有所帮助!

如有疑问请联系info@fanlog.org
2023 年 11 月
一二三四五六日
 12345
6789101112
13141516171819
20212223242526
27282930 
« 6 月    

AI辅助 (17)

© 2025 | Powered by Superbs Personal Blog theme
We use cookies on our website to give you the most relevant experience by remembering your preferences and repeat visits. By clicking “Accept All”, you consent to the use of ALL the cookies. However, you may visit "Cookie Settings" to provide a controlled consent.
Cookie SettingsAccept All
Manage consent

Privacy Overview

This website uses cookies to improve your experience while you navigate through the website. Out of these, the cookies that are categorized as necessary are stored on your browser as they are essential for the working of basic functionalities of the website. We also use third-party cookies that help us analyze and understand how you use this website. These cookies will be stored in your browser only with your consent. You also have the option to opt-out of these cookies. But opting out of some of these cookies may affect your browsing experience.
Necessary
Always Enabled
Necessary cookies are absolutely essential for the website to function properly. This category only includes cookies that ensures basic functionalities and security features of the website. These cookies do not store any personal information.
Non-necessary
Any cookies that may not be particularly necessary for the website to function and is used specifically to collect user personal data via analytics, ads, other embedded contents are termed as non-necessary cookies. It is mandatory to procure user consent prior to running these cookies on your website.
SAVE & ACCEPT