function GainData = convertgain(CompData,CurFormat,TargetFormat)
%CONVERTGAIN  Updates gain factors to conform with given format.
%
%   GAINDATA = CONVERTGAIN(COMPDATA,CURRENTFORMAT,TARGETFORMAT) 
%   returns the gain sign and magnitude for the desired format 
%   TARGETFORMAT.  CURRENTFORMAT specifies the current format 
%   (typically COMPDATA.Format).

%   Author(s): P. Gahinet
%   Copyright 1986-2000 The MathWorks, Inc. 
%   $Revision: 1.5 $  $Date: 2000/06/14 20:17:56 $

% RE: CurFormat is the current format, TargetFormat the target format

GainData = CompData.Gain;

if ~isempty(GainData) & ~strcmp(CurFormat,TargetFormat)
   % Different formats: perform the transformation
   % Get pole/zero data
   ZPData = getpz(CompData);Z = ZPData{1};P = ZPData{2}; % REVISIT: merge 
   if CompData.Ts, % discrete
      P = P-1;   Z = Z-1;
   end
   Gain = GainData.Sign * GainData.Magnitude;
   
   % Convert
   if strcmpi(TargetFormat(1),'t')
      % zero/pole/gain -> time constant
      Gain = Gain * real(prod(-Z(Z~=0,:))/prod(-P(P~=0,:)));
   else
      % time constant -> zero/pole/gain
      Gain = Gain * real(prod(-P(P~=0,:))/prod(-Z(Z~=0,:)));
   end
   
   % Update values
   GainData.Magnitude = abs(Gain);
   GainData.Sign = sign(Gain) + (~Gain);
end


