zero lag when using xcorr on 2 near-similar, shifted timeseries
4 views (last 30 days)
Show older comments
Hi all,
I try to cross correlate two signals which are from 2 different instruments.
I use xcorr to find the lag between signal A (blue) and signal B (red). However, I keep getting 0 lag which should not be the case. As can be seen from the figure, the lag should be around ~ 110 where it gives a quit nice fit.
Attached are the signals A & B.
I'm using xcorr as the following:
[c,lags] = xcorr(A,B);
Thanks in advance for you help.
Kind regards,
Jos
Answers (1)
dpb
on 22 Sep 2023
Edited: dpb
on 26 Sep 2023
load A, load B
whos
subplot(2,1,1)
plot(ADV_sampled)
hold on
plot(PIV_mag_test)
xlim([300 inf])
subplot(2,1,2)
plot(ADV_sampled)
hold on
plot(PIV_mag_test)
xlim([1 200])
legend('B','A','location','northwest')
The two are perfectly in phase over almost 90% of the total signal length; that the lag between the two is zero is what I would expect. From about sample 80 on, the peaks align almost perfectly.
Only the ramp up stage of B before any regular fluctuating behavior was achieved won't correlate highly, and there there simply isnt any significant similar behavior yet to even try to compare. I suppose B is somehow driven by A and you're trying to find where the two become phase locked, maybe?
The only area that appears to have a small phase shift is that A seems to lag B some in that initial startup section between 100 and 200+ samples, but other than within that small segment of the overall signal, there's simply so little lag that a lag shift by even one sample would appear to make the overall worse -- which is what xcorr() is telling you.
A subset section might show something...
2 Comments
dpb
on 26 Sep 2023
Edited: dpb
on 26 Sep 2023
There simply isn't a phase lag to speak of anywhere there's a defined sinsoidal component so I don't think correlation is the tool that will help.
Making the best first match to the amplitude of the initial peak in A by subsequent peaks in B, might serve to give a way to align them; that might then need a bit of a lag adjustment after the initial guess.
With some creativity, it should be possible to use lsqnonlin to minimize the error between the two traces by adjusting the offset of B relative to A as the parameter to solve for -- the error function would have to account for the changing length for B as it is shifted in some manner; updating the indices to match lengths between the two as pieces are lopped off the front of B.
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!