function th = canstart(z,pobs,nu,dkx)
%CANSTART State-space model with initial parameter estimates.
%	THETA = CANSTART(Z,ORDERS,NU,DKX)
%	
%	THETA:  The resulting model, parametrized according to the orders
%	        and with initial parameter estimates calculated
%	        from data. No model covariances are given.
%	Z :     The output-input data [y u], with each input/output component
%	        as a column vector.
%	ORDERS: The model order.  Optionally this argument can be given as a
%	        vector with as many entries as there are output channels.
%	        The model order is then the sum of these entries. Basically, 
%	        ORDERS(k) gives the number of delays of output # k that are 
%	        used by the model, and a canonical parametrization is built
%               up accordingly.
%	NU:     The number of inputs.
%	DKX:    Determines whether to estimate the K (Kalman gain/Noise model),
%               D (Direct term from input to output) and X0 (intial state) 
%	        elements in the state-space model. Enter as DKX = [D, K, X0],
%	        where entry '0' means not to estimate, but fix to 0, while
%               '1' means that the corresponding matrix is estimated.
%	        Default: DKX = [0, 1, 0].
%	THETA is just an initial estimate of the system, and could be further
%	      improved by TH = PEM(z,THETA);
%
%	See also N4SID, PEM, THETA.

%	L. Ljung 21-9-90,9-26-93
%	Copyright (c) 1986-95 by the MathWorks, Inc.
%	$Revision: 1.6 $  $Date: 1995/05/16 09:01:05 $

if nargin<3
   disp('Usage: TH = CANSTART(Z,ORDERS,NU)')
   disp('       TH = CANSTART(Z,ORDERS,NU,DKX)')
   return
end
if nargin>3,
   if isstr(dkx),if strcmp(dkx,'oe'),dkx=[0,0,0];else dkx=[0,1,0];end,end
end
% The above is to accomodate the old syntax.
if nargin<4,dkx=[];end,if isempty(dkx),dkx=[0,1,0];end
if length(dkx)<2,dkx(1)=0;dkx(3)=0;end
if length(dkx)<3,dkx(3)=0;end
if nu==0&dkx(2)==0
   error('For a time-series model, the K-matrix has to be estimated.')
end
[Ncap,nz]=size(z);
psobs=pobs;
if any(psobs<0)
   error('The order indices must be non-negative.')
end
ny=length(pobs);
if ny==1, 
  ny=nz-nu;
  if ny>1
     ind1=floor(pobs/ny);
     for kc=1:ny-1
       psobs(kc)=ind1;
     end
     psobs(ny)=pobs-sum(psobs);
   end
end  

if nz~=nu+ny|ny<1, error(['The number of inputs and outputs in the data vector is not'...
' consistent with the number of observability indices and the number of inputs.']),end

th = n4sid(z,sum(psobs),length(psobs),[],dkx);
if isempty(th),return,end
th = ss2th(th,psobs);

th(2,7)=24;

