function [Dc,gic] = d2c(Dd,options)
%D2C  Conversion of discrete MECHSS model to continuous time.

%   Copyright 2020 The MathWorks, Inc.
if strcmp(options.Method,'tustin')
   % Handle prewarp
   Ts = Dd.Ts;
   w = options.PrewarpFrequency;
   if w == 0
      h = Ts/2;
   else
      % Handle prewarping
      h = tan(w*Ts/2)/w;
   end
   h2 = h^2;

   % Get dimensions
   md = Dd.m;  kd = Dd.k;  cd = Dd.c;  fd = Dd.f;  gd = Dd.g;
   if isempty(md)
      md = speye(size(kd));
   elseif nnz(md)==0
      % Use first-order Tustin when M=0
      [Dc,gic] = d2c(sparss(Dd),options);
      Dc = mechss(Dc);
      return
   end
   m = (md+kd-cd)/4;
   k = (md+kd+cd)/(4*h2);
   c = (md-kd)/(2*h);
   b = Dd.b/(4*h2);
   [~,Y] = ltipack.util.safeMdivide(m,[],gd-fd);
   if ~allfinite(Y)
      error(message('Control:transformation:d2c06'))
   end
   f = 2*gd+(Y*cd)/2;
   g = h2*Y*c-2*h*fd;
   d = Dd.d-(Y*Dd.b)/4;
   Dc = ltipack.mechdata(m,c,k,b,f,g,d,0);
   Dc.Delay.Input = Ts * Dd.Delay.Input;
   Dc.Delay.Output = Ts * Dd.Delay.Output;
   Dc.Delay.Internal = Ts * Dd.Delay.Internal;
   Dc.StateInfo = Dd.StateInfo;
   gic = [];
else
   error(message('Control:transformation:d2c02'))
end


