Created
September 29, 2014 14:00
-
-
Save werediver/a003604e3a275cf40e39 to your computer and use it in GitHub Desktop.
Sample code
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from __future__ import division, print_function | |
import numpy as np | |
import matplotlib.pyplot as pp | |
def xcorr(a, b, mode='same'): | |
a = np.asarray(a) | |
b = np.asarray(b) | |
a = a - a.mean() | |
b = b - b.mean() | |
r = np.correlate(a, b, mode) | |
r /= np.sqrt(np.correlate(a, a) * np.correlate(b, b)) | |
return r | |
def xcorr_sl(a, b, k=2): | |
sl = k / np.sqrt(min(len(a), len(b))) # min? | |
return sl | |
def xcorr_lags(r): | |
lag_max = int(len(r) / 2) | |
lags = np.asarray(xrange(-lag_max, lag_max + len(r) % 2)) | |
return lags | |
def xcorr_plot(a, b, mode='same', sl_k=2): | |
r = xcorr(a, b) | |
sl = xcorr_sl(a, b, k=sl_k) | |
lags =xcorr_lags(r) | |
pp.plot(lags, r) | |
pp.plot(lags, np.ones(len(lags)) * sl, color='r') | |
pp.plot(lags, np.ones(len(lags)) * -sl, color='r') | |
pp.show() | |
n = 100 | |
a = np.random.normal(10, 2, n) | |
#b = np.random.normal(20, 5, n) | |
xcorr_plot(a, a) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment