为什么 mlab 和 scipy.signal 的交叉光谱不同?
Why the cross spectra is differ in mlab and 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()
和相移
(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()
我尝试使用 scipy
和 mlab
。谁能解释为什么我会得到不同的结果?
因为两个函数有些参数的默认值不同
例如,如果您将选项 noverlap=128
传递给 plt.cohere()
,您将获得与 numpy.signal()
解决方案几乎完美的匹配:
除了 0 Hz 频率的小失配外,我们真的不太关心直流分量的相干性,对吗?我敢打赌,如果您深入研究两者的文档,您会在两者的标准值中发现另一个更小的怪癖。
我有两个信号
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()
和相移
(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()
我尝试使用 scipy
和 mlab
。谁能解释为什么我会得到不同的结果?
因为两个函数有些参数的默认值不同
例如,如果您将选项 noverlap=128
传递给 plt.cohere()
,您将获得与 numpy.signal()
解决方案几乎完美的匹配:
除了 0 Hz 频率的小失配外,我们真的不太关心直流分量的相干性,对吗?我敢打赌,如果您深入研究两者的文档,您会在两者的标准值中发现另一个更小的怪癖。