function [b,a]= tf(h,sec)
%TF  Convert dfilt object to transfer function.
%   [B,A] = TF(H,N) returns the coefficients corresponding to
%   the N-th section of dfilt object H, in vectors B and A.  B
%   contains the numerator coefficients and A contains the
%   denominator coefficients.
%
%   [B,A] = TF(H) returns the overall transfer function
%   coefficients corresponding to the dfilt object H, in vectors
%   B and A.  B contains the numerator coefficients and A
%   contains the denominator coefficients.

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

error(nargchk(1,2,nargin));
error(nargoutchk(0,2,nargout));

if nargin == 2,
    h = section(h,sec);
end


% Determine overall transfer function
[b,a] = determinetf(h);         

%--------------------------------------------------------------
function [b,a] = determinetf(h)
%DETERMINETF Determine overall transfer function.
switch connection(h),
case 'cas'
    % Initialize b and a
    b = 1; a = 1;
    
    for n = 1:nsections(h),
        f = section(h,n);
        [bt,at] = sec2tf(f);
        
        b = conv(b,bt);
        a = conv(a,at);
    end
    
case 'par',
    % Initialize transfer function
    b=0; a=1;
    
    % Form transfer function recursively
    for n = 1:nsections(h),
        
        f = section(h,n);
        [bt,at] = sec2tf(f);
        conv1 = conv(b,at);
        conv2 = conv(bt,a);
        % Make sure they are of the same length to add
        conv1 = [conv1 zeros(1,length(conv2)-length(conv1))];
        conv2 = [conv2 zeros(1,length(conv1)-length(conv2))];
        
        % Add to get numerator
        b = conv1 + conv2;
        % Den is common denominator
        a = conv(a,at);
    end
end

 % Remove trailing zeros
 [b,a] = normalizengetord(b,a);
 
