function  noisy_acache = aggreg_cache_noise(acache,noise,gain)

%AGGREG_CACHE_NOISE -- Sufficient statistics over noisy PLM inputs.
%
%  noisy_acache = aggreg_cache_noise(acache,noise,gain)
%
%  ACACHE is a structure array produced by CACHE_INPUTS and then
%  aggregated by AGGREG_CACHE (using the default "compact" format).
%  NOISE indicates the standard deviation of the additive noise.
%  GAIN multiplies the input+noise values before they are sent through
%  the sigmoidal activation function of the representation layer and
%  clipped at zero:  Y = max(0,sigmoid(input+randn*NOISE)).  This is
%  the transformation assumed inside the function SQUASHGAUSS.
%
%  Alternatively, GAIN can be zero, which indicates the semi-linear 
%  function:  Y = max(0,input+randn*NOISE) ; see TRUNCGAUSS.
%  This option is provided solely for backward compatibility with
%  older versions of the PLM model (prior to Aug 1, 2002). Note that
%  in this case the field 'varN' is returned instead of 'varNX'.
%
%  NOISY_ACACHE is a structure with the following fields:
%   1-5. Copy of ACACHE: 'arg', 'N', 'mnX', 'sdX', and 'mnX1X'.
%   6. rep_noise -- ==noise, [1x1], see PLM_PARAMS.
%   7. rep_gain  -- ==gain, [1x1], see PLM_PARAMS.
%   8. mnNX -- mean of the "squashed" noisy values, see SQUASHGAUSS.
%   9. varNX -- variance of the "squashed" noisy values.
%
%  Typical usage:
%    acache = noisy_aggreg_cache(aggreg_cache(cache),rep_noise,rep_gain) ;
%
%  See also AGGREG_CACHE, PLM_PROBAB1, SQUASHGAUSS, TRUNCGAUSS.

%  Original coding by Alexander Petrov, UC Irvine.
%  $Revision: 1.0 $  $Date: 2002/08/05 17:13 $


muX = acache.mnX.*gain ;
sigX = sqrt(acache.sdX.^2 + noise.^2) .* gain ;

[mnNX,varNX] = squashGauss(muX,sigX) ;
varNX = varNX.^2 ;         % image+noise variance combined

noisy_acache = acache ;
noisy_acache.rep_noise = noise ;
noisy_acache.rep_gain = gain ;
noisy_acache.mnNX = mnNX ;
noisy_acache.varNX = varNX ;

%--- Return NOISY_ACACHE
%%%%% End of file AGGREG_CACHE_NOISE.M
