%% Lab 3
%% 3. SNR

lvls2n = @(lvls) ceil(log2(lvls)) ;

%

lvls = 15;
plot(lutix(lvls), lutdata(lvls)); xlabel('lutix (breakpoints)');ylabel('lutdata (mapped values)');

%

lvls = 4;
sim('lab3a.slx');
sigview(LUTout, SINEout)
lvls = 15;
sim('lab3a.slx');
sigview(LUTout, SINEout)

%

lvls = 4;
sim('lab3a.slx')
sigspectrum(LUTout)

%

n = 3:14; % #bits
i = 1; % for loop index
SNRvect= zeros(11, 1);
for  elm = n
    lvls=2^elm;
    sim('lab3a.slx');
    table=sigspectrum(LUTout, 'noplot');
    SNRvect(i)=table(6)/(sum(table)-table(6));
    i = i+1;
end 

%

hold off
plot(n, 10*log10(SNRvect), 'o-'); grid on; xlabel('n bits');ylabel('SNR_{dB}'); hold on
plot(n, 6.02*n+1.76), legend('SNR calculated', 'SNR ideal')

%
%%
%%
%%
%%
%%
%%
%
%%
%% 4. SNR vs. input power


ref_power = 0.5; % power of sine of ampl 1.

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

i = 1; % for loop index
SNRvect4= 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('lab3b.slx');
    table4=sigspectrum(LUTout4, 'noplot');
    SNRvect4(i)=table4(6)/(sum(table4)-table4(6));
    
    i=i+1;
end

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

%
%% 5. non-uniform quantization

N = 10; % #bits in DAC.

% R2R ladder with 10 bits resolution and 2% std deviation for each resistor.

r2rvect = r2r(N, 0.04);

ref_power = 0.5; % power of sine of ampl 1.

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

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

%
%% 4% std deviation

N = 10; % #bits in DAC.
r2rvect = r2r(N, 0.04);
ref_power = 0.5; % power of sine of ampl 1.
dB2ampl = @(dB) sqrt((10^((dB)/10))/0.5);
sine_pow = -20:0.5:3;
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), 'o-'); grid on; xlabel('P_{in} [dB]'); ylabel('SNR_{dB}');

%
%% 6. Sample Jitter

sine_ampl = 1; f_sine = 5; %Hz
N=10;
r2rvect = r2r(N);
sim('lab3d');
sigview(LUTout6_jittered, LUTout6) 
sigspectrum(LUTout6_jittered, LUTout6) 

% SNR for quantized signal with and without jitter

spectra = sigspectrum(LUTout6, 'noplot');
spectra_jittered = sigspectrum(LUTout6_jittered, 'noplot');
LUTout6_jittered_save = LUTout6_jittered;

SNR_Q =             10*log10( spectra(6)/(sum(spectra)-spectra(6)) )
SNR_Q_jittered =    10*log10( spectra_jittered(6)/(sum(spectra_jittered)-spectra_jittered(6)) )

%
% Input sine increased to 50Hz

f_sine = 50;
sim('lab3d');
spectra_jittered_50Hz = sigspectrum(LUTout6_jittered, 'noplot');

% Comparison between original 5Hz and 50Hz power spectra.

sigspectrum(LUTout6_jittered_save, LUTout6_jittered)

% SNR for each

spectra_5Hz_jittered = sigspectrum(LUTout6_jittered_save, 'noplot');
spectra_50Hz_jittered= sigspectrum(LUTout6_jittered, 'noplot');

SNR_Q_5Hz_jittered =    10*log10( spectra_5Hz_jittered(6)/(sum(spectra_5Hz_jittered)-spectra_5Hz_jittered(6)) )
SNR_Q_50Hz_jittered =   10*log10( max(spectra_50Hz_jittered)/(sum(spectra_50Hz_jittered)-  max(spectra_50Hz_jittered) ) )

%

sine_pow = -20:3;
f_sine = 50;

dB2ampl = @(dB) sqrt((10^((dB)/10))/0.5)

sig_pow = [];
noise_pow = [];
j = 1;
for i = sine_pow
    sine_ampl = dB2ampl(i)%+pow2db(0.5));
    sim('lab3d');
    spec = sigspectrum(LUTout6_jittered);
    sig_pow(j) = 10*log10(     spec(51) );
    noise_pow(j) = 10*log10(   sum(spec)-spec(51)   );
    
    j = j+1;
end

%

sigview(LUTout6_jittered)

%


plot(sine_pow, sig_pow, sine_pow, noise_pow);grid on
xlabel('P_{in} [dB]');ylabel('P_{out} [dB]');legend('Signal power', 'Noise power')

SNR_plot = sig_pow-noise_pow
plot(sine_pow, SNR_plot);grid on
xlabel('P_{in} [dB]');ylabel('SNR [dB]');
ylim([0 40])