%------------------------------------------------------
% MATLAB function that implements CDF based non-uniform 
% quantization using the CDF of the input source F(x) and 
% a uniform quantizer with B bits
%
% AUTHOR   : Balu Santhanam
% DATE     : 12/11/13
% SYNOPSIS : [xq,SNR] = cdfquantn(x,B,opt,para)
%----------------------------------------------------------
function [xq,SNR] = cdfquantn(x,B,opt,para)
% Apply the CDF transformation to convert 
% the source symbols to a uniform source
if length(para) == 1
   a = para;
   r = cdf(opt,x,a);
elseif length(para) == 2
    a = para(1);
    b = para(2);
    r = cdf(opt,x,a,b);
end
mo = mean(r);
z = r - mo; z_o = fxquant(z,B,'round','sat');
clear r z
z_q = z_o+ mo;
clear mo
if length(para) == 1
    xq = icdf(opt,z_q,a);
elseif length(para) == 2
    xq = icdf(opt,z_q,a,b);
end
SNR = -20*log10(norm(xq-x)/norm(xq));
return


