为什么matplotlib(python)中的cohere函数给出的答案与MATLAB中的mscohere函数不同? [英] Why is cohere function in matplotlib (python) give answer different from mscohere function in MATLAB?

查看:1161
本文介绍了为什么matplotlib(python)中的cohere函数给出的答案与MATLAB中的mscohere函数不同?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

maltlab和pythonmatplotlib.mlab中包含为与具有相同名称的MATLAB命令兼容而编写的Python数值函数.

in maltlab and pythonmatplotlib.mlab contains Numerical python functions written for compatability with MATLAB commands with the same names.

  1. 但是对我来说,我在MATLAB和python中得到了不同的结果.有谁知道,为什么会这样?

  1. But for me I get different results in MATLAB and python. Does anyone has any idea, why is it so?

MATLAB mscohere函数具有用于设置窗口大小的参数WINDOW,我在matplotlib.mlab(python)中找不到cohere函数

MATLAB mscohere function has a parameter WINDOW to set the size of the window, which I do not find for cohere function in matplotlib.mlab (python)

Cxy = mscohere(y1,y2,16,0,16)

Cxy = mscohere(y1,y2,16,0,16)

Cxy = matplotlib.pyplot.cohere(y1,y2,NFFT = 16,noverlap = 0)

Cxy = matplotlib.pyplot.cohere(y1, y2,NFFT=16,noverlap=0)

其中y1和y2在MATLAB和python中相同,并且长度为1024

where y1 and y2 are same in MATLAB and python and of length 1024

有帮助吗?

这是代码:

MATLAB:

Fs=8000;
y1=zeros(1,1024);
y2=zeros(1,1024);
for f =0:100:1900
    for n=0:1023
      y1(n+1)=y1(n+1)+sin(2*pi*f*n/Fs);
      if mod(f,200)==0
          y2(n+1)=y2(n+1)+sin(2*pi*f*n/Fs);
      end
    end
end

Cxy = mscohere(y1,y2,16,0,16);
display(Cxy);

Cxy =

    0.8300
    0.0504
    0.0006
    0.0082
    0.1828
    0.2562
    0.7984
    0.9788
    0.9884

PYTHON:

Fs=8000
sample=1024
frequencys=100 * np.arange(20)
#print(frequencys)

y1=np.zeros(sample)
y2=np.zeros(sample)
for f in range(frequencys.size):
    for n in range(sample):
        y1[n]=y1[n]+sin(2*pi*frequencys[f]*n/Fs)
        if frequencys[f]%200==0:
            y2[n]=y2[n]+sin(2*pi*frequencys[f]*n/Fs)
cxy,f = plt.cohere(y1, y2,NFFT=16,noverlap=0)
print(cxy)

    Cxy=[ 0.78894285  
0.06083255  
0.01161213  
0.00249976  
0.14194519  
0.38694284 
0.78120729  
0.8384586   
0.85438165]

推荐答案

最初,matplotlib.mlab函数旨在提供与其MATLAB等效项相似的功能,但MATLAB和numpy之间的某些基本方法有所不同,因此它们并不总是做事以完全相同的方式,并且在某些情况下,由于需求和/或目标的不同,它们有所不同.如今,mlab是更多的数字函数集,在numpy中不可用,但matplotlib却需要这些函数.

Originally matplotlib.mlab functions were meant to provide similar capabilities to their MATLAB equivalents, but some basics approaches between MATLAB and numpy differ so they don't always do things in exactly the same way, and in some cases they have diverged somewhat due to differing needs and/or goals. Nowadays mlab is more a set of numerical functions that aren't available in numpy but are needed by matplotlib.

在您的情况下,MATLAB函数默认使用hamming窗口,而matplotlib版本默认使用hanning窗口.您可以通过简单地传递适当长度的hamming窗口来重现MATLAB结果.另外,MATLAB默认情况下使用Fs=1,而matplotlib默认情况下使用Fs=2,但是我认为仅更改频率,而不更改cxy:

In your case, the MATLAB function defaults to using a hamming window, while the matplotlib version defaults to using a hanning window. You can reproduce the MATLAB results by simple passing an appropriately-lengthed hamming window. Also, MATLAB uses Fs=1 by default while matplotlib uses Fs=2 by default, but I think that only changes the frequencies, not cxy:

>>> cxy, _ = mlab.cohere(y1, y2, window=np.hamming(16), NFFT=16, noverlap=0, Fs=1)
>>> print(cxy)
[  8.29985202e-01   5.03649611e-02   5.54167037e-04   8.19190824e-03   1.82760544e-01   2.56179777e-01   7.98391906e-01   9.78827021e-01   9.88429511e-01]

这篇关于为什么matplotlib(python)中的cohere函数给出的答案与MATLAB中的mscohere函数不同?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆