Waterfilling algorithm
% the water-filling process
% x: a vector with each component representing noise power
% P: total power
%
% The returned vector p maximizes the total sum rate given by
% sum(log(1 + p./x)), subject to the power constraint sum(p)== P, p>=0.
%
% The return a vector p also minimizes
% norm(p+x)
% subject to the constraints
% sum(p)==P; p>=0
%
%
% The second output "level" is the water level
%
% Sample usage:
% Three parallel Gaussian channels with noise powers 1, 2 and 3.
% The total power is 2.
%
% >> waterfill([1 2 3],2)
% ans =
% 1.5000 0.5000 0
%
%
% Author: Kenneth Shum, 2010
%
% Reference:
% T. M. Cover and J. A. Thomas, "Elements of Information Theory", John Wiley & Sons, 2006