function levels = bwlevels(B, sigma)
% BWLEVELS  2^B bin centers or levels of B-bit DAC
%
% LEVELS = BWLEVELS(B, SIGMA)
%    levels of B-bit DAC assuming the B switched elements
%    are made up from unit elements with properties:
%         mean value             1
%         std. dev.              sigma
%         are independent (i.e. no systematic errors)
%    first level is zero (no error)

% generate values of switched elements
el = zeros(B, 1);
for i=1:B
   n = 2^(i-1);        % number of unit elements in this level
   el(i) = n + sigma * sum(randn(n, 1));
end

% compute all DAC output levels
codes  = (0:2^B-1)';
levels = zeros(2^B, 1);
for i=1:B
   levels = levels + el(i) * bitget(codes, i);
end
