2016-03-08 31 views
3

我有两个信号为什么交叉谱在mlab和scipy.signal中有所不同?

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。任何人都可以解释为什么我会得到不同的结果?

回答

4

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

例如,如果你在传递给plt.cohere()选项noverlap=128你获得与numpy.signal()解决了一个近乎完美的比赛: enter image description here

除了在0赫兹的频率小的失配,而我们并不真正在意关于DC组件的一致性呢?我敢打赌,如果你深入了解两者的文档,你会发现两者的标准值有一个较小的偏差。

相关问题