Why is cohere function in matplotlib (python) give answer different from mscohere function in MATLAB? -
in maltlab , pythonmatplotlib.mlab contains numerical python functions written compatability matlab commands same names.
but me different results in matlab , python. has idea, why so?
matlab mscohere function has parameter window set size of window, not find cohere function in matplotlib.mlab (python)
cxy = mscohere(y1,y2,16,0,16)
cxy = matplotlib.pyplot.cohere(y1, y2,nfft=16,noverlap=0)
where y1 , y2 same in matlab , python , of length 1024
any help?
here code:
matlab:
fs=8000; y1=zeros(1,1024); y2=zeros(1,1024); f =0:100:1900 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) f in range(frequencys.size): 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]
originally matplotlib.mlab
functions meant provide similar capabilities matlab equivalents, basics approaches between matlab , numpy differ don't things in same way, , in cases have diverged due differing needs and/or goals. nowadays mlab
more set of numerical functions aren't available in numpy needed matplotlib
.
in case, matlab function defaults using hamming
window, while matplotlib
version defaults using hanning
window. can reproduce matlab
results simple passing appropriately-lengthed hamming
window. also, matlab uses fs=1
default while matplotlib uses fs=2
default, think changes 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]
Comments
Post a Comment