2016-03-08 30 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組件的一致性呢?我敢打賭,如果你深入瞭解兩者的文檔,你會發現兩者的標準值有一個較小的偏差。

相關問題