How can I use xcorr to determine the amount of offset between one signal and another?
The following M-file demonstrates how to use xcorr to determine how much offset exists between two signals, x1 and x2 : t=0:.01:1.01; x1=[t.^2]; % generate sample data x2=[0 0 0 t.^2]; % x2 is delayed version of x1, delayed by 4 samples xc=xcorr(x1,x2); % perform cross correlation index=find(xc==max(xc)); % this is the index of the cross correlation where % the cross correlation is at its maximum. I.e. % this is where x1 and x2 line up the best. middle=(length(xc)+1)/2; % this is the middle element (since length(xc) is always % odd) if index-middle>=0, disp([‘x2 is delayed from x1 by ‘ num2str(index-middle)… ‘ sample(s)’]); else disp([‘x1 is delayed from x2 by ‘ num2str(middle-index)…