Skip to content

Instantly share code, notes, and snippets.

@jdmonaco
Created June 6, 2015 21:55
Show Gist options
  • Save jdmonaco/f0ecd87a9f8428ff030f to your computer and use it in GitHub Desktop.
Save jdmonaco/f0ecd87a9f8428ff030f to your computer and use it in GitHub Desktop.
An example of a lag-limited correlation function for spike train data.
"""
Limited-lag numpy implementation of spike train cross-correlation.
"""
import numpy as np
def xcorr(a, b, maxlag=1.0, bins=128):
"""
Compute the cross-correlogram of two spike train arrays.
"""
if bins % 2 == 0:
bins += 1
edges = np.linspace(-maxlag, maxlag, bins + 1)
nb = b.size
start = end = 0
H = np.zeros(bins)
for t in a:
while b[start] < t - maxlag:
start += 1
while end < nb and b[end] <= t + maxlag:
end += 1
H += np.histogram(b[start:end] - t, bins=edges)[0]
centers = (edges[:-1] + edges[1:]) / 2
return H, centers
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment