Forums

Cross correlation

Started by vakilp May 31, 2006
Hello,

I have a problem with my C code. My algorithm might be wrong but when I
work with fewer samples, I get convinced that I am right.

In C I have two arrays: Data and Code. They are both array of structs
(both are array of imaginary numbers).
Now, according to my shorter calculations, if I keep Data constant and
shift only Code array or vice versa, I ought to end up with the cross
correlation values that I would by putting these two arrays of numbers in
MATLAB albeit that the highest value of the cross correlation will not be
in the same spot as MATLAB's.

However, when I plot cross correlation from my code and that from MATLAB's
xcorr function, mine is wrong. Clearly I am doing something wrong.

Let me give you some code here:

% Code is a circular array and hence we have the begin and end indecies.
Each Data and Code are 15999 elements long. Each element of array holds
two doubles. One is real and one is imaginary %

%Currently begin = 0 and end = 15998%
void Cross_Correlate(being, end, index){ 
j = 0;
while (begin != end){
  cross[index].real = Code[begin].real*Data[j].real -
Code[being].imag*Data[j].imag;

  cross[index].imag = Code[being].imag*Data[j].real +  
Code[being].real*Data[j].imag;

  begin = (begin+1) % 15999;
  }
}

The Cross_Correlate function is called from main and I increment end as:

end = (end+1) % 15999

Basically, in iteration 2, end = 0 and begin = 1. So I will be multiplying
Code[1] to Data[0], Code[2] to Data[1] and so on. Keep iterating end until
end = 15997. So in this last case we have Code[15998]*Data[0],
Code[0]*Data[1] and so on. I am doing something wrong here. Help please?

Thanks.


vakilp wrote:

> > Let me give you some code here: > > % Code is a circular array and hence we have the begin and end indecies. > Each Data and Code are 15999 elements long. Each element of array holds > two doubles. One is real and one is imaginary % > > %Currently begin = 0 and end = 15998% > void Cross_Correlate(being, end, index){ > j = 0; > while (begin != end){ > cross[index].real = Code[begin].real*Data[j].real - > Code[being].imag*Data[j].imag; > > cross[index].imag = Code[being].imag*Data[j].real + > Code[being].real*Data[j].imag; > > begin = (begin+1) % 15999; > } > } >
Did you copy and paste this code ? Only there are a couple of typos in it, where you have "being" instead of "begin", which might lead to some unexpected behaviour. Paul
>vakilp wrote: > >> >> Let me give you some code here: >> >> % Code is a circular array and hence we have the begin and end
indecies.
>> Each Data and Code are 15999 elements long. Each element of array
holds
>> two doubles. One is real and one is imaginary % >> >> %Currently begin = 0 and end = 15998% >> void Cross_Correlate(being, end, index){ >> j = 0; >> while (begin != end){ >> cross[index].real = Code[begin].real*Data[j].real - >> Code[being].imag*Data[j].imag; >> >> cross[index].imag = Code[being].imag*Data[j].real + >> Code[being].real*Data[j].imag; >> >> begin = (begin+1) % 15999; >> } >> } >> > >Did you copy and paste this code ? Only there are a couple of typos in >it, where you have "being" instead of "begin", which might lead to some >unexpected behaviour. > >Paul >
Hey Paul, I was infact typing up the code so that infact is a typo. My code compiles and runs fine. I guess my question is more algorithmic than code related. I am shifting only one of the arrays and not both. My thought is that it should work. But it doesnt! Thanks.