function [xb,Ns] = equi_binning2(x,m,Nbb)
%%
%%%%%returns discrete values in xb of the time series x using m bins
%%%%%%%%%For x taking already few discrete values some problems may occur
%%%%%%%%%in the assignation of equal mass to each bin. Nbb fix an adhoc
%%%%%%%%%number of samples per bin, by default use 0.
N = length(x);
Nb = N/m;
if Nbb==0
    
else
   Nb = Nbb; 
end
ux = unique(x);
Nu = length(ux);
maxx = max(x);

te =0;
Ns = zeros(m,1);
bs = zeros(m,1);
count =0;
for i = 1:Nu
    te = te + length(find(x==ux(i)));
    if te >Nb
        count = count +1;
        if count < m
         Ns(count)= te;
         te =0;
         bs(count)= ux(i);
        end
    end
end 
Ns(m)= te;
bs(m)= maxx;
xb = zeros(N,1);
for i = 1:N
   te = x(i)-bs;
   t = find(te<=0);
   xb(i)= t(1);
end

