function [c,Ls]=dgt(f,g,a,M,L)
%DGT  Discrete Gabor transform.
%   Usage:  c=dgt(f,g,a,M);
%           c=dgt(f,g,a,M,L);
%           [c,Ls]=dgt(f,g,a,M);
%           [c,Ls]=dgt(f,g,a,M,L);
%
%   Input parameters:
%         f     : Input data
%         g     : Window function.
%         a     : Length of time shift.
%         M     : Number of modulations.
%         L     : Length of transform to do.
%   Output parameters:
%         c     : M*N array of coefficients.
%         Ls    : Length of input signal.
%
%   DGT(f,g,a,M) computes the Gabor coefficients of the input
%   signal f with respect to the window g and parameters a and M. The
%   output is a vector/matrix in a rectangular layout.
%
%   The length of the transform will be the smallest multiple of a and M
%   that is larger than the signal. f will be zero-extended to the length of
%   the transform. If f is a matrix, the transformation is applied to each
%   column. The length of the transform done can be obtained by
%   L=size(c,2)*a;
%
%   The window g may be a text string decribing the window. The following
%   types are recognized
% 
%   'gauss'     - Gaussian window.
%
%   'dualgauss' - Canonical dual of Gaussian window.
%
%   'tight'     - Tight window generated from a Gaussian.
% 
%   DGT(f,g,a,M,L) computes the Gabor coefficients as above, but does
%   a transform of length L. f will be cut or zero-extended to length L before
%   the transform is done.
%
%   [c,Ls]=DGT(f,g,a,M) or [c,Ls]=DGT(f,g,a,M,L) additionally returns the
%   length of the input signal f. This is handy for reconstruction:
% 
%                [c,Ls]=dgt(f,g,a,M);
%                fr=idgt(c,gd,a,Ls);
% 
%   will reconstruct the signal f no matter what the length of f is, provided
%   that gd is a dual window of g.
%
%   The Discrete Gabor Transform is defined as follows: Consider a window g
%   and a one-dimensional signal f of length L and define N=L/a.
%   The output from c=DGT(f,g,a,M) is then given by
% 
%                  L-1 
%     c(m+1,n+1) = sum f(l+1)*exp(-2*pi*i*m*l/M)*conj(g(l-a*n+1)), 
%                  l=0  
% 
%   where m=0,...,M-1 and n=0,...,N-1 and l-an is computed modulo L.
% 
%   SEE ALSO:  IDGT, IIRPAR, DWILT, CANTIGHT
% 
%   EXAMPLES:  EXAMP_DGT
% 
%   REFERENCES:
%     H. G. Feichtinger and T. Strohmer, editors. Gabor Analysis and
%     Algorithms. Birkhäuser, Boston, 1998.
%     
%     K. Gröchenig. Foundations of Time-Frequency Analysis. Birkhäuser, 2001.

% This program is free software: you can redistribute it and/or modify
% it under the terms of the GNU General Public License as published by
% the Free Software Foundation, either version 3 of the License, or
% (at your option) any later version.
% 
% This program is distributed in the hope that it will be useful,
% but WITHOUT ANY WARRANTY; without even the implied warranty of
% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
% GNU General Public License for more details.
% 
% You should have received a copy of the GNU General Public License
% along with this program.  If not, see <http://www.gnu.org/licenses/>.

%   AUTHOR : Peter Soendergaard.

% Assert correct input.

error(nargchk(4,5,nargin));

if nargin<5
  L=[];
end;

if isnumeric(g)
  if size(g,2)>1
    if size(g,1)>1
      error('g must be a vector');
    else
      % g was a row vector.
      g=g(:);
    end;
  end;
  Lwindow=size(g,1);
else
  Lwindow=0;
end;

assert_squarelat(a,M,1,'DGT',0);
    
% Change f to correct shape.
[f,Ls,W,wasrow,remembershape]=comp_sigreshape_pre(f,'DGT',0);

[b,N,L]=assert_L(Ls,Lwindow,L,a,M,'DGT');

g=comp_window(g,a,M,L,0,'DGT');

f=postpad(f,L);

c=comp_dgt(f,g,a,M,L);

c=reshape(c,M,N,W);


