%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%                              Example 7.4                                %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%                                                                         %
%   2nd Order Sigma-Delta A/D Modulator                                   %
%   The modulator structure is simulated using Simulink (Paral).          %
%   Post-processing of the results is done with Matlab.5                  %
%       1. Plots the Power Spectral Density of the bit-stream             %
%       2. Calculates the SNR                                             %
%       3. Calculates histograms at the integrator outputs                %
%                                                                         %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

clear all;

t0=clock;

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

bw=30e3;				% Base-band
R=50;
Fs=R*2*bw;				% Oversampling frequency
Ts=1/Fs;
N=1024*8;				% Samples number
Tstop=Ts*(N+1000);
nper=27;
Fin=nper*Fs/N;          % Input signal frequency (Fin = nper*Fs/N)

Ampl=0.5-pi/128;		% Input signal amplitude [V]
Vref=1;
QI=0.125;
Ntransient=0
%
finrad=Fin*2*pi;		% Input signal frequency in radians
%
offset=0.0;
phasejitter=0.00;
gainerror=0.00;

%-------------------------------------------------------------------------%
%                       Open Simulink diagram first                       %
%-------------------------------------------------------------------------%

options=simset('RelTol', 1e-3, 'MaxStep', 1/Fs);
sim('Ex7_4', (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=N*(bw/Fs)                % Base-band frequency bins
yy1=zeros(1,N);
yy1=yout(2+Ntransient:1+N+Ntransient)';

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

%-------------------------------------------------------------------------%
%                               Plots                                     %
%-------------------------------------------------------------------------%
figure(1);
plot(linspace(0,Fs/2,N/2), ptot(1:N/2), 'r');
grid on;
title('PSD of a 3-Path Sigma-Delta Modulator')
xlabel('Frequency [Hz]')
ylabel('PSD [dB]')
axis([0 Fs/2 -150 0]);

figure(2);
clf;
semilogx(linspace(0,Fs/2,N/2), ptot(1:N/2), 'r');
grid on;
title('PSD of a 3-Path Sigma-Delta Modulator')
xlabel('Frequency [Hz]')
ylabel('PSD [dB]')
axis([0 Fs/2 -150 0]);

figure(3);
clf;
plot(linspace(0,Fs/2,N/2), ptot(1:N/2), 'r');
hold on;
title('PSD of a 3-Path Sigma-Delta Modulator (detail)')
xlabel('Frequency [Hz]')
ylabel('PSD [dB]')
axis([0 2*(Fs/R) -150 0]);
grid on;
hold off;
text_handle = text(floor(Fs/R),-40, sprintf('SNR = %4.1fdB @ OSR=%d\n',snr,R));
text_handle = text(floor(Fs/R),-60, sprintf('Rbit = %2.2f bits @ OSR=%d\n',Rbit,R));

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