# cross correlation

Started by June 29, 2005
```Hi expert,
I used xcorr to calculate cross correlation in matlab, i tried to do the
same thing by using fft, so :
i have 2 signals x, y
L=max(lenght(x), lenght(y))
xx=fft(x,2*L).
yy=fft(y,2*L).
Sxy=xx.*conj(y);
rxy=fftshift(real(ifft(Sxy))) ;
Rxy is my cross correlation,the problem is when i drawed rxy and
xcorr(x,y)

i dont find equality between rxy et xcorr, where is the problem
Thanks a lot to answer at my question.

This message was sent using the Comp.DSP web interface on
www.DSPRelated.com
```
```
I don't have xcorr available, so I don't know the detail of
how it works, but here are some suggestions of what to look
for.

> I used xcorr to calculate cross correlation in matlab, i tried to do the
> same thing by using fft, so :
> i have 2 signals x, y
> L=max(lenght(x), lenght(y))

Make sure to use the signal the same length as xcorr does.
I wouldn't be surprised if it is something along the lines of

L=length(x)+2*(length(y)-1);

> xx=fft(x,2*L).
> yy=fft(y,2*L).

Make sure to use the same FFT length as xcorr does. Again,
I wouldn't be surprised if it is something along the lines of

Nfft = length(x)+2*(length(y)-1);

Remember, no symmetry can be expected for cross correlation.

> Sxy=xx.*conj(y);

Could it be that the order of the conjugates are different?

> rxy=fftshift(real(ifft(Sxy))) ;

Does xcorr shift the data so that zero lag ends up in the middle?
If so, how does it handle the different lengths of x and y?

> Rxy is my cross correlation,the problem is when i drawed rxy and
> xcorr(x,y)

Rune

```
```signal wrote:

> Sxy=xx.*conj(y);

Sxy=xx.*conj(yy);
```
```signal wrote:
> Sxy=xx.*conj(y);

Should that be Sxy=xx.*conj(yy);

??

--
Jim Thomas            Principal Applications Engineer  Bittware, Inc
jthomas@bittware.com  http://www.bittware.com    (603) 226-0404 x536
Any sufficiently advanced technology is indistinguishable from magic.
- Arthur C. Clarke
```
```Hi,
thanks for your response.
This is a typing error, i used sxy=xx.*conj(yy).

>signal wrote:
>> Sxy=xx.*conj(y);
>
>Should that be Sxy=xx.*conj(yy);
>
>??
>
>--
>Jim Thomas            Principal Applications Engineer  Bittware, Inc
>jthomas@bittware.com  http://www.bittware.com    (603) 226-0404 x536
>Any sufficiently advanced technology is indistinguishable from magic.
>   - Arthur C. Clarke
>

This message was sent using the Comp.DSP web interface on
www.DSPRelated.com
```
```Hi,
thanks for your response.
This is a typing error, i used sxy=xx.*conj(yy).

>signal wrote:
>> Sxy=xx.*conj(y);
>
>Should that be Sxy=xx.*conj(yy);
>
>??
>
>--
>Jim Thomas            Principal Applications Engineer  Bittware, Inc
>jthomas@bittware.com  http://www.bittware.com    (603) 226-0404 x536
>Any sufficiently advanced technology is indistinguishable from magic.
>   - Arthur C. Clarke
>

This message was sent using the Comp.DSP web interface on
www.DSPRelated.com
```
```Hello,

when I plotted  two curves, I mean xcorr and rxy , I found same pace, but
one of the curves is shifted and out of phase with an angle of 180
compared to the other

>Hi,
>thanks for your response.
>This is a typing error, i used sxy=xx.*conj(yy).
>
>
>>signal wrote:
>>> Sxy=xx.*conj(y);
>>
>>Should that be Sxy=xx.*conj(yy);
>>
>>??
>>
>>--
>>Jim Thomas            Principal Applications Engineer  Bittware, Inc
>>jthomas@bittware.com  http://www.bittware.com    (603) 226-0404 x536
>>Any sufficiently advanced technology is indistinguishable from magic.
>>   - Arthur C. Clarke
>>
>
>
>
>This message was sent using the Comp.DSP web interface on
>www.DSPRelated.com
>

This message was sent using the Comp.DSP web interface on
www.DSPRelated.com
```
```signal wrote:
> Hello,
>
> when I plotted  two curves, I mean xcorr and rxy , I found same pace, but
> one of the curves is shifted and out of phase with an angle of 180
> compared to the other

Perhaps, along the way, you missed an odd number of minus signs.

...

Jerry
--
Engineering is the art of making what you want from things you can get.
&#2013266095;&#2013266095;&#2013266095;&#2013266095;&#2013266095;&#2013266095;&#2013266095;&#2013266095;&#2013266095;&#2013266095;&#2013266095;&#2013266095;&#2013266095;&#2013266095;&#2013266095;&#2013266095;&#2013266095;&#2013266095;&#2013266095;&#2013266095;&#2013266095;&#2013266095;&#2013266095;&#2013266095;&#2013266095;&#2013266095;&#2013266095;&#2013266095;&#2013266095;&#2013266095;&#2013266095;&#2013266095;&#2013266095;&#2013266095;&#2013266095;&#2013266095;&#2013266095;&#2013266095;&#2013266095;&#2013266095;&#2013266095;&#2013266095;&#2013266095;&#2013266095;&#2013266095;&#2013266095;&#2013266095;&#2013266095;&#2013266095;&#2013266095;&#2013266095;&#2013266095;&#2013266095;&#2013266095;&#2013266095;&#2013266095;&#2013266095;&#2013266095;&#2013266095;&#2013266095;&#2013266095;&#2013266095;&#2013266095;&#2013266095;&#2013266095;&#2013266095;&#2013266095;&#2013266095;&#2013266095;&#2013266095;&#2013266095;
```
```Jerry,
Please, could you explain more, above i writted my method.
thanks

>signal wrote:
>> Hello,
>>
>> when I plotted  two curves, I mean xcorr and rxy , I found same pace,
but
>> one of the curves is shifted and out of phase with an angle of 180
>> compared to the other
>
>Perhaps, along the way, you missed an odd number of minus signs.
>
>   ...
>
>Jerry
>--
>Engineering is the art of making what you want from things you can get.
>&#2013266095;&#2013266095;&#2013266095;&#2013266095;&#2013266095;&#2013266095;&#2013266095;&#2013266095;&#2013266095;&#2013266095;&#2013266095;&#2013266095;&#2013266095;&#2013266095;&#2013266095;&#2013266095;&#2013266095;&#2013266095;&#2013266095;&#2013266095;&#2013266095;&#2013266095;&#2013266095;&#2013266095;&#2013266095;&#2013266095;&#2013266095;&#2013266095;&#2013266095;&#2013266095;&#2013266095;&#2013266095;&#2013266095;&#2013266095;&#2013266095;&#2013266095;&#2013266095;&#2013266095;&#2013266095;&#2013266095;&#2013266095;&#2013266095;&#2013266095;&#2013266095;&#2013266095;&#2013266095;&#2013266095;&#2013266095;&#2013266095;&#2013266095;&#2013266095;&#2013266095;&#2013266095;&#2013266095;&#2013266095;&#2013266095;&#2013266095;&#2013266095;&#2013266095;&#2013266095;&#2013266095;&#2013266095;&#2013266095;&#2013266095;&#2013266095;&#2013266095;&#2013266095;&#2013266095;&#2013266095;&#2013266095;&#2013266095;
>

This message was sent using the Comp.DSP web interface on
www.DSPRelated.com
```
```signal wrote:
> Jerry,
> Please, could you explain more, above i writted my method.
> thanks
>
>
>
>>signal wrote:
>>
>>>Hello,
>>>
>>>when I plotted  two curves, I mean xcorr and rxy , I found same pace,
>
> but
>
>>>one of the curves is shifted and out of phase with an angle of 180
>>>compared to the other
>>
>>Perhaps, along the way, you missed an odd number of minus signs.

My remark was intended to me a humorous way to point out that a
180-degree phase shift and a negation are the same. I'm sorry that the
joke fell flat.

Jerry
--
Engineering is the art of making what you want from things you can get.
&#2013266095;&#2013266095;&#2013266095;&#2013266095;&#2013266095;&#2013266095;&#2013266095;&#2013266095;&#2013266095;&#2013266095;&#2013266095;&#2013266095;&#2013266095;&#2013266095;&#2013266095;&#2013266095;&#2013266095;&#2013266095;&#2013266095;&#2013266095;&#2013266095;&#2013266095;&#2013266095;&#2013266095;&#2013266095;&#2013266095;&#2013266095;&#2013266095;&#2013266095;&#2013266095;&#2013266095;&#2013266095;&#2013266095;&#2013266095;&#2013266095;&#2013266095;&#2013266095;&#2013266095;&#2013266095;&#2013266095;&#2013266095;&#2013266095;&#2013266095;&#2013266095;&#2013266095;&#2013266095;&#2013266095;&#2013266095;&#2013266095;&#2013266095;&#2013266095;&#2013266095;&#2013266095;&#2013266095;&#2013266095;&#2013266095;&#2013266095;&#2013266095;&#2013266095;&#2013266095;&#2013266095;&#2013266095;&#2013266095;&#2013266095;&#2013266095;&#2013266095;&#2013266095;&#2013266095;&#2013266095;&#2013266095;&#2013266095;
```