function out = saturate(in, SatHi, SatLo)
% out = saturate(in, SatHi, SatLo)
%
% Saturates the real and imaginary parts of "in" independently to the
% given SatHi and SatLo values.  To disable one side of the saturation,
% give a very large positive/negative number as an argument.

% 00/05/30  Written


%--- Input checking
if nargin == 1,
   fprintf('Warning: saturate(): No saturation limits given!\n');
   SatHi = +1e12;
   SatLo = -1e12;
end

if nargin == 2,
   fprintf('Warning: saturate(): SatLo not given!\n');
   SatLo = -1e12;
end

%--- Main
out = in;

satur = real(out) > SatHi;
out = (~satur .* out) + satur .* (SatHi + i*imag(out));

satur = imag(out) > SatHi;
out = (~satur .* out) + satur .* (i * SatHi + real(out));

satur = real(out) < SatLo;
out = (~satur .* out) + satur .* (SatLo + i*imag(out));

satur = imag(out) < SatLo;
out = (~satur .* out) + satur .* (i * SatLo + real(out));

