function fhndls = latcarmaparse
%LATCARMAPARSE  Parser for LATCARMA filter objects.
%   FHNDLS = LATCARMAPARSE returns a 3 element vector of function handles, 
%   FHNDLS, that point to the three functions required by DFILTPARSE
%   when called form any DFILT constructor.
%
%   For more information on what the 3 functions do, see the help for
%   DFILTPARSE.

%   Author(s): R. Losada
%   Copyright 1988-2000 The MathWorks, Inc.
%   $Revision: 1.3 $  $Date: 2000/06/13 21:22:52 $ 

fhndls = [@latcarmadefaultfilt,@formcelllatcarma,@checklatcarma];

%----------------------------------------------------------------------------------
function coeffs = latcarmadefaultfilt
%LATCARMADEFAULTFILT  Default filter to use in latcarma objects.

% Create a default filter
coeffs = {{0,1}};

%-----------------------------------------------------------------------------------
function [coeffs,msg] = formcelllatcarma(varargin)
%FORMCELLLATCARMA  Form cell array of coefficients of latcarma object.

% Dummy initialization
coeffs = {};
msg = '';

if rem(nargin,2),
    msg = 'You must specify lattice and ladder coefficients for each section.';
    return
end

% Form cell of cells of pairs of coefficients: {{K1,V1},{K2,V2},...}
for n = 1:2:nargin
    coeffs{(n+1)./2} = {varargin{n},varargin{n+1}};
end

%----------------------------------------------------------------------------------
function [c,msg] = checklatcarma(c)
%CHECKLATCARMA Check if cell contains valid reflection coefficients.


msg = '';

k = c{1};
v = c{2};

if isempty(k),
    k = 0;
end

if isempty(v),
    v = 1;
end


% Check that k and v are vectors
if prod(size(k)) ~= max(size(k)),
   msg = 'Reflection coeffs. must be given in a vector.';
   return
end

if prod(size(v)) ~= max(size(v)),
    msg = 'Ladder coeffs. must be given in a vector.';
    return
end


% The length of k and v are related as follows:
% If k is empty or zero, v can is a scalar, length 1
% if k is a nonzero scalar, v must have length 2,
% in general, if k is a vector, length(v) must be length(k) + 1,
% if k is a vector, but v was specified as a scalar, we assume an
% allpole filter and fill v with zeros until its length is lengtk(k) + 1.

if length(k)==1 & k == 0,
    if length(v) ~= 1,
        msg = 'There must be a single ladder coefficient when k is zero or empty.';
        return
    else
        c = {k,v};
        return        
    end
end

% Always store k and v as columns
k = k(:);
v = v(:);

if length(v) ~= 1,
    % If v is a vector, it must be of length(k) + 1
    if length(v) ~= length(k) + 1,
        msg = 'The ladder coefficient vector length must exceed the lattice by one.';
        return
    end
else
    % Assume an allpole filter, pad with zeros
    v = [v;zeros(size(k))];
end
    
c = {k,v};

