cross-correlation of two signal always shows unexpected 0 lag

조회 수: 29 (최근 30일)
YANAN ZHU
YANAN ZHU 2018년 11월 20일
댓글: Honglei Chen 2018년 11월 20일
Hi,everyone, I got a very strange issue.
b is a signal of 251 samples. c is got by shifting b by 5 sample. (b and c are attached).
Image 5.png
But when I do xcorr on b and c, the max correlation always occurs at 0 lag. which I do not understand. Can someone help?
Below is my code. Thank you.
load b.mat
load c.mat
figure;
plot(b);hold on;
plot(c);
[r,lag] = xcorr(b,c,'coeff');
[m, i_max]=max(r);
shift=lag(i_max);
% plot(lag,r);
display(shift);

채택된 답변

Honglei Chen
Honglei Chen 2018년 11월 20일
You may want to try
[r,lag] = xcorr(b-mean(b),c-mean(c),'coeff');
HTH
  댓글 수: 2
YANAN ZHU
YANAN ZHU 2018년 11월 20일
Thank you so much! It works! Can I know what really happened under the hood after subtracting the mean?
Honglei Chen
Honglei Chen 2018년 11월 20일
Since your goal is to look for similarity between two signals, I think in theory the cross correlation really requires both signal to be zero mean. Otherwise, the DC component will affect your result. For example, let's say two signals are uncorrelated and have all positive values. In theory, the correlation coefficient should be 0. However, without removing the means, the result will always be positive.
HTH

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

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

제품


릴리스

R2018a

Community Treasure Hunt

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

Start Hunting!

Translated by