# Cross-correlation questions

12 views (last 30 days)
Charms on 20 Feb 2020
Commented: Adam Danz on 20 Feb 2020
Hi, I have 2 spike trains and would like to use cross-correlation to find out their time lag. Are there any resources out there that may guide me on cross correlation and explain how to further interpret the results into smt useful? Also, I have a few questions.
1) why is it that there is still a peak r value with 2 spike trains that are not correlated?
2) Do I have to normalize the spike train?
3) xcorr gives me the r and lag value. But how may I utilize this information to make it useful? ie is it significant?
4) why is it that a xcorr value (i input what mathworks suggested xcorr(x,y), returns me a correlation sequence) can be >1/-1? shouldnt a r value be bounded within that range?
Thank you and i appreciate any help for a beginner here :)

Adam Danz on 20 Feb 2020
Edited: Adam Danz on 20 Feb 2020
Perhaps working through one of the examples in the xcorr documentation would be helpful.
1) why is it that there is still a peak r value with 2 spike trains that are not correlated?
You can cross correlate any two vectors and there will be a maximum correlation at some time point (unless both vectors are flat).
Produce this plot, for example.
theta = linspace(0,2*pi,100);
x1 = sin(theta);
x2 = sin(1.2*theta);
[r, lags] = xcorr(x1,x2);
clf()
tiledlayout(2,1)
nexttile
hold on
plot(theta, x1, 'DisplayName', 'x1')
plot(theta, x2, 'DisplayName', 'x2')
legend()
nexttile
plot(lags,r, 'ks')
grid on
xlabel('lag')
ylabel('cross-corr') 2) Do I have to normalize the spike train?
You don't have to but it might be a good idea to normalize the amplitudes, especially if you're comparing responses from two different sources (ie, two different neurons). I also advise you to look at the literature from your field to see the common approach taken.
3) xcorr gives me the r and lag value. But how may I utilize this information to make it useful? ie is it significant?
After you get the lag, use circshift to circularly shift the 2nd vector so that it aligns to the first vector at the maximum correlation. Plot out the shifted data so you can visually confirm that the shift was done correctly. Then use [rho,pval] = corr(X,Y) to compute the correlation and the corresponding p-value for the shifted vectors.

Charms on 20 Feb 2020
Hi Adam, you are truly God-sent. Thank you so much for taking the effort to write out the explanations - they are extremely clear :). Just wondering, could I use corrcoef instead for Q3?
Adam Danz on 20 Feb 2020
Thanks, Charms. Always happy to help.
Yes, you can use corrcoef, but if you're going to compare the results between xcorr and corrcoef, you'll need to understand how the two functions differ. David Goodmanson explains it well here:
Adam Danz on 20 Feb 2020
I also want to add that circularly shifting the data is useful if you're dealing with circular data. You might just need a linear shift of the data which would likely result in left and right components of the data that aren't paired due to the shift.