# cross correlation implemenation

Started by May 5, 2006
```For starters, I hope I'm not off topic.

I'm looking for a C/C++ cross correlation implemention.   Something
akin to matlab xcorr.  I'm perusing the matlab source for xcorr and -
it's so say the least - intimidating.  Trying to convert my matlab
source to C++ but would hate to re-invent the wheel if a sound
implementation of these exists someplace.

```
```<forums_mp@hotmail.com> wrote in message
>
> For starters, I hope I'm not off topic.
>
> I'm looking for a C/C++ cross correlation implemention.   Something
> akin to matlab xcorr.  I'm perusing the matlab source for xcorr and -
> it's so say the least - intimidating.  Trying to convert my matlab
> source to C++ but would hate to re-invent the wheel if a sound
> implementation of these exists someplace.
>
>

Enjoy:

Call with two vectors of data in x and y. Let z point to memory for result.
Allocate length_x + length_y-1 locations. lenx and leny are the respective
lengths of data.

// performs straight convolution by Clay S. Turner
// for correlattion just reverse the order of sequence y;

ConvolveFunction(double *z, double *x, double *y, int lenx, int leny)
{
double *zptr,s,*xp,*yp;
int lenz;
int i,n,n_lo,n_hi;

lenz=lenx+leny-1;
zptr=z;

for (i=0;i<lenz;i++) {
s=0.0;
n_lo=0>(i-leny+1)?0:i-leny+1;
n_hi=lenx-1<i?lenx-1:i;
xp=x+n_lo;
yp=y+i-n_lo;
for (n=n_lo;n<=n_hi;n++) {
s+=*xp * *yp;
xp++;
yp--;
}
*zptr=s;
zptr++;
}
}

```