% ASSERT_L  Validate lattice and window size.
%    Usage:  [b,N,L]=assert_L(Lin,Lwindow,a,M,callfun);
% 
%    Input parameters:
%          Lin     : Length of signal (see below).
%          Lwindow : Length of window.
%          a       : Length of time shift.
%          M       : Number of modulations.
%          callfun : Name of calling function.
%          flag    : See below.
%    Output parameters:
%          b       : Length of frequency shift.
%          N       : Number of translations.
%          L       : Transform length.         
% 
%   Calculate a minimal transform length, or verify a user specified
%   input length.
% 
%   The routine assumes that a and M has already been checked. use
%   asser_squarelat for this.
% 
%   Lin takes the following values:
%       positive : This is a value of L specified by the user
%       negative : This is the length of the signal. A minimal value of
%                  L is returned.
% 
%   Set Lwindow to 0 if window length is not to be checked.
%   
% 
% ---------------------------------------------------------------
function [b,N,L]=assert_L(Lin,Lwindow,a,M,callfun)
%ASSERT_L  Validate lattice and window size.
%   Usage:  [b,N,L]=assert_L(Lin,Lwindow,a,M,callfun);
%
%   Input parameters:
%         Lin     : Length of signal (see below).
%         Lwindow : Length of window.
%         a       : Length of time shift.
%         M       : Number of modulations.
%         callfun : Name of calling function.
%         flag    : See below.
%   Output parameters:
%         b       : Length of frequency shift.
%         N       : Number of translations.
%         L       : Transform length.         
%
%  Calculate a minimal transform length, or verify a user specified
%  input length.
%
%  The routine assumes that a and M has already been checked. use
%  asser_squarelat for this.
%
%  Lin takes the following values:
%      positive : This is a value of L specified by the user
%      negative : This is the length of the signal. A minimal value of
%                 L is returned.
%
%  Set Lwindow to 0 if window length is not to be checked.
%  

if (prod(size(Lin))~=1 || ~isnumeric(Lin))
  error([callfun,': L must be a scalar']);
end;

if rem(Lin,1)~=0
  error([callfun,': L must be an integer']);
end;

% This test should not be placed here, it gets called to often.
% However, this is an easy place to put it.
%
% Length of window must be dividable by M.
% We cannot automically zero-extend the window, as it can
% possible break some symmetry properties of the window, and we don't
% know which symmetries to preserve.
if rem(Lwindow,M)~=0
  error([callfun,': Length of window must be dividable by M.']);
end;


if Lin<0
  % Smallest length transform.
  Lsmallest=lcm(a,M);

  % Choose a transform length larger than both the length of the
  % signal and the window.
  L=ceil(max(-Lin,Lwindow)/Lsmallest)*Lsmallest;
else
  L=Lin;

  if rem(L,M)~=0
    error([callfun,': The length of the transform must be divisable by M=',num2str(M)]);
  end;

  if rem(L,a)~=0
    error([callfun,': The length of the transform must be divisable by a=',num2str(a)]);
  end;

  if rem(L,a)~=0
    error([callfun,': The length of the transform must be divisable by a=',num2str(a)]);
  end;

  
end;

b=L/M;
N=L/a;
