Algorithm for 'coeff' scaling of xcorr?

20 views (last 30 days)
Alison
Alison on 11 Apr 2011
Edited: Dostdar on 11 Jul 2018
I am cross-correlating some vectors using xcorr, and need to scale the cross-correlation values from 0 to 1. Conceptually xcorr lets you do that with the scaling option 'coeff', but it does not work if the vectors are of different length; the vectors with which I am working are, unfortunately, of different lengths.
I know that the documentation says that when the 'coeff' scaling is used, it "normalizes the sequence so the autocorrelations at zero lag are identically 1.0". Conceptually, I could just manually divide all of my cross-correlation values by the autocorrelation... except the problem is, when I look at vectors of the same length (to understand how the 'coeff' calculation is occurring), I don't know what is being used as the "autocorelation". It isn't the autocorrelation of either vector individually, and doesn't seem to be any obvious calculation therein. For a simplistic example that demonstrated the problem, if I have two vectors a and b:
a = [1,2,3]
b = [3,4,5]
The regular cross-correlation with xcorr, xcorr(a,b) gives:
5 14 26 18 9
And when I scale it with xcorr(a,b,'coeff'), I get:
0.1890 0.5292 0.9827 0.6803 0.3402
All of the numbers have been divided by approximately 26.46, and I have NO idea where that number came from. If I did, it might let me manually scale all of my cross-correlations to zero. Does anyone know what goes into the calculation?
As an aside, since I know that when vectors are of different length, xcorr pads them with zeros when normally cross-correlating, I thought that I might manually pad my vectors with zeros as needed, and then use xcorr's 'coeff' function. After doing this, however, I think that the zero padding affects the calculation...
So, in short, I need to cross-correlate vectors of different lengths and scale the cross-correlation values between 0 and 1. Any help would be awesome. Thanks.
  1 Comment
Alison
Alison on 11 Apr 2011
There is always a chance that my manual zero-padding didn't actually mess up the calculation (I haven't verified for sure yet), but verifying the algorithm that MATLAB normally uses to do the 'coeff' normalization would confirm whether or not it was a reasonable method. Thanks again.

Sign in to comment.

Accepted Answer

Mike Hosea
Mike Hosea on 12 Apr 2011
xcorr(a,b,'coeff') = xcorr(a,b)/(norm(a)*norm(b))
except that I think xcorr with the 'coeff' option actually computes norm(a)*norm(b) in a slightly different way (i.e., as sqrt(sum(abs(a).^2)*sum(abs(b).^2))). -- Mike
  3 Comments
Alison
Alison on 12 Apr 2011
How did you know, by the way?
Mike Hosea
Mike Hosea on 12 Apr 2011
:) I work at MathWorks adapting functions like that for code generation, and I did that one several years ago. I refreshed my memory by typing "edit xcorr" at the command line, putting a breakpoint at the top of the file, calling xcorr with the coeff option, and stepping through the code.

Sign in to comment.

More Answers (1)

Dostdar
Dostdar on 11 Jul 2018
Edited: Dostdar on 11 Jul 2018
Hi Mike, How to use 'coeff' if the two matrix (A,B) have the different size of length?

Products

Community Treasure Hunt

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

Start Hunting!