python - 为什么 mlab 和 scipy.signal 的交叉光谱不同?

标签 python numpy matplotlib scipy signal-processing

我有两个信号

import numpy as np
import matplotlib.pyplot as plt
from matplotlib import mlab
import mpld3
from scipy import signal

mpld3.enable_notebook()

nfft = 256
dt = 0.01
t = np.arange(0, 30, dt)
nse1 = np.random.randn(len(t))  * 0.1              # white noise 1
nse2 = np.random.randn(len(t))  * 0.1              # white noise 2



# two signals with a coherent part and a random part
s1 = np.sin(2*np.pi*1*t) + nse1
s2 = np.sin(2*np.pi*1*t+np.pi) + nse2 

plt.plot(s1, 'r', s2, 'g')
plt.show()

我想得到连贯性

cxy, fcoh = plt.cohere(s1, s2, nfft, 1./dt)
fcoh,cxy = signal.coherence(s1,s2, nfft=nfft, fs=1./dt)
plt.hold(True)
plt.plot(fcoh, cxy)
#plt.xlim(0, 5)
plt.show()

Coherence

和相移

(csd, f) = mlab.csd(s1, s2, NFFT=nfft, Fs=1./dt)
fig = plt.figure()
angle = np.angle(csd,deg =False)
angle[angle<-np.pi/2] += 2*np.pi

plt.plot(f, angle, 'g')
plt.hold(True)

(f, csd) = signal.csd(s1, s2, fs=1./dt, nfft=nfft)

angle = np.angle(csd,deg =False)
angle[angle<-np.pi/2] += 2*np.pi

plt.plot(f, angle,'r')


#plt.xlim(0,5)
plt.show()

enter image description here

我尝试使用 scipymlab。谁能解释为什么我会得到不同的结果?

最佳答案

因为这两个函数有些参数的默认值不同。

例如,如果您向 plt.cohere() 传递选项 noverlap=128,您将获得与 numpy.signal( ) 解决方案: enter image description here

除了 0 Hz 频率的小失配外,我们真的不太关心直流分量的相干性,对吗?我敢打赌,如果您深入研究两者的文档,您会在两者的标准值中发现另一个更小的怪癖。

关于python - 为什么 mlab 和 scipy.signal 的交叉光谱不同?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35875617/

相关文章:

python - 如何将 (dtype=object) numpy 数组值设置为 Python 列表,而不用 numpy 将列表解释为值列表?

python - 如何使用 BeautifulSoup 查找元素的父标签?

python - math.sqrt 结果 TypeError :'float' 对象无法解释为整数

python - 如何在图像中找到形状的坐标

python - 综合整理 "or"连接的几条语句

python - 具有三角形值的三角轮廓

python - 如何绘制可拖动的多边形

python - 防止在正则表达式上回溯以查找非注释行(不是以缩进 '#' 开头)

python - (Django) 从 charField 中修剪空格

python - 用 python : How to set up the graph correctly? 切割的图