%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%                              Example 8.2                                %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%


t0=clock;
%-------------------------------------------------------------------------%
%                   comment to freeze the mismatch                        %
%-------------------------------------------------------------------------%
  mism=0.002*rand(1,8);
  mism=mism-mean(mism);
  
% ************************************************************************

%-------------------------------------------------------------------------%
%                            Design variables                             %
%-------------------------------------------------------------------------%

R=20;
in_lvl = -2;        % input in dB
Fs=20e6;            % Oversampling frequency
bw=Fs/2/R;
Ts=1/Fs;
N=1024*4;           % Samples number
Vref=1;
Tstop=Ts*(N+300);
nper=19;
Fin=(nper*Fs/N);    % Input signal frequency (Fin = nper*Fs/N)
Ampl=Vref*20^(in_lvl/20);
Ntransient=0;
finrad=Fin*2*pi;	% Input signal frequency in radians
NComp=7;
Delta=2*(Vref)/NComp;
k=0.25;

%---------------------% Random Noise Generator %--------------------------%

flag=2;     % flag=2 no butterfly 
            % flag=1/2 butterfly

%-------------------------------------------------------------------------%
%                       Open Simulink diagram first                       %
%-------------------------------------------------------------------------%
%
options=simset('RelTol', 1e-3, 'MaxStep', 1/Fs);
sim('Ex8_2', (N+Ntransient)/Fs, options);	


%-------------------------------------------------------------------------%
%       Calculates SNR and PSD of the bit-stream and of the signal        %
%-------------------------------------------------------------------------%

w=hann(N);
f=Fin/Fs;                   % Normalized signal frequency
fB=round(N*(bw/Fs));		% Base-band frequency bins
yy1=zeros(1,N);
yy1=yout(2+Ntransient:1+N+Ntransient)';

ptot=zeros(1,N);
[snr,ptot]=calcSNRdef(yy1(1:N),f,fB,w',N);
Rbit=(snr-1.76)/6.02;       % Equivalent resolution in bits


%-------------------------------------------------------------------------%
%       Calculates Error Spectrum                                         %
%-------------------------------------------------------------------------%

w=hann(N);
f=Fin/Fs;                % Normalized signal frequency
fB=round(N*(bw/Fs));     % Base-band frequency bins
yy2=zeros(1,N);
yy2=yout1(2+Ntransient:1+N+Ntransient)';

ptot1=zeros(1,N);
[snr1,ptot1]=calcSNRdef(yy2(1:N),f,fB,w',N);

%-------------------------------------------------------------------------%
%                            Plots                                        %
%-------------------------------------------------------------------------%
ptot=ptot-max(ptot);
ptot1=ptot1-max(ptot)+ptot(1) -ptot1(1);

clf;
figure(1);
subplot(2,1,1)
semilogx(linspace(0,Fs/2,N/2), ptot1(1:N/2), 'r');
grid on;
title('PSD  of the Mismatch Error')
xlabel('Frequency [Hz]')
ylabel('PSD [dB]')
axis([0 Fs/2 -140 0]);
subplot(2,1,2)
semilogx(linspace(0,Fs/2,N/2), ptot(1:N/2), 'b');
grid on;
title('PSD of the Output')
xlabel('Frequency [Hz]')
ylabel('PSD [dB]')
axis([0 Fs/2 -140 0]);


s1=sprintf('   In_level=%1.3f,    SNR(dB)=%1.3f,    Rbit=%1.3f', in_lvl, snr,  Rbit);
s2=sprintf('');
max(yout)
%s2=sprintf('   Simulation time =%1.3f min',etime(clock,t0)/60);
disp(s1)
disp(s2)
