Python+matplotlib实现计算两个信号的交叉谱密度实例

yipeiwu_com6年前Python基础

 计算两个信号的交叉谱密度

结果展示:

完整代码:

import numpy as np
import matplotlib.pyplot as plt


fig, (ax1, ax2) = plt.subplots(2, 1)
# make a little extra space between the subplots
fig.subplots_adjust(hspace=0.5)

dt = 0.01
t = np.arange(0, 30, dt)

# Fixing random state for reproducibility
np.random.seed(19680801)


nse1 = np.random.randn(len(t))         # white noise 1
nse2 = np.random.randn(len(t))         # white noise 2
r = np.exp(-t / 0.05)

cnse1 = np.convolve(nse1, r, mode='same') * dt  # colored noise 1
cnse2 = np.convolve(nse2, r, mode='same') * dt  # colored noise 2

# two signals with a coherent part and a random part
s1 = 0.01 * np.sin(2 * np.pi * 10 * t) + cnse1
s2 = 0.01 * np.sin(2 * np.pi * 10 * t) + cnse2

ax1.plot(t, s1, t, s2)
ax1.set_xlim(0, 5)
ax1.set_xlabel('time')
ax1.set_ylabel('s1 and s2')
ax1.grid(True)

cxy, f = ax2.csd(s1, s2, 256, 1. / dt)
ax2.set_ylabel('CSD (db)')
plt.show()

总结

以上就是本文关于Python+matplotlib实现计算两个信号的交叉谱密度实例的全部内容,希望对大家有所帮助。感兴趣的朋友可以继续参阅本站其他相关专题,如有不足之处,欢迎留言指出。感谢朋友们对本站的支持!

相关文章

关于Python中浮点数精度处理的技巧总结

关于Python中浮点数精度处理的技巧总结

前言 最近在使用Python的时候遇到浮点数运算,发现经常会碰到如下情况: 出现上面的情况,主要还是因浮点数在计算机中实际是以二进制保存的,有些数不精确。 比如说: 0.1是十进制,...

python如何实现远程控制电脑(结合微信)

不知道大家有没有这样一个烦恼,“自己的电脑总是被别人使用,又不好意思设置密码”,所以利用python设计了一个程序来实现自由管控。 功能虽然简单,但大家可以通过其思路来实现更多的功能。...

浅谈Python 列表字典赋值的陷阱

今天在用python刷leetcode 3Sum problem时,调入到了一个大坑中,检查半天并没有任何逻辑错误,但输出结果却总是不对,最终通过调试发现原来python中list和di...

python Django编写接口并用Jmeter测试的方法

python Django编写接口并用Jmeter测试的方法

一、环境准备 python3.6.7 Pycharm 二、创建项目 我这里是在Django项目中新建了个APP,目录结构如下图所示: 那么怎么在已有的Django项目中新建...

对python添加模块路径的三种方法总结

之前对mac os系统自带的python进行了升级,结果发现新安装的python的site-packages目录并没有加到python的系统路径中,所以在使用其他库时发现出现了缺少模块的...