-
Notifications
You must be signed in to change notification settings - Fork 0
/
FastCorrelate.m
40 lines (29 loc) · 906 Bytes
/
FastCorrelate.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
function [ACMat, Instants] = FastCorrelate(Matrix1, Matrix2, ShiftedBy)
if nargin() < 3
ShiftedBy = 0;
end
nr1 = size(Matrix1, 1);
nc1 = size(Matrix1, 2);
nr2 = size(Matrix2, 1);
nc2 = size(Matrix2, 2);
if nr1 ~= nr2 && nr1 ~= 1 && nr2 ~= 1
ME = MException('VerifyInput:InvalidInput', ...
'The Number of rows must be equal or atleast one must be single row');
throw(ME);
end
expof2 = ceil(log2(nc1 + nc2));
Matrix1 = [Matrix1 zeros(nr1, 2^expof2 - nc1)];
Matrix2 = [Matrix2 zeros(nr2, 2^expof2 - nc2)];
MatFFT1 = fft(Matrix1');
MatFFT2 = fft(Matrix2');
if nr1 == 1
MatFFTfinal = (MatFFT1*ones(1,nc1)).*conj(MatFFT2);
elseif nr2 == 1
MatFFTfinal = (MatFFT1).*(conj(MatFFT2).*ones(1,nc2));
else
MatFFTfinal = MatFFT1.*conj(MatFFT2);
end
MatFFTifft = ifft(MatFFTfinal);
ACMat = [MatFFTifft(end-nc2:end,:); MatFFTifft(1:nc1+1, :)]';
Instants = -nc2-ShiftedBy:nc1+1-ShiftedBy;
end