function f = latcma(h,varargin)
%LATCMA   Convert a DFILT object to a LATCMA object.
%   G = LATCMA(H) returns a LATCMA filter object G given
%   a minimum-phase FIR DFILT object H.
%
%   G = LATCMA(H,I) converts the I-th section of H to a
%   LATCMA filter object.

%   Author(s): R. Losada
%   Copyright 1988-2000 The MathWorks, Inc.
%   $Revision: 1.6 $  $Date: 2000/09/11 15:41:45 $

error(nargchk(1,2,nargin));

returnhndl = 0; % By default don't return handle

if nargin == 2 & strcmpi(varargin{1},'gethndl'),
    returnhndl = 1;
    inputargs = {h};
else
    inputargs = {h,varargin{:}};
end 

if returnhndl,
    f = @tolatcma;
    return
else,
    [newsection,connect] = tolatcma('minphase',inputargs{:});
    f = latcma(newsection,connect);
end

%-----------------------------------------------------------------------------
function [newsection,connect] = tolatcma(phasestr,h,varargin)
%TOLATCMA  Generate input args for both latcma and latcmax constructors

if nargin == 3,
    h = section(h,varargin{1});
end

% Make sure filter is FIR first
if ~isfir(h),
    error('The filter to convert must be FIR.');
end

% Make sure filter has minimum-phase or maximum-pase sections
switch phasestr,
case 'minphase',
    if ~isminphase(h),
        error('The filter to convert must have minimum-phase sections.');
    end
	for n = 1:nsections(h),
		[num{n},den{n}] = tf(section(h,n));
		k{n} = tf2latc(num{n},'min');
		newsection{n} = {k{n}};
	end
case 'maxphase',
	if ~ismaxphase(h),
		error('The filter to convert must have maximum-phase sections.');
	end
	for n = 1:nsections(h),
		[num{n},den{n}] = tf(section(h,n));
		k{n} = tf2latc(num{n},'max');
		newsection{n} = {k{n}};
	end
end

connect = connection(h);
