2010-11-16 123 views
1

親愛的全部 我正在尋找一種numpy/scipy函數來計算雙向相干性和自動雙相干性,研究三波相互作用。 謝謝所有可能的幫助 尼古拉函數計算雙相干

回答

1

也許這Matlab toolbox將幫助;通常,將Matlab轉換爲Python非常容易。

3

這在Python土地最佳的解決方案是http://pypi.python.org/pypi/nitime

它有幾個相干估計,但我並沒有在那些非常仔細。它是一個神經影像學軟件包,但是算法只是故意使用numpy和scipy,所以它可以被其他應用程序使用。

1

這是一個依賴於scipy.spectrogram函數(scipy version> 0.17)並計算兩個信號之間的雙相干函數。從2001年Hagihira和林2007。參見Wikipedia-bicoherence

希望

定義有所幫助。

問候,

def compute_bicoherence(s1, s2, rate, nperseg=1024, noverlap=512): 
    """ Compute the bicoherence between two signals of the same lengths s1 and s2 
    using the function scipy.signal.spectrogram 
    """ 
    from scipy import signal 
    import numpy 
    # compute the stft 
    f1, t1, spec_s1 = signal.spectrogram(s1, fs = rate, nperseg = nperseg, noverlap = noverlap, mode = 'complex',) 
    f2, t2, spec_s2 = signal.spectrogram(s2, fs = rate, nperseg = nperseg, noverlap = noverlap, mode = 'complex') 

    # transpose (f, t) -> (t, f) 
    spec_s1 = numpy.transpose(spec_s1, [1, 0]) 
    spec_s2 = numpy.transpose(spec_s2, [1, 0]) 

    # compute the bicoherence 
    arg = numpy.arange(f1.size/2) 
    sumarg = arg[:, None] + arg[None, :] 
    num = numpy.abs(
     numpy.mean(spec_s1[:, arg, None] * spec_s1[:, None, arg] * numpy.conjugate(spec_s2[:, sumarg]), 
     axis = 0) 
     ) ** 2 
    denum = numpy.mean(
     numpy.abs(spec_s1[:, arg, None] * spec_s1[:, None, arg]) ** 2, axis = 0) * numpy.mean(
      numpy.abs(numpy.conjugate(spec_s2[:, sumarg])) ** 2, 
      axis = 0) 
    bicoh = num/denum 
    return f1[arg], bicoh 

# exemple of use and display 
freqs, bicoh = compute_bicoherence(s1, s2, rate) 
f = plt.figure(figsize = (9, 9)) 
plt.pcolormesh(freqs, freqs, bicoh, 
    # cmap = 'inferno' 
    ) 
plt.colorbar() 
plt.clim(0, 0.5) 
plt.show()