# Wrong estimate of lag by xcorr between a signal and its segmented identical copy in Matlab

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);

S1=Raw_Data(1,:);

S_crop=S1(1:X); %x < numel(S1)
{"ft_lang_label":"__label__en","ft_lang_prob":0.90648365,"math_prob":0.69854397,"size":1110,"snap":"2020-34-2020-40","text_gpt3_token_len":283,"char_repetition_ratio":0.10488246,"word_repetition_ratio":0.0,"special_character_ratio":0.25945947,"punctuation_ratio":0.13675214,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.95353997,"pos_list":[0,1,2],"im_url_duplicate_count":[null,2,null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2020-08-05T10:50:51Z\",\"WARC-Record-ID\":\"<urn:uuid:1bdbfd01-8bbe-4261-8c53-214158df1bdf>\",\"Content-Length\":\"138475\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:863560a5-4322-400e-ba93-a99516c626b3>\",\"WARC-Concurrent-To\":\"<urn:uuid:e702508f-c63d-4f91-989f-481bcea1bea2>\",\"WARC-IP-Address\":\"151.101.129.69\",\"WARC-Target-URI\":\"https://dsp.stackexchange.com/questions/60355/wrong-estimate-of-lag-by-xcorr-between-a-signal-and-its-segmented-identical-copy\",\"WARC-Payload-Digest\":\"sha1:ONKLTDH6L6E2L4JTLOJEMADNYX3UJNZ5\",\"WARC-Block-Digest\":\"sha1:VRPYO2SCBBAGI7AGRKTLXWJKA4IBN5UP\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2020/CC-MAIN-2020-34/CC-MAIN-2020-34_segments_1596439735939.26_warc_CC-MAIN-20200805094821-20200805124821-00426.warc.gz\"}"}