% figure number set in calling routine
% figure(1)
clf
orient portrait
set(gcf,'Position',[100 50 600 800])
set(gcf,'PaperPosition',[1 .5 7 10])
subplot(3,2,1)
% Report values used

%Inductor & power
% text(0,1,'L =','HorizontalAlignment','Left', ...
%     'VerticalAlignment','Base')  
% text(.16,1,num2str(L),'HorizontalAlignment','Left', ...
%     'VerticalAlignment','Base')  
% text(0,.9,'K_{PD} =','HorizontalAlignment','Left', ...
%     'VerticalAlignment','Base')
% text(.16,.9,num2str(K_PD),'HorizontalAlignment','Left', ...
%     'VerticalAlignment','Base')

% photodiode
% text(0,.6,'R_{F} =','HorizontalAlignment','Left', ...
%     'VerticalAlignment','Base')
% text(.16,.6,num2str(R_F),'HorizontalAlignment','Left', ...
%     'VerticalAlignment','Base')
% text(0,.5,'C_{F} =','HorizontalAlignment','Left', ...
%     'VerticalAlignment','Base')
% text(.16,.5,num2str(C_F),'HorizontalAlignment','Left', ...
%     'VerticalAlignment','Base')
% text(0,.4,'C_{PD} =','HorizontalAlignment','Left', ...
%     'VerticalAlignment','Base')
% text(.16,.4,num2str(C_PD),'HorizontalAlignment','Left', ...
%     'VerticalAlignment','Base')
% text(0,.3,'f_{o} =','HorizontalAlignment','Left', ...
%     'VerticalAlignment','Base')
% text(.16,.3,[ num2str(w_o/(2E+6*pi),'%6.1f') ' MHz'],'HorizontalAlignment','Left', ...
%     'VerticalAlignment','Base')

% Report on approximation f_T_approx
% text(0,0,'f_{T(APPROX)} =','HorizontalAlignment','Left', ...
%     'VerticalAlignment','Base')
% text(.5,0,[num2str(f_T_approx/1000) ' kHz'],'HorizontalAlignment','Left', ...
%     'VerticalAlignment','Base')
% 




% Control amp
% text(.5,1,'R_{Z} =','HorizontalAlignment','Left', ...
%     'VerticalAlignment','Base')
% text(.66,1,num2str(R_Z),'HorizontalAlignment','Left', ...
%     'VerticalAlignment','Base')
% text(.5,.9,'C_{I} =','HorizontalAlignment','Left', ...
%     'VerticalAlignment','Base')
% text(.66,.9,num2str(C_I),'HorizontalAlignment','Left', ...
%     'VerticalAlignment','Base')
% text(.5,.8,'R_{PD} =','HorizontalAlignment','Left', ...
%     'VerticalAlignment','Base')
% text(.66,.8,num2str(R_I),'HorizontalAlignment','Left', ...
%     'VerticalAlignment','Base')
% text(.5,.7,'C_{FI} =','HorizontalAlignment','Left', ...
%     'VerticalAlignment','Base')
% text(.66,.7,num2str(C_FI),'HorizontalAlignment','Left', ...
%     'VerticalAlignment','Base')

% Scaling stuff at PWM amplifier
% text(.5,.5,'R_{5} =','HorizontalAlignment','Left', ...
%     'VerticalAlignment','Base')
% text(.66,.5,num2str(R_5),'HorizontalAlignment','Left', ...
%     'VerticalAlignment','Base')
% text(.5,.4,'R_{46t} =','HorizontalAlignment','Left', ...
%     'VerticalAlignment','Base')
% text(.66,.4,num2str(R_46t),'HorizontalAlignment','Left', ...
%     'VerticalAlignment','Base')
% text(.5,.3,'R_{9} =','HorizontalAlignment','Left', ...
%     'VerticalAlignment','Base')
% text(.66,.3,num2str(R_9),'HorizontalAlignment','Left', ...
%     'VerticalAlignment','Base')

axis off

% plot pole locations
subplot(3,2,1)
hold off
plot(real(pole_loc(1)),imag(pole_loc(1)),'x','MarkerSize',16)
hold on
plot(real(pole_loc(2)),imag(pole_loc(2)),'x','MarkerSize',16)
ylim([-5e6 5e6])
xlim([-1e7 1e6])
grid on
title('CLOSED LOOP POLES')
xlabel('Real axis')
ylabel('Imaginary axis')

subplot(3,2,3)
hold off
loglog(f,magAOL,'LineWidth',2)    % plot magnitude
hold on
% loglog(f,magT_approx,'k')    % plot approximate magnitude
plot(fT,1,'or')
text(fT,1,['f_T = ' num2str(fT/1000,'%6.0f') ' kHz'], ...
    'HorizontalAlignment','Left', ...
    'VerticalAlignment','Base', ...
    'FontWeight','Bold')
grid on
ylim([1e-4 1e+2])
xlim([1e+4 1e+8])
title('LOOP GAIN MAG, PHASE')

subplot(3,2,5)
hold off
semilogx(f,phaseAOL,'LineWidth',2)
hold on
plot(fT,pfT,'or')
text(fT,pfT,['PM = ' num2str(pmargin,'%5.0f') ' ^o'], ...
    'HorizontalAlignment','Left', ...
    'VerticalAlignment','Base', ...
    'FontWeight','Bold')

grid on
ylim([-200 10])
xlim([1e+4 1e+8])
xlabel('freq [Hz]')


subplot(3,2,4)
hold off
loglog(f,magGCL,'LineWidth',2)
grid on
ylim([1e-5 10])
xlim([1e+4 1e+8])
title('CLOSED LOOP GAIN MAG, PHASE')

subplot(3,2,6)
hold off
semilogx(f,phaseGCL,'LineWidth',2)
grid on
ylim([-200 10])
xlim([1e+4 1e+8])
xlabel('freq [Hz]')

% Step response of closed loop system over 100usec
subplot(3,2,2)
[ystep,tstep] = step(G,5E-6);
plot(tstep,ystep,'Linewidth',2)
ylim([0 2])
xlim([0 5e-6])
title('Closed loop step response')
grid on
xlabel('Time [\mu sec]')
