xcorr function: want only negative values

조회 수: 10 (최근 30일)
John D'Uva
John D'Uva 2020년 8월 21일
편집: John D'Uva 2020년 8월 21일
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?

채택된 답변

John D'Uva
John D'Uva 2020년 8월 21일
편집: John D'Uva 2020년 8월 21일
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);

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Correlation and Convolution에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by