function adc = dsexample1
% Design example for a lowpass or bandpass modulator.

% Design parameters
order = 3;
osr = 16;
opt = 1;
Hinf = 2.5;
M = 8;
f0 = 0;
form = 'CRFB';
quadrature = 0;

% Derived parameters
nlev = M + 1;

% Plot control
global LiveDemo
clc
if f0 == 0
type = 'Lowpass';
else
type = 'Bandpass';
end
fprintf( 1, '\t\t\t%dth-Order %s Example\n\n', order, type );
fig_pos = { ...
[ 10 820 620 350]
[ 650 820 620 350]
[ 10 440 620 350]
[ 650 440 400 350]
[1070 440 430 350]
};
if LiveDemo
sizes.lw = 3; % LineWidth
sizes.ms = 8; % MarkerSize
sizes.fs = 18; % FontSize
sizes.fw = 'bold'; % FontWeight
else
sizes.lw = 1; % LineWidth
sizes.ms = 5; % MarkerSize
sizes.fs = 12; % FontSize
sizes.fw = 'normal'; % FontWeight
end

% NTF synthesis and realization
fprintf(1,'Doing NTF synthesis and realization... ');
design_step = 1;
ntf = synthesizeNTF(order,osr,opt,Hinf,f0); % Optimized zero placement
[a,g,b,c] = realizeNTF(ntf,form);
b = [b(1) zeros(1,length(b)-1)]; % Use a single feed-in for the input
ABCD = stuffABCD(a,g,b,c,form);
fprintf(1,'Done.\n');
figure(design_step); clf

set(design_step,'position',fig_pos{design_step});
DocumentNTF(ABCD,osr,f0,quadrature,sizes,1);
drawnow;

% Time-domain simulations
fprintf(1,'Doing time-domain simulations... ');
design_step = design_step+1;
figure(design_step); clf; subplot('position', [0.05 0.12 0.9 0.78]);
set(gcf,'name','Time Domain')
set(gcf,'numbertitle','off');
set(gcf,'MenuBar','none');
set(design_step,'position',fig_pos{design_step});
% Time-domain plot
N = 100;
t = 0:N-1;
u = undbv(-3)*M*sin( 2*pi*(f0+1/N)*t );
v = simulateDSM(u,ABCD,nlev);
h = stairs( t, u, 'g');
set(h,'Linewidth',sizes.lw);
hold on;
h = stairs( t, v, 'b');
set(h,'Linewidth',sizes.lw);
figureMagic([0 N],10,5, [-M M],2,1);
xlabel('Sample Number');
% Example spectrum
design_step = design_step+1;
figure(design_step); clf; subplot('position', [0.05 0.12 0.9 0.78]);
set(gcf,'name','Example Spectrum')
set(gcf,'numbertitle','off');
set(gcf,'MenuBar','none');
set(design_step,'position',fig_pos{design_step});
PlotExampleSpectrum(ntf,M,osr,f0,quadrature,sizes);
%figureMagic([0 0.5], 1/16, 2, [-140 0], 10, 2, [5.5 3], 'Example Spectrum');
% SQNR plot
design_step = design_step+1;
figure(design_step); clf;
set(design_step,'position',fig_pos{design_step});
if nlev==2
[snr_pred,amp_pred] = predictSNR(ntf,osr);
plot(amp_pred,snr_pred,'-');
hold on;
end
[snr,amp] = simulateSNR(ntf,osr,[],f0,nlev);
fprintf(1,'Done.\n');
h = plot(amp,snr,'ob', 'Linewidth',sizes.lw);
set(h, 'MarkerSize',sizes.ms);
figureMagic([-120 0], 10, 2, [0 120], 10, 2, [3 3], 'SQNR vs. Input Level');
xlabel('Input Level (dBFS)');
ylabel('SQNR (dB)');
[peak_snr,peak_amp] = peakSNR(snr,amp);
msg = sprintf('Peak SQNR = %4.1fdB \n@ A = %4.1f dBFS',peak_snr,peak_amp);
h = text(-60,117,msg,'hor','cen', 'vertical','top');
set(h, 'FontSize',sizes.fs, 'FontWeight',sizes.fw);
drawnow;

if ~LiveDemo % Dynamic range scaling
fprintf(1,'Doing dynamic range scaling... ');
design_step = design_step+1;
figure(design_step); clf;
set(design_step,'position',fig_pos{design_step});
ABCD0 = ABCD;
[ABCD, umax] = scaleABCD(ABCD0,nlev,f0);
[a,g,b,c] = mapABCD(ABCD,form);
fprintf(1,'Done.\n');
fprintf(1,'Verifying dynamic range scaling... ');
u = linspace(0,0.95*umax,30);
N = 1e4; N0 = 50;
test_tone = cos(2*pi*f0*[0:N-1]);
test_tone(1:N0) = test_tone(1:N0) .* (0.5-0.5*cos(2*pi/N0*[0:N0-1]));
maxima = zeros(order,length(u));
for i = 1:length(u)
ui = u(i);
[v,xn,xmax] = simulateDSM( ui*test_tone, ABCD, nlev ); %#ok<ASGLU>
maxima(:,i) = xmax(:);
if any(xmax>1e2)
fprintf(1,'Warning, umax from scaleABCD was too high.\n');
umax = ui;
u = u(1:i);
maxima = maxima(:,1:i);
break;
end
end
fprintf(1,'Done.\n');
colors = hsv(order);
cmd = 'legend( handles';
handles = zeros(1,order);
for i = 1:order
handles(i) = plot(u,maxima(i,:),'--','color',colors(i,:));
if i==1
hold on;
end
cmd = [ cmd ',''State ' num2str(i) '''' ]; %#ok<AGROW>
plot(u,maxima(i,:),'o','color',colors(i,:));
end
hold off; grid on;
text(umax/2,0.05,'DC Input','Hor','cen','Vert','mid');
figureMagic([ 0 umax],[],[], [0 1],0.1,2, [3 3],'State Maxima');
set(gca,'position', [0.1 0.07, 0.85, 0.85]);
cmd = [cmd ',4);'];
eval(cmd);
else
umax = [];
end

% The next step would be to size capacitors for each integrator state based
% on noise (kT/C) limits and area.

% Simulations modelling the effects of finite op-amp gain and capacitor
% errors should also be performed.

adc.order = order;
adc.osr = osr;
adc.nlev = nlev;
adc.f0 = f0;
adc.ntf = ntf;
adc.ABCD = ABCD;
adc.umax = umax;
adc.peak_snr = peak_snr;
adc.form = form;
adc.coefficients.a = a;
adc.coefficients.g = g;
adc.coefficients.b = b;
adc.coefficients.c = c;