Commit a142cf7e authored by Andyara Callegare's avatar Andyara Callegare
Browse files

Cross correlation function

parent 2a4b1f7d
......@@ -120,27 +120,19 @@ def basic_stats(cdfmat, var_span, verbose=False):
return median, max, mean, min
def cross_correlation(x, y, normed=True, detrend=False, maxlags=10):
''' Cross correlation of two signals of equal length
def cross_correlation(x, y, maxlags=10):
''' Cross correlation of two signals of equal length
Returns the coefficients when normed=True
Returns inner products when normed=False
Usage: lags, c = xcorr(x,y,maxlags=len(x)-1)
Optional detrending e.g. mlab.detrend_mean'''
c_{av}[k] = sum_n a[n+k] * conj(v[n]) '''
Nx = len(x)
if Nx != len(y):
raise ValueError('x and y must be equal length')
if detrend:
import matplotlib.mlab as mlab
x = mlab.detrend_mean(np.asarray(x)) # can set your preferences here
y = mlab.detrend_mean(np.asarray(y))
c = np.correlate(x, y, mode='full')
if normed:
n = np.sqrt(np.dot(x, x) * np.dot(y, y)) # this is the transformation function
c = np.true_divide(c, n)
if maxlags is None:
maxlags = Nx - 1
......@@ -151,4 +143,4 @@ def cross_correlation(x, y, normed=True, detrend=False, maxlags=10):
lags = np.arange(-maxlags, maxlags + 1)
c = c[Nx - 1 - maxlags:Nx + maxlags]
return lags, c
return lags, c
\ No newline at end of file
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment