function Dpoly = idpoly(Dss, varargin)
% Conversion to polydata.
%
%  Type: 'measured', or 'split'.
%     'measured': Convert to measured model component (A=C=D=1).
%     'split':    Split model inputs so that that the last Ny inputs are
%                 treated as noise inputs. Original model must have Ny or
%                 more inputs.

%   Author(s): Rajiv Singh
%   Copyright 2010-2016 The MathWorks, Inc.

Split = nargin>1 && strncmpi(varargin{1},'s',1);

% If noise channels must be split out, go via idss algorithm so that a
% stable predictor may be obtained.
 
[~, nu] = iosize(Dss);
if ~Split
   if nu==1
      Dpoly = idpoly(tf(Dss));
   else
      % IDSS can't handle output delays.
      yd = Dss.Delay.Output; % column vector
      Dss.Delay.Output = yd*0;
      Dpoly = convertToARMAX(idss(Dss));
      if any(yd)
         Dpoly = setIODelay(Dpoly, repmat(yd,[1 nu]));
      end
   end
else
   % First convert to IDSS so that K is corrected. Then convert to idpoly.
   Dpoly = idpoly(idss(Dss,'split'));
end
