function [a,b,c,d] = sec2ss(h)
%SEC2SS  Convert single-section df1 object to state-space.

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

[num,den] = tf(h);

% Normalize the transfer function and get order of num and den
[num,den,N,M] = normalizengetord(num,den);

if M == 0 & N == 0
    % Scalar case,
    a = []; b = []; c = []; d = num;
    return
end

if M==0,
    % FIR
    a = sysmatrix(den,N,M);
    b = [1;
        zeros(N-1,1)];
elseif N==0,
    % All pole
    a = [-den(2:end);
        eye(M-1) zeros(M-1,1)];
    b = [num(1);
        zeros(M-1,1)];
else
    % IIR
    % Build the system matrix A
    a = [zeros(1,N+M);
        eye(N-1) zeros(N-1,M+1);
        num(2:end) -den(2:end);
        zeros(M-1,N) eye(M-1) zeros(M-1,1)];
    
    % Build the input matrix B
    b = [1;
        zeros(N-1,1);
        num(1);
        zeros(M-1,1)];
end

% Build output matrix C
c = [num(2:end) -den(2:end)];

% Build direct feedthrough matrix D
d = num(1);
