# Applying Circular Cross Correlation in MATLAB

I have searched Google for circular cross correlation using Matlab, and I have found it only for one dimensional signals.

Cross correlation can be implemented in the frequency domain using FFT by multiplying signals.
For a cyclic cross correlation you can use:

cc = ifft(fft(x).*conj(fft(y)));

You can also calculate the linear cross correlation using FFT by zero-padding the signals before the FFT.
Related: Cross correlation with FFT and fftshift

Edit:
In order to calculate the cross correlation of two images you have 2 options:

1. x and y should be the grayscale representation of the images
2. Repeat the process 3 times. Each time x and y are a single channel (r,g or b) from each image
• using Matlab, x will be the matrix of the first image and y the matrix of the second image?
– Noha
Nov 14, 2020 at 18:48
• It depends. See the edit
– ThP
Nov 14, 2020 at 19:00
• It might be safe to 1) check that the imaginary part is negligible 2) use the real part of the result them. Nov 14, 2020 at 19:46
• function [ h ] = cxcorr_fft( a,b ) %CXCORR_FFT Calculates the circular crosscorrelation of the two input % vectors using the fft based method % calculate crosscorrelation e = fft2(a); f = fft2(b); g = f.*conj(e); h = fftshift(ifft2(g)); end
– Noha
Nov 20, 2020 at 11:33
• I have used the CXCORR_FFT function in matlab, which is used to find the circular cross correlation for two vectors. I have replaced the fft function by the fft2 function, as you told me. The code is in the previous comment. Unfortunately, the results are not acceptable. The value of correlation between an image and its self is approximately the same as the value of the correlation between an image and a different image !!!
– Noha
Nov 20, 2020 at 11:38

For 1D signals MATLAB has the function cconv().
In order to apply correlation and not convolution just flip the signals:

c = cconv(a, b(end:-1:1));


For 2D, in case you the Image Processing toolbox you may use imfilter(). Its default mode is correlation:

c = imfilter(a2D, b2D);
c = imfilter(a2D, b2D, 'corr'); % Equals to the above