1function A=ref_ambiguityfunction(f, g)
2%-*- texinfo -*-
3%@deftypefn {Function} ref_ambiguityfunction
4%@verbatim
5%REF_AMBIGUITYFUNCTION  Reference ambiguity function
6%   Usage:  A=ref_ambiguityfunction(f)
7%	    A=ref_ambiguityfunction(f,g)
8%
9%   REF_AMBIGUITYFUNCTION(f) computes the ambiguity function of f.
10%   REF_AMBIGUITYFUNCTION(f,g) computes the cross-ambiguity function of f and g.
11%@end verbatim
12%@strong{Url}: @url{http://ltfat.github.io/doc/reference/ref_ambiguityfunction.html}
13%@end deftypefn
14
15% Copyright (C) 2005-2016 Peter L. Soendergaard <peter@sonderport.dk>.
16% This file is part of LTFAT version 2.3.1
17%
18% This program is free software: you can redistribute it and/or modify
19% it under the terms of the GNU General Public License as published by
20% the Free Software Foundation, either version 3 of the License, or
21% (at your option) any later version.
22%
23% This program is distributed in the hope that it will be useful,
24% but WITHOUT ANY WARRANTY; without even the implied warranty of
25% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
26% GNU General Public License for more details.
27%
28% You should have received a copy of the GNU General Public License
29% along with this program.  If not, see <http://www.gnu.org/licenses/>.
30
31% AUTHOR: Jordy van Velthoven
32
33if ~all(length(f)==length(g))
34  error('%s: f and g must have the same length.', upper(mfilename));
35end;
36
37L = length(f);
38H = floor(L/2);
39R = zeros(L,L);
40A = zeros(L,L);
41
42% Compute the analytic representation of f
43if (nargin == 1)
44  if isreal(f)
45   z = fft(f);
46   z(2:L-H) = 2*z(2:L-H);
47   z(H+2:L) = 0;
48   z1 = ifft(z);
49   z2 = z1;
50  else
51   z1 = f;
52   z2 = z1;
53  end
54elseif (nargin == 2)
55  if isreal(f) || isreal(g)
56   z1 = fft(f);
57   z1(2:L-H) = 2*z1(2:L-H);
58   z1(H+2:L) = 0;
59   z1 = ifft(z1);
60
61   z2 = fft(g);
62   z2(2:L-H) = 2*z2(2:L-H);
63   z2(H+2:L) = 0;
64   z2 = ifft(z2);
65  else
66    z1 = f;
67    z2 = g;
68  end
69end
70
71
72
73% Compute instantaneous autocorrelation matrix R
74for l = 0 : L-1;
75  for m = -min([L-l, l, round(L/2)-1]) : min([L-l, l, round(L/2)-1]);
76    R(mod(L+m,L)+1, l+1) =  z1(mod(l+m, L)+1).*conj(z2(mod(l-m, L)+1));
77  end
78end
79
80% Compute ambiguity function A
81for hh=0:L-1
82  for ii=0:L-1
83    for jj = 0:L-1
84      A(hh+1, ii+1) = A(hh+1, ii+1) + R(jj+1, ii+1) .* exp(-2*pi*i*hh*jj/L);
85    end
86  end
87end
88
89A = fftshift(fft2(A));
90
91