% work: calculate the max correlation
% parameters: basic image and a template image
% output: max correlation between the basic and template image

function maxCorrelation=correlationFFT(bIm,tIm)

tIm = rot90(tIm,2);
 if (size(bIm,2) >= size(tIm,2))
    ComWidth = size(bIm,2);
else
    ComWidth = size(tIm,2);
end;

if (size(bIm,1) >= size(tIm,1))
    ComHeight = size(bIm,1);
else
    ComHeight = size(tIm,1);
end;

M = sym(log2(ComWidth));
N = sym(log2(ComHeight));
if (frac(M)~=0)
    M = ceil(M);
    ComWidth = int8(2^M);
end;
if (frac(N)~=0)
    N = ceil(N);
    ComHeight = int8(2^N);
end;

bIm(ComHeight,ComWidth) = 0;
tIm(ComHeight,ComWidth) = 0;

FbIm = fft2(bIm);
FtIm = fft2(tIm);
FResult = FbIm.*FtIm;
corrlIm = ifft2(FResult);

maxCorrelation = max(corrlIm(:))/sqrt(sum(bIm(:))*sum(tIm(:)));
