# Corelation problem in matlab.

Status
Not open for further replies.

#### syedshan

Dear all,

I am was not convinced or may be could not understand the correlation in matlab i.e. xcorr function.
So to test I simple made two exactly similar sine wave without any delay, and it gives me delay of 21.
I cannot understand why. please first see the code below

Code:
fo=200; fs=2500;
t=0:1/fs:.008;
x=sin(2*pi*fo*t);
y=x; %now x and y are exactly the same

%correlation start here
xc = xcorr(x,y);
[a,delay]=max(xc);
subplot(3,1,1); plot(t,x); %xlabel(t);
subplot(3,1,2); plot(t,y);
subplot(3,1,3); plot(xc);

display(sprintf('maximum of xc  is %d',max(xc)));
display(sprintf('delay is %d',delay));

So I get the delay at the index 21, where I think we should not have any delay. Note that the x and y both have 21 elements.
What I understand is, may be matlab implicitly padd zeros to the second element, in this case y,

Also the please clarify one more thing that the maximim value in the vector 'xc' indicates that at this point the peaks of the two points come together right...!

Shan

You can use correlation to compare the similarity of two sets of data. Correlation computes a measure of similarity of two input signals as they are shifted by one another. The correlation result reaches a maximum at the time when the two signals match best. If the two signals are identical, this maximum is reached at t = 0 (no delay). If the two signals have similar shapes but one is delayed in time and possibly has noise added to it then correlation is a good method to measure that delay.

[xc,lag] = xcorr(x,'coeff');
[a,delay]=max(xc);
subplot(3,1,1); plot(t,x); %xlabel(t);
subplot(3,1,2); plot(t,y);
subplot(3,1,3); plot(lag,xc);

Status
Not open for further replies.