In general I have a series of signals that each following is a bit stretched version of the previous. To find the incremental stretching, I crop the original signal into some number of short segments and try to match each segment to the relevant region in the next (stretched signal).

To test the performance of xcorr in Matlab I did the following testcase and get really weird result. I have some signal S1 (blue, only part of it is shown in the image), crop a segment from it (red) and pad it with zeros to get identical length to S1. Then, I compute cross-correlation using xcorr with some predefined maxlag. Expecting to get the lag equal to 0 since the location of the segment didn't change compare to it's initial location in S1, but for some reason the lag I get is equal 2. I tried as well different options for normalization, but none really helped. What's the problem with xcorr or with my code?

maxlag=m; % m < numel(S1);


S_crop=S1(1:X); %x < numel(S1)

S_crop_pad=[S_crop, zeros(1,numel(S1)-X)];


[Cmax,I] = max(C12);

lagDiff = Lag(I);

Wrong cross-correlation lag


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.