function NoisySignal = addNoise( CleanSignal, VarNoise )
% addNoise - simulate additive, white Gaussian noise channel
%
% Independent Gaussian noise of variance specified by the second 
% input parameter is added to the (vector or matrix) signal passed 
% as the first input. The result is returned in a vector or matrix 
% of the same size as the input signal.
%
% The function determines if the signal is real or complex valued 
% and generates noise samples accordingly. In either case the total  
% variance is equal to the second input.
%
% Inputs:
%   CleanSignal - a vector or matrix signal to which noise will be added.
%   VarNoise    - variance of noise samples; for complex input signals the 
%                 noise will be complex valued and real and imaginary part
%                 each have variance VarNoise/2
%
% Example:
%   NoisySignal = addNoise( CleanSignal, VarNoise )

%% generate noisy signal
% distinguish between real and imaginary input signals
if ( isreal( CleanSignal ) )
    NoisySignal = CleanSignal + ...
        sqrt(VarNoise) * randn( size(CleanSignal) );
else
    NoisySignal = CleanSignal + sqrt(VarNoise/2) * ...
        ( randn( size(CleanSignal) ) + j*randn( size(CleanSignal) ) );
end