function [x_gr,x_cd]=nukvant(x,b);
% Neuniformni kvantizator s osiguranim nultim nivoom.
%   [GRANICE,CENTRI] = nckvant(X, BIT)
%   Funkcija uzima signal X i broj bitova BIT, a vraca granice kvanta GRANICE
%   i vrijednosti kvanta CENTRI.

% Autor: Peter Skoda

n_hist=50;
% aproksimacija pdf i cdf
x_pdf=hist(x,n_hist);
x_cdf=cumsum(x_pdf)/length(x);
clear x_pdf;

% prosirenje matrica
x_1=linspace(min(x),max(x),n_hist+1);
y_1=[0 x_cdf];
clear c_cdf;

% diferencije
dx=diff(x_1);
dy=diff(y_1);

% centar
i=max(find(x_1<0));
c=dy(i)/dx(i)*(0-x_1(i))+y_1(i)-0.5;

% granice
N=2^b; % broj nivoa
y_1k=y_1*N;
dy_k=dy*N;
for i=1:N-1;
    w=max(find(y_1k<i));
    x_gr(i)=(i-y_1k(w))*dx(w)/dy_k(w)+x_1(w);
end

% centri
for i=1:N;
    w=max(find(y_1k<(i-0.5)));
    x_cd(i)=((i-0.5)-y_1k(w))*dx(w)/dy_k(w)+x_1(w);
end

% onaj centar koji je najblizi nuli, proglasiti nulom
x_ca=abs(x_cd);
w=find(x_ca==min(x_ca));
x_cd(w)=0;