%5. non-uniform quantization
N = 10; % #bits in DAC.
ref_power = 0.5; % power of sine of ampl 1.

dB2ampl = @(dB) sqrt((10^((dB)/10))/0.5);
sine_pow = -20:0.5:3;

%R2R ladder with 10 bits resolution and 2% std deviation for each resistor.
for j = 1:10
    r2rvect = r2r(N, 0.04);

    i = 1; % for loop index
    SNRvect5= zeros(length(sine_pow), 1);
    for sine_pow_i = sine_pow
        sine_pow_i = sine_pow_i+pow2db(ref_power);
        sine_ampl = dB2ampl(sine_pow_i);
        sim('lab3c.slx');
        table5=sigspectrum(LUTout5, 'noplot');
        SNRvect5(i)=table5(6)/(sum(table5)-table5(6));

        i=i+1;
    end

    plot(sine_pow,  10*log10(SNRvect5), '-'); grid on; xlabel('P_{in} [dB]'); ylabel('SNR_{dB}');
    hold on
end