corrcoef & xcorr

Hello i've two complex functions ( size 1x1x2501) and i need to do a correlation between these (cross-correlation).
i've tried to use this command:
r=corrcoef(Hmimo_tb(1,:)',Hmimo_tb1(1,:)','coeff');
where Hmimo_tb and Hmimo_tb1 are my two signals in which the only difference is the fact that they have been measured in different positions. The difference betweeen these two signals is max equal to 1.5e-13, so they are only affected by noise.
i obtain as result:
ans =
1.0000 1.0000 + 0.0000i 1.0000 - 0.0000i 1.0000
the function that i'm going to correlate are complex but the 0.0000i leave me some doubts.... Another doubt is the fact that the the signals are not equal in fact as i've told before there is a difference of 1.5e-13 that is not reported on the secondary diagonal why?
what are the difference between corrcoef and xcorr?

 Accepted Answer

Wayne King
Wayne King on 10 Nov 2011
Salvatore, corrcoef() is not the cross correlation sequence. It does not shift one vector with respect to the other.
x = cos(pi/4*n);
y = cos(pi/4*n-(3*pi)/4);
[r,p] = corrcoef(x,y);
But
[c,lags] = xcorr(y,x,'coeff');
[maxcorr,I] = max(c);
lags(I)
You see that if you allow for shifts then y and x are perfectly correlated and that happens at lag 3, which makes perfect sense since the frequency of x and y is pi/4 radians/sample and y is shifted (3*pi)/4 radians.
Now, note for
lags(length(x))
c(length(x))
This is exactly equal to r in [r,p] = corrcoef(x,y);

7 Comments

wait i'ven't understood well.... let's analyze step by step:
x = cos(pi/4*n);
y = cos(pi/4*n-(3*pi)/4);
[r,p] = corrcoef(x,y);
i've created a n=0:1:100 and i obtain as result:
r =
1.0000 -0.7108
-0.7108 1.0000
p =
1.0000 0.0000
0.0000 1.0000
Instead:
[c,lags] = xcorr(y,x,'coeff');
[maxcorr,I] = max(c);
lags(I)
gives me as result 3, what does this mean?
lags(length(x))-->result 0
c(length(x))-->result -0.7106
i'ven't understood the point.
my problem with the correlation of xcorr is that it gives a lot of values in the variable "c" . What i need is a a "single value" , and xcorr gives me a bunch of values. That's because it is calculating the cross-correlation for all the possible array offsets- kind of like a convolution.
the question that i want to ask you is: from this bunch of values how can i be sure that the 2 measure/functions are correlated?for the fact that there is in one of the lags, the one centered in the phase 0, the result 1? if yes all the other lags are negligible?
again thank you for you replies...i know i'm hard to understand ^^
Wayne King
Wayne King on 10 Nov 2011
Salvatore, corrcoef() is the same as xcorr() at lag =0. When one signal is not shifted with respect to the other. xcorr() shifts the signal and then correlates. In the example I gave you, the signals are perfectly correlated if you shift one by three samples. That's what the 3 tells you.
corrcoef() only tells you what the correlation is if you take the vectors as they are WITHOUT shifting one of them with respect to the other.
ok but explain me why i have to shift the signal?
i have 6 measure syncronized i only want to know if they are correlated and how much they are near to be. in what i wrong if i take only the value at lag=0?
Wayne King
Wayne King on 10 Nov 2011
There is nothing wrong per se. It depends on your application. xcorr() tells you the delay between two signals. That can be very important information. corrcoef() does not tell you that.
ok great!let me understand the last thing for delay you mean a shift of the phase? and if yes how can i recognize it?
Wayne King
Wayne King on 10 Nov 2011
It can mean a phase shift. It depends on the nature of the signals whether it is more natural to view it as a phase shift or just a delay. If the signals are sine waves, I think it is more natural to think of it as a phase shift. Have you tried to understand my examples??? I've shown you a number of example where you find the delay in by the peak in the cross correlation.
ok so i understand that the results different from the lag-0 have a non-clear meaning or bytheway what i want to study deeper is if for example the xcorr between M1(measure 1) and M2 gives in the lags=1000 c=0.002 how much shifted is respect the c=0.003 in the lags=1000 of the xcorr between M1 and M3?

Sign in to comment.

More Answers (2)

Walter Roberson
Walter Roberson on 10 Nov 2011

1 vote

0.0000i implies that there is a non-zero complex component which is too small to be represented using your current display format (which is probably "format short f")

6 Comments

how can i change the display format?
Walter Roberson
Walter Roberson on 10 Nov 2011
format long g
Column 1
1
1 - 3.73593550274545e-11i
Column 2
1 + 3.73593550274545e-11i
1
it works.
the fact that on the secondary diagonal i have a complex value means that the two signals have different phase or what?
Walter Roberson
Walter Roberson on 10 Nov 2011
With values that small, it could indicate round-off error.
mmm well consider that also in the positive value there is a round-off error in my opinion in surplus because the two measurements are not equal (but near to be). does exist a way to not have this approximation?

Sign in to comment.

Salvatore Turino
Salvatore Turino on 11 Nov 2011
Wayne i've tried your code
x = cos(pi/4*n);
y = cos(pi/4*n-(3*pi)/4);
[r,p] = corrcoef(x,y);
[c,lags] = xcorr(y,x,'coeff');
[maxcorr,I] = max(c);
lags(I)
but i'm on trouble. i've set n=0:1:100 and as you say i have as result 3. you say that those functions are correlated at lags 3 but watching the "c" if lags 3 correspond to c(:,3) i have this result: -0.0139333076031825
so why do you say that they are perfectly correlated?

1 Comment

Wayne King
Wayne King on 11 Nov 2011
Salvatore, you keep making this mistake. c(3) is not at lag three. You are forgetting about the negative lags. If you enter lags(3) for the example you have above, you see that c(3) is the value of the cross correlation sequence at lag -98. c(104) is the cross correlation sequece at lag 3. That value is very close to 1.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!