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

yipeiwu_com5年前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实现媒体播放器功能的具体代码,供大家参考,具体内容如下 playMP3.py # -*- coding: utf-8 -*- import wx;...

python单例模式的多种实现方法

前言 单例模式(Singleton Pattern),是一种软件设计模式,是类只能实例化一个对象, 目的是便于外界的访问,节约系统资源,如果希望系统中 只有一个对象可以访问,就用单例模式...

pyqt5、qtdesigner安装和环境设置教程

pyqt5、qtdesigner安装和环境设置教程

前言 最近工作需要写一个界面程序来调用摄像头并对摄像头采集的图像做一些处理。程序需要使用Python语言编写,经过调研发现PyQt5配合QtDesigner在界面程序编写方面具有功能丰富...

Python json模块dumps、loads操作示例

本文实例讲述了Python json模块dumps、loads操作。分享给大家供大家参考,具体如下: python中json数据的使用。 dumps和loads也是需要成对使用的,就像c...