
function dac=dac(b,B,R)

%function dac=dac(b,B,R)
%this function to perform digital-analog convertor
%bipolar two's complement D/A converter
% bits are dimensioned as \(b[0], b[1], \dotsc, b[B-1]\)

dac=0;

b(1)=1-b(1); %complement MSB
for i=B:-1:1
    dac=0.5*(dac + b(i));
end

dac=R*(dac-0.5); %shift and scale

b(1)=1-b(1); %restore MSB



