I am trying to perform a cross-correlation but noticed that performing this in two different ways results in slightly different results.
I have a vector with some spikes ('dual_spikes') and I want to cross-correlate this with 'dips' (using xcorr in Matlab).
I noticed a difference when I perform this in two different ways:
- perform an xcorr as normal with 'dual_spikes'
- perform an xcorr with each individual spike, add them together, and normalise.
I do not know why there should be a difference. Use the following function below for illustration.
function [] = xcorr_fault()
dual_spikes = [zeros(1,200),ones(1,200),zeros(1,400),ones(1,100),zeros(1,100)];
dips = 1-[zeros(1,400),ones(1,1),zeros(1,599)];
plot(dips)
single_spike_1 = [zeros(1,200),ones(1,200),zeros(1,600)];
single_spike_2 = [zeros(1,800),ones(1,100),zeros(1,100)];
xcorr_dual = xcorr_div(dual_spikes,dips);
xcorr_single1 = xcorr_div(single_spike_1,dips);
xcorr_single2 = xcorr_div(single_spike_2,dips);
xcorr_single_all = (xcorr_single1+xcorr_single2)/max(xcorr_single1+xcorr_single2);
xcorr_dual_norm = xcorr_dual/max(xcorr_dual);
figure(1)
clf
hold all
plot(xcorr_dual_norm)
plot(xcorr_single_all)
legend('Single xcorr','xcorr with individual spikes')
function [xcorr_norm] = xcorr_div(lines,signal)
xcorr_signal = xcorr(signal,lines,'none');
xcorr_signal(xcorr_signal<1e-13) = NaN;
xcorr_bg = xcorr(ones(1,length(signal)),lines,'none');
xcorr_norm = xcorr_signal ./ xcorr_bg;
xcorr_norm(isnan(xcorr_norm)) = 1;
Note, the xcorr signal must have a 'background' (bg) divided so only the dips are found. This happens in 'xcorr_div'.