function  [ADC_out,Dig_out] = ADC(in,n,Vref);


Dig_out=floor((in+Vref)*2^(n-1)/Vref); %Digital output of the ADC
Dig_out(Dig_out<0)=0;% Clipping
Dig_out(Dig_out>2^n-1)=2^n-1;%Clipping
ADC_out=(Dig_out-2^(n-1))/2^(n-1)*Vref+Vref/2^n;%

endfunction