try: X = X/abs(X) Y = Y/abs(Y) except ArithmeticException, ae: print "Error: Division by zero", e except: print "Anderer Fehler! (Vielleicht Index?)" pd = n.exp(n.log(X)-n.log(Y)) return abs(pd.mean()) elif len(x.shape)==2: X = n.zeros((x.shape),"D") Y = n.zeros((y.shape),"D") for i in range(x.shape[1]): X[:,i] = hilbert(demean(x[:,i])) Y[:,i] = hilbert(demean(y[:,i])) try: for i in range(X.shape[1]): X[:,i] /= abs(X[:,i]) Y[:,i] /= abs(Y[:,i]) except ArithmeticException, ae: print "Error: Division by zero", e pd = n.exp(n.log(X)-n.log(Y)) return abs(pd.mean(axis=1)) #def phase_coherence_fetptrials def phases(data,continuous=False):
def phase_coherence(x,y): """Diese Funktion berechnet aus den Arrays die mittlere Phasenkohärenz R, ein Mass zwischen 0 und 1 (zirkuläre Statistik). If the arrays are 1d, calculate the mean over time. If they are 2d (second dimensions usually trials), calculate mean over this dimension.""" assert x.shape == y.shape if len(x.shape)==1: X = hilbert(demean(x)) Y = hilbert(demean(y)) try: X = X/abs(X) Y = Y/abs(Y) except ArithmeticException, ae: print "Error: Division by zero", e except:
def phase_coherence(x, y): """Diese Funktion berechnet aus den Arrays die mittlere Phasenkohärenz R, ein Mass zwischen 0 und 1 (zirkuläre Statistik). If the arrays are 1d, calculate the mean over time. If they are 2d (second dimensions usually trials), calculate mean over this dimension.""" assert x.shape == y.shape if len(x.shape) == 1: X = hilbert(demean(x)) Y = hilbert(demean(y)) try: X = X / abs(X) Y = Y / abs(Y) except ArithmeticException, ae: print "Error: Division by zero", e except:
def wt_analyze(x,freqs=None,Fs=1000.0,wtSampleWidth=0.8): """ Analyzes a given dataset via wavelet-analyis. :param x: input array :type x: array-like :param freqs: 1-d array of center frequencies :type freqs: array-like :param Fs: Sampling frequency :type Fs: float :param wtSampleWidth: time span for which the wavelets are sampled. :type wtSampleWidth: float .. hint:: .. plot:: :include-source: import matplotlib.pyplot as p import numpy as np from eegpy.analysis.wavelet import wt_analyze Fs = 100. F_sin1 = 3. F_sin2 = 7. freqs = np.arange(1,10) ts = np.arange(0,10,1/Fs) ys = np.zeros((Fs*10)) ys[:Fs*10/2] = np.sin(2*np.pi*F_sin1*ts[:Fs*10/2])*np.hanning(Fs*10/2) ys[Fs*10/2:] = np.sin(2*np.pi*F_sin2*ts[:Fs*10/2])*np.hanning(Fs*10/2) * 2 wts = wt_analyze(ys,freqs=freqs,Fs=Fs) power = abs(wts)**2 p.subplot(211) p.plot(ts,ys) p.title(r"$F_1=%.1f, \; F_2=%.1f$"%(F_sin1,F_sin2)) p.subplot(212) p.imshow(power.T,aspect="auto",interpolation="nearest", origin="lower", extent=[0,10,0.5,9.5]) """ assert len(x)>0, "x must be an array containing the data" if not freqs==None: assert len(freqs)>0, "freqs must be an iterable object with the frequencies" else: freqs = n.logspace(0.4,2.1,80) #tStart = time() wts = make_wavelets(freqs,Fs=Fs,wtSampleWidth=wtSampleWidth) #print "--", time()-tStart x = taper(demean(x)) #preprocessing... rv = n.zeros((wts.shape[1]+x.shape[0]-1,len(freqs)), "D")#wts.shape[1]),"D") for fi in range(len(freqs)): #print convolve(x,wts[fi,:]).shape, rv.shape #sys.stdout.flush() rv[:,fi] = convolve(x,wts[fi,:])#,"full") rv = rv[(wts.shape[1]-1)/2 : rv.shape[0]-1-(wts.shape[1]-1)/2,:] return rv
def wt_analyze(x, freqs=None, Fs=1000.0, wtSampleWidth=0.8): """ Analyzes a given dataset via wavelet-analyis. :param x: input array :type x: array-like :param freqs: 1-d array of center frequencies :type freqs: array-like :param Fs: Sampling frequency :type Fs: float :param wtSampleWidth: time span for which the wavelets are sampled. :type wtSampleWidth: float .. hint:: .. plot:: :include-source: import matplotlib.pyplot as p import numpy as np from eegpy.analysis.wavelet import wt_analyze Fs = 100. F_sin1 = 3. F_sin2 = 7. freqs = np.arange(1,10) ts = np.arange(0,10,1/Fs) ys = np.zeros((Fs*10)) ys[:Fs*10/2] = np.sin(2*np.pi*F_sin1*ts[:Fs*10/2])*np.hanning(Fs*10/2) ys[Fs*10/2:] = np.sin(2*np.pi*F_sin2*ts[:Fs*10/2])*np.hanning(Fs*10/2) * 2 wts = wt_analyze(ys,freqs=freqs,Fs=Fs) power = abs(wts)**2 p.subplot(211) p.plot(ts,ys) p.title(r"$F_1=%.1f, \; F_2=%.1f$"%(F_sin1,F_sin2)) p.subplot(212) p.imshow(power.T,aspect="auto",interpolation="nearest", origin="lower", extent=[0,10,0.5,9.5]) """ assert len(x) > 0, "x must be an array containing the data" if not freqs == None: assert len( freqs) > 0, "freqs must be an iterable object with the frequencies" else: freqs = n.logspace(0.4, 2.1, 80) #tStart = time() wts = make_wavelets(freqs, Fs=Fs, wtSampleWidth=wtSampleWidth) #print "--", time()-tStart x = taper(demean(x)) #preprocessing... rv = n.zeros((wts.shape[1] + x.shape[0] - 1, len(freqs)), "D") #wts.shape[1]),"D") for fi in range(len(freqs)): #print convolve(x,wts[fi,:]).shape, rv.shape #sys.stdout.flush() rv[:, fi] = convolve(x, wts[fi, :]) #,"full") rv = rv[(wts.shape[1] - 1) / 2:rv.shape[0] - 1 - (wts.shape[1] - 1) / 2, :] return rv
try: X = X / abs(X) Y = Y / abs(Y) except ArithmeticException, ae: print "Error: Division by zero", e except: print "Anderer Fehler! (Vielleicht Index?)" pd = n.exp(n.log(X) - n.log(Y)) return abs(pd.mean()) elif len(x.shape) == 2: X = n.zeros((x.shape), "D") Y = n.zeros((y.shape), "D") for i in range(x.shape[1]): X[:, i] = hilbert(demean(x[:, i])) Y[:, i] = hilbert(demean(y[:, i])) try: for i in range(X.shape[1]): X[:, i] /= abs(X[:, i]) Y[:, i] /= abs(Y[:, i]) except ArithmeticException, ae: print "Error: Division by zero", e pd = n.exp(n.log(X) - n.log(Y)) return abs(pd.mean(axis=1)) #def phase_coherence_fetptrials