Can i get Pearson R value out of xcorr()

10 views (last 30 days)
I am trying to compare vectors that sample across two spatial fields. One is divergence in wind field. Other is rainfall intensity. There appears to be a correlation in the two fields but with a notable spatial lag between peak value in one sample vector and peak value in the other. We want to quantify this and xcorr would seem to be the logical choice.
If I am interpreting the docs for xcorr correctly, it gives us the cross correlation between the two curves at every possible offset. In our case, the offset is spatial vs. temporal, but that should not matter?
The result we generate is confusing. We do get a reasonable lag value from xcorr, but the correlation values do not make sense to us. In particular, they are not the expected Pearson's R values in range between -1 and 1.
As a baseline test we run xcorr comparing one of our curves to itself. We do get the expected 0 value for lag, but the correlation value generated at zero lag is also near zero. In that case what we'd expect would be perfect correlation of 1.0.
When we run the same tests using the MATLAB corr and corrcoef functions, we do get the expected 1.0 value. We could use either of those functions for our testing, but neither accounts for the spatial lag in our input vectors.
So the questions: 1) what is xcorr generating for its correlation values, and 2) is there some way to get xcorr to generate standard Pearson Product Moment Correlation (R) values?
Thanks,
Mike Wilson

Accepted Answer

Wayne King
Wayne King on 15 May 2016
Hi Mike, if the vectors are the same length, then you can use the 'coeff' option to give you values between -1 and 1
Ex:
dt = 0.001;
t = 0:dt:2;
x = cos(2*pi*100*t)+0.1*randn(size(t));
y = cos(2*pi*100*t-pi)+0.05*randn(size(t));
[xc,lags] = xcorr(x,y,200,'coeff');
stem(lags(201:end),xc(201:end))
If you are dealing with zero-mean signals (remove the mean), then you will find that the value of xcorr() with the 'coeff' option at zero lag is very close to the result obtained with say, corr(), in MATLAB.
x = randn(16,1);
y = randn(16,1);
x = detrend(x,0);
y = detrend(y,0);
[xc,lags] = xcorr(x,y,1,'coeff');
xc(2)
rho = corr(x,y);
rho
  1 Comment
Mike Wilson
Mike Wilson on 15 May 2016
Excellent! Yes, vectors are same length. My tests of the 'coeff' option were early on and I did not interpret the result correctly. I thought I was getting a normalized value (forced to max of 1.0).
Thanks very much Wayne.

Sign in to comment.

More Answers (0)

Products

Community Treasure Hunt

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

Start Hunting!