Clear Filters
Clear Filters

xcorr function: want only negative values

20 views (last 30 days)
Hi all,
I have two signals as shown in the attached picture. When I calculate the cross correlation with xcorr, it will sometimes calculate the shift to the left and sometimes to the right. I want the lag to always be negative (so only shifted one direction because the ground truth is that the blue signal always occurs first in real time). I know that there is a 'MAXLAG' option, but it computes the correlation over the range of lags -MAXLAG to MAXLAG (so won't work for my purposes).
In essence, I want the red line to always shift to the left when showing the correction (i.e. the output of the highest correlation), as opposed to the right as shown in the attached image. Does anyone know if this is possible?

Accepted Answer

John D'Uva
John D'Uva on 21 Aug 2020
Edited: John D'Uva on 21 Aug 2020
For anyone interested, I ended up solving this myself with the help of a coworker. Just cut results at 0 to only show negatives:
[c, lags] = xcorr(zscore(v1), zscore(v2), 25, 'coeff');
% cut results by half
lags = lags(lags<0);
c = c(lags<0);
% Get the index of the max value and use it to find the (now more precise) lag
[~,index] = max(c);
t = lags(index);

More Answers (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!