Dear all, I have two signals which I want to test the correlation of the image by comparing two procedures, the cross correlation and fast fourier transform. I do that in matlab, but I have a problem that I don't know how to use a function correctly. Here is my code; xt = 2*(rand (1024,1)-0.5); yt = rand (1024,1); for i=1:1024; yt(i)=0; end; i=1; for hh=501:532; yt(i)=xt(hh); i=i+1; end; rk = rand (1024,1); for g=1:1024; rk(g) = 0; end; %----------------------------------------------------------------------- %calculation using normal cross correlation. for k=0:992; j=1; for i=k:31+k; rk(k+1) = rk(k+1) + xt(i+1)*yt(j); j = j+1; end; rk(k+1) = rk(k+1)/32; end; for k=993:1023; j=1; d=0; for i=k:31+k; if i<=1023; rk(k+1) = rk(k+1) + xt(i+1)*yt(j); else; rk(k+1) = rk(k+1) + xt(d+1)*yt(j); d=d+1; end; j = j+1; end; rk(k+1) = rk(k+1)/32; end; %----------------------------------------------------------------------- %calculation using FFT correlation. N=32; r1=fft(xt); r2=fft(yt); r3=conj(r2); z=(ifft(r1.*r3))/N; As you can see that I have generated two random signals from -1 to 1 for 1,024 points, one fo xt and another one for yt. Then I copied 501 to 532 from xt into yt at the first 32 points of yt and for other points I set to zero for testing purpose. I tried to calculate the cross correlation by using function 'crosscorr' and 'xcorr' from matlab but neither of these gave me the result of 1024 points as I expected. It gave me the double size (2048 points) instead which I don't know why. So I did the calculation by using equation of cross correlation by theory I have read, which is r(k)= 1/N(Ex(n)y(n-k)) where E=summation of n=0 to N The graph I got from this equation was matched with those I got from FFT that I have done at the end, which should be corrected because I followed the procedure exactly, but I still need to know how to use the matlab function to do the cross correlation since it will save time when I increase the number N points and also for the 2D correlation I won't have to think all of those equation again. I'm doing this for a first time and don't have much knowledge on this, so thanks for any help. regards, sangthong
cross correlation vs FFT
Started by ●December 2, 2007