python 经典数字滤波实例

作者:weixin_42528089 时间:2022-08-26 10:23:22 

数字滤波分为 IIR 滤波,和FIR 滤波。

FIR 滤波:


import scipy.signal as signal
import numpy as np
import pylab as pl
import matplotlib.pyplot as plt
import matplotlib
from scipy import signal
b = signal.firwin(80, 0.5, window=('kaiser', 8))
w, h = signal.freqz(b)

import matplotlib.pyplot as plt
fig, ax1 = plt.subplots()
ax1.set_title('Digital filter frequency response')

ax1.plot(w, 20 * np.log10(abs(h)), 'b')
ax1.set_ylabel('Amplitude [dB]', color='b')
ax1.set_xlabel('Frequency [rad/sample]')

ax2 = ax1.twinx()
angles = np.unwrap(np.angle(h))
ax2.plot(w, angles, 'g')
ax2.set_ylabel('Angle (radians)', color='g')
ax2.grid()
ax2.axis('tight')
plt.show()

运行结果:

python 经典数字滤波实例

IIR 滤波器:


from scipy import signal
import matplotlib.pyplot as plt
import matplotlib.ticker
import numpy as np
# 蓝色的是频谱图,绿色的是相位图
wp = 0.2
ws = 0.3
gpass = 1
gstop = 40
system = signal.iirdesign(wp, ws, gpass, gstop)
w, h = signal.freqz(*system)
fig, ax1 = plt.subplots()
ax1.set_title('Digital filter frequency response')
ax1.plot(w, 20 * np.log10(abs(h)), 'b')
ax1.set_ylabel('Amplitude [dB]', color='b')
ax1.set_xlabel('Frequency [rad/sample]')
ax1.grid()
ax1.set_ylim([-110, 10])

nticks = 8
ax1.yaxis.set_major_locator(matplotlib.ticker.LinearLocator(nticks))

plt.show()

运行结果:

python 经典数字滤波实例

IIR 滤波器中cheyb2 滤波器的运用


from  scipy import signal
import matplotlib.pyplot as plt
import numpy as np
b, a = signal.cheby2(4, 40, 100, 'low', analog=True)
w, h = signal.freqs(b, a)
plt.semilogx(w, 20 * np.log10(abs(h)))#用于绘制折线图,两个函数的 x 轴、y 轴分别是指数型的。
#plt.plot(w, 20 * np.log10(abs(h)))
plt.title('Chebyshev Type II frequency response (rs=40)')
plt.xlabel('Frequency [radians / second]')
plt.ylabel('Amplitude [dB]')
plt.margins(0, 0.1)#  not sure
plt.grid(which='both', axis='both')

t = np.linspace(0, 1, 1000, False) # 1 second
sig = np.sin(2*np.pi*10*t) + np.sin(2*np.pi*20*t)
fig, (ax1, ax2) = plt.subplots(2, 1, sharex=True)
ax1.plot(t, sig)
ax1.set_title('10 Hz and 20 Hz sinusoids')
ax1.axis([0, 1, -2, 2])

sos = signal.cheby2(12, 20, 17, 'hp', fs=1000, output='sos')
filtered = signal.sosfilt(sos, sig)
ax2.plot(t, filtered)
ax2.set_title('After 17 Hz high-pass filter')
ax2.axis([0, 1, -2, 2])
ax2.set_xlabel('Time [seconds]')

plt.show()

python 经典数字滤波实例

python 经典数字滤波实例

来源:https://blog.csdn.net/weixin_42528089/article/details/102893808

标签:python,数字滤波
0
投稿

猜你喜欢

  • 利用Python脚本批量生成SQL语句

    2023-06-10 10:02:37
  • Python操作redis实例小结【String、Hash、List、Set等】

    2021-07-13 12:24:03
  • thinkPHP学习笔记之安装配置篇

    2023-09-27 18:32:03
  • Mysql 数据库常用备份方法和注意事项

    2024-01-17 15:43:25
  • python中List添加与删除元素的几种方法实例

    2021-10-07 09:13:36
  • php模板引擎技术简单实现

    2023-11-14 13:28:37
  • Adobe AIR beta 2震撼发布!

    2007-10-07 11:57:00
  • MySQL 按指定字段自定义列表排序的实现

    2024-01-16 08:09:22
  • 有效LOGO设计的最重要的提示

    2010-06-09 12:05:00
  • php 方便水印和缩略图的图形类

    2023-10-15 15:31:28
  • 使用zabbix监控mongodb的方法

    2024-01-21 05:13:16
  • Python人工智能之波士顿房价数据分析

    2021-09-23 19:43:35
  • SQL Server中导入导出数据的三种方式

    2008-11-28 15:53:00
  • IE7下动态创建Iframe时,去除边框的办法

    2009-01-19 13:56:00
  • Apache部署Django项目图文详解

    2023-12-17 06:51:05
  • mysql-8.0.15-winx64 解压版安装教程及退出的三种方式

    2024-01-23 21:56:20
  • pytorch 实现在预训练模型的 input上增减通道

    2023-12-02 00:49:33
  • Python对象的深拷贝和浅拷贝详解

    2022-08-29 23:39:02
  • 恢复被删除的数据 Log Explorer for SQL Server 4.2 (一)

    2010-07-01 19:24:00
  • Python Pandas批量读取csv文件到dataframe的方法

    2022-12-15 17:05:03
  • asp之家 网络编程 m.aspxhome.com