0

ms-coherence在两个信号之间进行分析,并将结果scipy.signal.coherence与​​ Matlab 的mscohere函数进行比较。当我在两个函数中使用相同的参数时,我没有得到相同的结果:

MATLAB

x = [1,3,5,7,4,6,7,8,9,3,5,6]
y = [3,4,7,8,9,1,2,3,4,1,5,6]

[cxy,w] = mscohere(x,y,hann(6),3,6,1)

cxy =
    0.7489
    0.6034
    0.2813
    0.3319

w =
         0
    0.1667
    0.3333
    0.5000

Python

from scipy.signal import coherence
x = [1,3,5,7,4,6,7,8,9,3,5,6]
y = [3,4,7,8,9,1,2,3,4,1,5,6]

coherence(x,y,'hann',noverlap=3,nperseg=6,fs=1,detrend=False)

(array([0.        , 0.16666667, 0.33333333, 0.5       ]),
 array([0.76762535, 0.53185638, 0.32743784, 0.07759385]))

我错过了什么?

4

1 回答 1

0

内部算法都是不同的,我对两个代码都进行了调试,我可以感知到差异。下面你会看到在 scipy.signal.coherence 和 Matlab 的 mscohere 中使用相同参数的差异。

按照matlab的一些细节默认参数:

# %   When WINDOW and NOVERLAP are not specified, MSCOHERE divides X into
# %   eight sections with 50% overlap and windows each section with a Hamming
# %   window. MSCOHERE computes and averages the periodogram of each section
# %   to produce the estimate.
# L = fix(M./4.5);  noverlap = fix(0.5.*L); options.nfft = max(256,2^nextpow2(N));
#k = (M-noverlap)./(L-noverlap);
#m= n_sample L = int( n_sample // 4.5 ) noverlap= n/2

使用相同的参数matlab跟随示例python

 L = int( n_sample // 4.5 )
 nfft = max( 256, 2 ** math.ceil( math.log2( L ) ) )
 frequences, coherence = signal.coherence(mean_zero_left, mean_zero_rigth, fs=self._fa, nfft=nfft, window=signal.get_window('hamming', L, fftbins=False), noverlap=int(L//2))

通过这种方式,您可以看到比较 Python x Matlab的两个结果的图表

我建议使用 matplotlib.mlab.cohere 具有相同的算法 matlab,但点 fft (nfft) 的数量具有验证等于 niquist 定理。

coherence2 = mlab.cohere(mean_zero_left, mean_zero_rigth, window=np.hamming(L), NFFT=nfft, noverlap=int(L//2), Fs=self._fa)
于 2021-08-09T11:01:25.643 回答