# Discrepancy between xcorr2 and fft2

13 ビュー (過去 30 日間)
Alessandro Masullo 2015 年 10 月 15 日

Hello!
I want to implement an fft cross-correlation in my algorithm in order to replace the much slower xcorr2. In theory, direct cross-correlation (xcorr2) and product of fft transform should be exactly the same. In practice, results can be different if the matrix size is not a power of two, because of the zero padding.
I made this snippet of code for a comparison, where matrices are exactly 128. The strange thing is that the two methods are almost the same in the centre of the correlation, but they differ quite a lot in the edges. How is that possible?
a = randn(128);
b = randn(128);
% Subtract the mean
a = a - mean(a(:));
b = b - mean(b(:));
% Direct cross-correlation
dc = xcorr2(b,a);
% Select the central part
dc = dc(65:128+64,65:128+64);
% FFT cross-correlation
fa = fft2(rot90(a,2));
fb = fft2(b);
cf = fftshift(real(ifft2(fa.*fb)));
figure,imagesc(abs(cf-dc))

サインインしてコメントする。

### 回答 (1 件)

Alessandro Masullo 2015 年 10 月 17 日
I figured it out by myself, and I uploaded a script on the file exchange to get a numerically equivalent function of xcorr2 which uses FFT. http://www.mathworks.com/matlabcentral/fileexchange/53570-xcorr2-fft-a-b-
##### 1 件のコメント-1 件の古いコメントを表示-1 件の古いコメントを非表示
Tapas Bar 2020 年 10 月 9 日

Hello Mr. Masullo,
I also have the same problem. I could not understand your answer quite well regarding the difference in values (in two methods) at the edges. Please help !
Thanks

サインインしてコメントする。

### カテゴリ

Help Center および File ExchangeCorrelation and Convolution についてさらに検索

### Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by