function [a,b,c,d] = dtf2ss(num, den)
%DTF2SS	Discrete transfer function to state-space conversion.
%	[A,B,C,D] = DTF2SS(NUM,DEN)  calculates the state-space representation:
%
%		x(n+1) = Ax(n) + Bu(n)
%		y(n) = Cx(n) + Du(n) 
%
%	of the system:
%			NUM(z) 
%		H(s) = -------
%			DEN(z)
%
%	from a single input.  Vector DEN must contain the coefficients of the 
%	denominator in ascending  powers of z^-1 (constant first).  
%	Uses the Signal Processing Toolbox representation of a discrete
%	transfer function and therefore pads with trailing zeros
%	for unassigned coefficients.
%	Matrix NUM must contain the 
%	numerator coefficients with as many rows as there are outputs y.  The
%	A,B,C,D matrices are returned in controller canonical form.   

% 	A.C.W.Grace 4-5-90  
%	Revised ACWG 5-29-91
%	Copyright (c) 1990-94 by The MathWorks, Inc.

% Must pad numerator with trailing zeros for discrete case 
[mnum,nnum] = size(num);
[mden,n] = size(den);

% Strip leading zeros from denominator
inz = find(den ~= 0);
den = den(inz(1):n);
[mden,n] = size(den);

% Pad denominator with trailing zeros if necessary:
if nnum > n
	den = [den zeros(mden,nnum-n)];
	[mden,n] = size(den);
end


% Pad numerator with trailing  zeros, to make it have the same number of
% columns as the denominator, and normalize it to den(1)
num = [num zeros(mnum,n-nnum)];

[a,b,c,d]=tf2ss(num,den);



