function [wR , muR, p_stdR] = update_parameters(x,w,mu,p_std, c_matched, alfa,numComps,min_p_std,max_p_std)
%UPDATE_PARAMETERS update the Gaussian parameters. Note that in the code, a
% two different updating schemes could be selected;

% x = [1,2,3]';
% w=[10,11,12;20,21,22;30,31,32];
% mu=w;
% p_std =[1,0,0;2,1,0;3,0,0];
% c_matched = [1;2;1];
% numComps = 3;

X           =   size(w,1); 
Y           =   numComps;
x           =   x*ones(1,numComps);
first       =   X*Y; % 

w           =   reshape(w,first,1);
mu          =   reshape(mu,first,1);
p_std       =   reshape(p_std,first,1);
x           =   reshape(x,first,1); 

s_matched   =   couple2single((1:X)',c_matched,X,Y); 

w                   =   w.*(1-alfa);
w(s_matched)        =   w(s_matched)+ alfa;
diff                =   (x(s_matched)-mu(s_matched)).^2;
ro                  =   alfa.*(1./(p_std(s_matched)*sqrt(2*pi))).*exp(-0.5.*diff./(p_std(s_matched).^2));
mu(s_matched)       =   mu(s_matched).*(1-ro)+ro.*x(s_matched);
p_std(s_matched)    =   p_std(s_matched).*(1-ro)+ro.*(diff);
% -- Minimum and maximum std test;
p_std(s_matched)    =   (p_std(s_matched)<min_p_std)*min_p_std + (p_std(s_matched)>=min_p_std).*p_std(s_matched);
p_std(s_matched)    =   (p_std(s_matched)>max_p_std)*max_p_std + (p_std(s_matched)<=max_p_std).*p_std(s_matched);
wR                  =   reshape(w,X,Y);
p_stdR              =   reshape(p_std,X,Y);

muR                 =   reshape(mu,X,Y);
Sum                 =   sum(wR');
Sum2                =   (Sum'*ones(1,numComps));
Sum2                =   reshape(Sum2,first,1);%[Sum';Sum';Sum'];
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% --Updating scheme 1
w=w./Sum2; % one-shot updating (you have to use it when a "bootstrap" frame is present,
% or when in the first frame FG objects are absent). After the first frame, 
% the gaussian mixtures have 1 BG component with weight 1, which mean value
% is equal to the pixel signal observed. 
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% --Updating scheme 2
%  good = Sum2>1;
%  w(good)=w(good)./Sum2(good); 
%  (you have to use it when a "bootstrap" frame is absent,
%   or when in the first frame FG objects are present)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
wR                  =   reshape(w,X,Y);
return;
