function [xhat, M]=BGsQuantizer(x,B)
% [xhat, M]=BGsQuantizer(x,B)
%
% INPUTS:
% x..............data vector
% B..............bits of the quantizer
% OUTPUTS:
% xhat...........quantized data vector
% M..............reconstruction values
%
% Bernhard Geiger, SPSC, 2011
% with suggestsions of Martin Kirchengast -- THANKS!
% and Michael Kalcher -- THANKS!
%
% adapted during 2013 to work on 10/90% quantile instead of min/max.

stepsize=(quantile(x,0.9)-quantile(x,0.1))/(2^B-1);
M=quantile(x,0.1):stepsize:quantile(x,0.9);
M_threshold = M - (stepsize / 2);

xhat=M(1) * ones(size(x));
for n=1:length(M)
    xhat(x>M_threshold(n))=M(n);
end