Dear all,
I’m a novice in mne. I need to compute inter-trial phrase coherence across epochs not resolved on time. I may have missed something but I did not found a function for this simple task. I wrote my own function (see below) however it is not clear to me which object to use to store the data in order to be correctly dealt by mne. I used mne.time_frequency.SpectrumArray but probably I need to change the info field in order to set the measure as coh (between 0 and 1).
Thanks in advance,
Francesco
- MNE version: e.g. 1.9.0
- operating system: Ubuntu 18.04
def itpc_spectrum(epo, fmin=None, fmax=None):
"""Compute INTER TRIAL PHASE COHERENCE from epochs.
Parameters
----------
epo : Epochs
fmin : minimum frequency to return [Hz]
fmax : maximum frequency to return [Hz]
Returns
-------
spectrum : a spectrum mne object with inter trial phase coherence, it will be plotted with wrong labels and scales.
"""
data = epo.get_data()
N = data.shape[2]
if np.mod(N,2):
N = N-1
data = data[:,:,range(0,N)]
Nf = np.divmod(N,2)[0]
yf = fft(data)
xf = fftfreq(N, 1 / epo.info['sfreq'])[0:Nf]
ind_min = 0
ind_max = xf.shape[0]
if (fmin!=None):
ind_min= np.where(xf>fmin)[0][0]
if (fmax!=None):
ind_max= np.where(xf<fmax)[0][-1]
xf=xf[ind_min:ind_max]
yf=yf[:,:,ind_min:ind_max]
cf = np.sum(np.divide(np.real(yf),np.abs(yf)), axis=0)
sf = np.sum(np.divide(np.imag(yf),np.abs(yf)), axis=0)
k = np.power(yf.shape[0],2)
itpc = np.multiply(cf,cf)/k+np.multiply(sf,sf)/k
spectrum = mne.time_frequency.SpectrumArray(
data=itpc,
freqs=xf,
info=epo.info,
)
return spectrum