DSPRelated.com
Forums

cross correlation

Started by sangthong December 7, 2007
I have a problem doing cross corrlation in matlab. The problem is I don't
know how to use the 'xcorr' function correctly. For example, if I have
x=1024 numbers do the crosscorrelation with y= 1024 numbers, the result I
have will be around 2048 numbers with half left of the result is zero, as
it suppost to be only 1024 numbers and give me the peak where I expect
it.

If I do the FFT correlation, I have the correct result for the code beow:

z=ifft(fft(x)*(conj(y)))

This give me 1024 numbers of result with correct position of peak, but it
doesn't work for normal correlation using 'xcorr' function. Could anybody
tell me how to work it out the cross correlation function in matlab so
that the result will be equal to those from the fft correlation (which
think should be correct)?

Thanks,
sangthong
On 8 Des, 01:49, "sangthong" <sangthong2...@yahoo.com> wrote:
> I have a problem doing cross corrlation in matlab. The problem is I don't > know how to use the 'xcorr' function correctly. For example, if I have > x=1024 numbers do the crosscorrelation with y= 1024 numbers, the result I > have will be around 2048 numbers with half left of the result is zero, as > it suppost to be only 1024 numbers and give me the peak where I expect > it. > > If I do the FFT correlation, I have the correct result for the code beow: > > z=ifft(fft(x)*(conj(y))) > > This give me 1024 numbers of result with correct position of peak, but it > doesn't work for normal correlation using 'xcorr' function. Could anybody > tell me how to work it out the cross correlation function in matlab so > that the result will be equal to those from the fft correlation (which > think should be correct)?
The FFT stuff above gives the wrong result, as it suffers from wrap-around effects. Try Nfft= length(x)+length(y)-1; z=ifft(fft(x,Nfft)*(conj(y,Nfft))); instead. Rune
>The FFT stuff above gives the wrong result, as it >suffers from wrap-around effects. Try > >Nfft= length(x)+length(y)-1; >z=ifft(fft(x,Nfft)*(conj(y,Nfft))); > >instead. > >Rune >
Hi, Thanks for your help. I still have a problem that when I tried equation of cross correlation which is R(k)=1/N((sum(n=0,N)x(n)y(n-k))) The result from this equation is exactly the same as those from fft that I have done before, so if I change the fft as you instructed, I will have 2047 numbers which is different from the equation of cross correlation. Could you explain to me what the differences between the algebra equation and the correlation function in matlab? Thanks a lot, Sangthong
On 8 Des, 20:54, "sangthong" <sangthong2...@yahoo.com> wrote:
> >The FFT stuff above gives the wrong result, as it > >suffers from wrap-around effects. Try > > >Nfft= length(x)+length(y)-1; > >z=ifft(fft(x,Nfft)*(conj(y,Nfft))); > > >instead. > > >Rune > > Hi, > > Thanks for your help. I still have a problem that when I tried equation of > cross correlation which is > > R(k)=1/N((sum(n=0,N)x(n)y(n-k))) > > The result from this equation is exactly the same as those from fft that I > have done before, so if I change the fft as you instructed, I will have > 2047 numbers which is different from the equation of cross correlation.
Then you have made the same mistake in two places. If you compute the correlation series between two sequences of lengths N and M, the result is a sequence of length N+M-1. Rune
>Then you have made the same mistake in two places. >If you compute the correlation series between >two sequences of lengths N and M, the result is >a sequence of length N+M-1. > >Rune >
Thanks again. So for example, if I have x=1024 random numbers, y=1024 zero numbers. I then copy some 32 numbers from x, like from 101-132 into the first 1-32 numbers of y. From the theory that I read, I supposed to have the peak at 101 after the correlation. But then if I computed by matlab function, I will have the peak at around 200, right? And the first half of the result is padded to zero, Is this correct? And one more thing, Do I need to adjust the result in order to used it according to the theory? Thank you very much, Sangthong