function [PID,gic] = d2c(PID,options)
%D2C  Conversion of discrete time PID to continuous time.

%   Author(s): Rong Chen
%   Copyright 2009-2011 The MathWorks, Inc.
gic = [];
Ts = PID.Ts;
if isa(PID,'ltipack.piddata2')
    % 2-DOF
    objFcn = @pidstd2;
else
    % 1-DOF
    objFcn = @pidstd;
end
switch lower(options.Method)
    case 'tustin'
        % first convert to PID with tustin formulas
        try
            PID = objFcn(PID,struct('IFormula','T','DFormula','T'));
        catch ME
            ctrlMsgUtils.error('Control:transformation:pidConversionFailed','d2c');
        end
        % set Ts to 0 and formulas to default
        PID.Ts = 0;
        PID.IFormula = 'F';
        PID.DFormula = 'F';
        % adjust Ti Td when prewarping is present
        w = options.PrewarpFrequency;
        if w ~= 0
            Tsw = Ts * w / 2;
            Ratio = tan(Tsw)/Tsw;
            PID.Ti = PID.Ti*Ratio;
            PID.Td = PID.Td*Ratio;
        end
    case 'zoh'
        % first convert to PID with ForwardEuler formulas
        PID = objFcn(PID,struct('IFormula','F','DFormula','F'));
        % adjust Td only when derivative term is present
        if PID.Td~=0
            if PID.Td<=Ts*PID.N
                % error out when Td<=Ts*N to avoid complex Td
                ctrlMsgUtils.error('Control:transformation:pidConversionFailed','d2c');
            else
                PID.Td = -Ts*PID.N/log(1-Ts*PID.N/PID.Td);
            end
        end
        % set Ts
        PID.Ts = 0;
    case 'foh'
        % first convert to PID with Trapezoidal formulas
        PID = objFcn(PID,struct('IFormula','T','DFormula','T'));
        % adjust Kd and Tf only when derivative term is present
        Td = PID.Td;
        if Td~=0
            N = PID.N;
            if Td<=Ts*N/2
                % error out when Td<=Ts*N/2 to avoid complex Td
                ctrlMsgUtils.error('Control:transformation:pidConversionFailed','d2c');
            else
                PID.N = (Td/Ts) * log((2*Td+Ts*N)/(2*Td-Ts*N));
            end
        end
        % set Ts
        PID.Ts = 0;
    case 'matched'
        ZPKd = zpk(PID);
        ZPKc = d2c(ZPKd,options);
        try
            PID = objFcn(ZPKc);
        catch ME
            ctrlMsgUtils.error('Control:transformation:pidConversionFailed','d2c');
        end
end

