Strange results with xcorr
30 views (last 30 days)
Show older comments
Hi,
I'm running the following script to calculate the cross-correlation between two signals:
s1 = sig.signals.values(:,1,5);
s2 = ref.signals.values(:,1,5);
C = xcorr(s1,s2);
figure; subplot(2,1,1); plot([s1 s2]); subplot(2,1,2); plot(C);
Unfortunately, instead of the expected result I get maximum cross-correlation on the frame size (1024 samples), like shown in the figure:
Any clue on what's going on?
Thanks, Guilherme
1 Comment
Michael King
on 1 Mar 2019
Did you ever find a suitable solition to your question? I have found that detrending with the detrend function did not help with the issue you presented above that I was also having.
Michael
Answers (3)
Erick Oberstar
on 25 Jul 2018
XCORR wants zero mean inputs - remove the DC offset and I suspect your results will be more inline with what you expect. You may also try passing in the 'unbiased' flag
3 Comments
ES_Thorny
on 6 Dec 2018
I have a similar problem.
I have to compute the cross-correlation between two transient signals with non-zero mean. As they represent transient phenomena, it doesn't make sense to me to subtract the mean value The objective is to compute the time delay between the maximum of the two signals. The signals are not exactly correlated-similar, but I guess this is always the case ...
If i try to compute the time delay using XCORR, I get close results to what I expect (i.e the time delay checked visually by checking where the maximum of the two signals are) only using the UNBIASED options.
Why is that? Does the unbiased routine subtract the mean values from both my signals?
Please let me know what you think.
Regards,
E
Michael King
on 1 Mar 2019
Did you find a suitable solution to your question? I have the same issue that Guilherme T Silva posted on 14 Oct 2016. And I have tried to use the detrend function without success, which I think was suggested by Erick Oberstar on 25 Jul 2018 and Pistachio on 14 Nov 2018
Image Analyst
on 13 Oct 2016
I'm not sure what you were expecting. It looks reasonable to me. What were you expecting? As the signals slide past, they first start to overlaps at -1/2 the frame width, then the overlap gets more and more until they completely overlap, at a shift of the frame width, then the start to separate and the sum of the products decreases until they finally completely separate at 1.5 times the frame width. Then, since MATLAB can't have negative indexes, the shift of -1/2 the frame width will show up at index 1, and the correlation at a shift of 1.5 times the frame width will happen at twice the frame width. Were you expecting something different? Like you wanted to see ripples on the triangle or didn't expect to even see a triangle at all? Please explain what is strange to you.
2 Comments
Image Analyst
on 14 Oct 2016
Try subtracting off the mean before you correlate. The problem is that your signal is on huge offsets, so essentially you're practically convolving two very tall step (rect) functions. And as you know, the convolution (correlation) of two rect functions is a triangle. That's like the first thing they teach when they teach convolution to you.
Honglei Chen
on 14 Oct 2016
You may want to get the lags from xcorr too so you can plot the correlation over lags.
[C,lags] = xcorr(s1,s2);
plot(lags, C)
HTH
0 Comments
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!