2
$\begingroup$

I am trying to implement Matlab's cross correlation function but I can't seem to replicate the results obtained when using Matlab's built in function "xcorr".

Documentation for xcorr can be found HERE.

Looking at the Matlab documentation it seems as though the cross correlation is implemented as

$$ R_{xy}[m] = \left.\begin{cases} \sum_{n=0}^{N - m - 1} x[n + m] y[n] & m \geq 0\\ \\ R_{yx}[-m] & m < 0\\ \end{cases} \right\} $$

Or equivalently as

$$ R_{xy}[m] = \left.\begin{cases} \sum_{n=1}^{N - m} x[n + m] y[n] & m \geq 0\\ \\ \sum_{n=1}^{N + m} y[n - m] x[n] & m < 0\\ \end{cases} \right\} $$

if x and y have indexes starting at 1.The result will then be C, where

$$ C(m) = R_{xy}(m - N) \hspace{1cm} \text{for} \hspace{1cm} m = 1, 2,...,2N - 1. $$

The implementation of this formula in Matlab is shown below:

function [result] = myXCorr(A , B)
%Implementation of the Cross Coorelation Function

N = size(A,2);
M = size(B,2);
result = zeros(1, N + M - 1 );
len = size(result,2);

for m = 1 : len  
   arg = (m - N); 

   if(arg < 0)
       negativeCondition = 1;
       limit = N + arg;
   else
       negativeCondition = 0;
       limit = N - arg;
   end

   for n = 1:limit
           if(negativeCondition == 0)
            result(m) = result(m) + A(arg + n) + B(n);
           else
            result(m) = result(m) + A(n) + B(n - arg);
           end
   end
end

end

This implementation does not produce the same results as Matlab and I can't seem to find out what the error is.The plots of the autocorrelation's are shown below.

A = 1:1:10;
B = 1:1:10;

myResult = myXCorr(A,B);
mResult = xcorr(A,B);

lag = -9:1:9;

plot(lag, myResult);
plot(lag, mResult);

Autocorrelation using my function:

myXCorr.jpg

Autocorrelation using "xcorr":

xcorr.jpg

Does anyone know what 'm doing wrong here? Any help will be appreciated.

$\endgroup$
3
  • $\begingroup$ Do you succeed to find the correct matlab for this ? I really need this :D $\endgroup$
    – Juan
    Jan 10, 2017 at 11:50
  • $\begingroup$ The accepted answer is correct, I added the A and B values instead of multiplying them. $\endgroup$
    – KillaKem
    Jan 10, 2017 at 12:15
  • $\begingroup$ Ok thank you, its working now, but I want to create a function like xcorr (= cross- and auto-correlation). And so with this function, I want to be able to make the cross correlation when two inputs vectors are used (x,y) (This part is ok with your program) but I also want to make the auto-correlation if only one vector is present in the list of arguments... Do you know if its possible ? $\endgroup$
    – Juan
    Jan 10, 2017 at 13:52

2 Answers 2

3
$\begingroup$

In the loop where you compute your result vector, you should be multiplying the A and B values instead of adding them up.

$\endgroup$
1
  • $\begingroup$ Nice catch! Upvoted. $\endgroup$ Mar 29, 2020 at 3:36
1
$\begingroup$
for n = 1:limit
      if (negativeCondition==0)
        result(m) = result(m) + A(n) * B(n - arg);
      else
        result(m) = result(m) + A(arg + n) * B(n);
      end
end
$\endgroup$
1
  • 2
    $\begingroup$ Welcome to DSP SE. Generally code-only answers are considered low quality. Any chance of elaborating and explaining? $\endgroup$
    – jojeck
    Feb 7, 2017 at 9:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.