function [a,b,c,d,k,x0,da,db,dc,dd,dk,dx0]=th2ss(th)
%TH2SS	Transforms a model in THETA-format to state-space
%
%	[A,B,C,D,K,X0]=th2ss(TH)
%
%	TH: The model, defined in the THETA format (see help theta).
%	A,B,C,D,K,X0 : The matrices in the state-space description 
%	
%	Xn = A X + B u + K e       X0 initial value
%	y  = C X + D u + e
% 	
%	Here Xn is the derivative of X or the next value of X, depending
%	on whether TH is denoted as continuous or discrete time.
%
%	With [A,B,C,D,K,X0,dA,dB,dC,dD,dK,dX0]=th2ss(TH) also the
%	standard deviations of the matrix elements are computed.

%	L. Ljung 10-2-90
%	Copyrigth (c) 1990 by the MathWorks, Inc.
%	All rights reserved.

T=th(1,2);
if isthss(th), if nargout<7, eval('[a,b,c,d,k,x0]=eta2ss(th);'),
else eval('[a,b,c,d,k,x0,da,db,dc,dd,dk,dx0]=eta2ss(th);'),end,return,end
[a,b,c,d,k,x0]=th2ssaux(th);

if nargout>6,
% This assumes that TH does not use both an A-polynomial and F/D polynomials
[thpar,p]=th2par(th);
thpar=thpar+sqrt(diag(p))';
th(3,1:length(thpar))=thpar;
[a1,b1,c1,d1,k1,x01]=th2ssaux(th);
da=abs(a-a1);db=abs(b-b1);dc=abs(c-c1);dd=abs(d-d1);dk=abs(k-k1);
dx0=abs(x0-x01);end

