%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%                              Example 4.3                                %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%                                                                         %
%                           Reference Voltage                             %
%                                                                         %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

clear all;

%-------------------------------------------------------------------------%
%                                                                         %
%                               Input Data                                %
%                                                                         %
%-------------------------------------------------------------------------%

n=6;                                    
m=2^n;                              % number of resistors
rnom=50;                            % resistors nominal value
grad=0.0003/2;                      % linear gradient 300ppm/um
pitch=25;                          
r(1)=rnom/2;                        
r1(1)=rnom/2;
rn(1)=rnom/2;
rn(2:m-1)=rnom;
rn(m)=rnom/2;

%-------------------------------------------------------------------------%
%                                                                         %
%                            systematic errors                            %
%                                                                         %
%-------------------------------------------------------------------------%

err=0;
err1=0;

for i=2:m/2,
    err=err+grad*pitch;
    err1=err1+grad*pitch;
    r(i)=rnom*(1+err);
    r1(i)=rnom*(1+err1);
end

for i=(m/2+1):(m-1),
    err=err+grad*pitch;
    err1=err1-grad*pitch;
    r(i)=rnom*(1+err);
    r1(i)=rnom*(1+err1);
end

err=err+grad*pitch/2;
err1=err1-grad*pitch/2;
r(m)=rnom*(1+err)/2;
r1(m)=rnom*(1+err1)/2;

%-------------------------------------------------------------------------%
%                            total resistances                            %
%-------------------------------------------------------------------------%

rtot=sum(r);
rtot1=sum(r1);
rntot=sum(rn);

%-------------------------------------------------------------------------%
%                                voltages                                 %
%-------------------------------------------------------------------------%

v(1)=r(1)/rtot;
v1(1)=r1(1)/rtot1;
vid(1)=rn(1)/rntot;
for i=2:m,
    v(i)=sum(r(1:i))/rtot;
    v1(i)=sum(r1(1:i))/rtot1;
    vid(i)=sum(rn(1:i))/rntot;
end

%-------------------------------------------------------------------------%
%                                errors                                   %
%-------------------------------------------------------------------------%

verr=(vid-v)*(m-1);
verr1=(vid-v1)*(m-1);

figure (1)
n=[1:m];
plot(n, verr, n,verr1);
xlabel('bin');
ylabel('INL [LSB]');
xlim([1 m]);
legend('single straight line','folded')
grid;
max(r)/rnom
max(r1)/rnom




