%% $Revision: 1.1.6.2 $
%% 
%%
%% Copyright 1994-2003 The MathWorks, Inc.
%%
%% Abstract:
%%      ComplexToMagnitudeAngle Block Target File.

%implements ComplexToMagnitudeAngle "C"

%% Function: BlockInstanceSetup ==============================================
%% Abstract:
%%   Set expression folding compliance
%%
%function BlockInstanceSetup(block, system) void
  %<LibBlockSetIsExpressionCompliant(block)>
%endfunction


%% Function: Outputs ===========================================================
%% Abstract:
%%      Compute the magnitude and/or angle of the complex valued input.
%%
%function Outputs(block, system) Output
  %assign dTypeId = LibBlockInputSignalDataTypeId(0)
  %assign inputComplex = LibBlockInputSignalIsComplex(0)
  %assign rollVars = ["U", "Y"]
  %roll idx = RollRegions, lcv = RollThreshold, block, "Roller", rollVars
    %assign ure = LibBlockInputSignal(0, "", lcv, "%<tRealPart>%<idx>")
    %if (inputComplex == 1)
      %assign uim = LibBlockInputSignal(0, "", lcv, "%<tImagPart>%<idx>")
    %endif
    %if ((ParamSettings.Output == "Magnitude") || ...
         (ParamSettings.Output == "Magnitude and angle"))
      %assign y = LibBlockOutputSignal(0, "", lcv, idx)
      %if (inputComplex == 1)
	%<y> = %<LibGenMathFcnCall("hypot", dTypeId, "%<ure>", "%<uim>")>;
      %else
	%% If buffer is being reused and we are outputting both
	%% magnitude and angle, we need to save the magnitude
	%% in a temporary variable so as not to overwrite the input
	%% before the angle is computed.
	%if ((LibBlockInputSignalBufferDstPort(0) == 0) && ...
	  (ParamSettings.Output == "Magnitude and angle"))
	  %assign dTypeId = LibBlockInputSignalDataTypeId(0)
	  %assign DTname = LibGetDataTypeNameFromId(dTypeId)
	  {
	  %<DTname> tmpRe;
	  %assign y = "tmpRe"
	  \
	%endif
	%<y> = %<LibGenMathFcnCall("abs", dTypeId, "%<ure>", "")>;
      %endif
    %endif
    %%
    %if ((ParamSettings.Output == "Angle") || ...
         (ParamSettings.Output == "Magnitude and angle"))
      %assign angPort = (ParamSettings.Output == "Angle") ? 0 : 1
      %assign y = LibBlockOutputSignal(angPort, "", lcv, idx)
      %if (inputComplex == 1)
	%<y> = %<LibGenMathFcnCall("atan2", dTypeId, "%<uim>", "%<ure>")>;
      %else
	%assign dtZero = SLibGetFormattedValueFromId(dTypeId, 0)
	if (%<ure> >= %<dtZero>) {
	  %<y> = %<dtZero>;
	} else {
	  %<y> = %<LibGetMathConstant("RT_PI", dTypeId)>;
	}
      %endif
    %endif
    %%
    %% If a temporary variable was used, assign the first output
    %% back to the temporary variable.
    %if ((LibBlockInputSignalBufferDstPort(0) == 0) && ...
      (ParamSettings.Output == "Magnitude and angle"))
      %assign y = LibBlockOutputSignal(0, "", lcv, idx)
      %<y> = tmpRe;
      }
    %endif
  %endroll
  
%endfunction %% Outputs

%% [EOF] cmpx2maan.tlc
