printFigs = true;
%%
cd /Users/kissel/Desktop/scratch/2017-05-30;

load SCRN0005.TXT
load SCRN0006.TXT
load SCRN0007.TXT
load SCRN0008.TXT
load SCRN0009.TXT
load SCRN0010.TXT

%%
carmugf_noboost = SCRN0005(find(SCRN0005(:,2)<0,1,'first'),1);
carmugf_wiboost = SCRN0007(find(SCRN0007(:,2)<0,1,'first'),1);
imcugf = SCRN0009(find(SCRN0009(:,2)<0,1,'first'),1);

carmugf_nb_mag = SCRN0005(find(SCRN0005(:,2)<0,1,'first'),2);
carmugf_wb_mag = SCRN0007(find(SCRN0007(:,2)<0,1,'first'),2);
imcugf_mag = SCRN0009(find(SCRN0009(:,2)<0,1,'first'),2);

carmugf_nb_pha = SCRN0006(find(SCRN0005(:,2)<0,1,'first'),2);
carmugf_wb_pha = SCRN0008(find(SCRN0007(:,2)<0,1,'first'),2);
imcugf_pha = SCRN0010(find(SCRN0009(:,2)<0,1,'first'),2);

%%
figure(1);
subplot(211);
ll = semilogx(SCRN0005(:,1),[SCRN0005(:,2) SCRN0007(:,2)],...
         carmugf_noboost,carmugf_nb_mag,'o',...
         carmugf_wiboost,carmugf_wb_mag,'s');
title('CARM OLG TF');
ylabel('Magnitude dB([V/V])');
xlabel('Frequency [Hz]');
legend('No IMC Boost','With IMC Boost')
set(ll,'MarkerSize',12)
set(gca,'XTick',10.^(3:5),'YTick',-40:10:40)

subplot(212);
ll = semilogx(SCRN0006(:,1),[SCRN0006(:,2) SCRN0008(:,2)],...
         carmugf_noboost,carmugf_nb_pha,'o',...
         carmugf_wiboost,carmugf_wb_pha,'s');
xlabel('Frequency [Hz]');
ylabel('Phase [deg]');
title({['No IMC Boost CARM UGF: ' num2str(carmugf_noboost/1000,3) ' kHz '];...
       ['With IMC Boost CARM UGF: ' num2str(carmugf_wiboost/1000,3) ' kHz']})
set(ll,'MarkerSize',12)
set(gca,'XTick',10.^(3:5),'YTick',-180:45:180)
legend(['Phase Margin: ' num2str(abs(-180 - carmugf_nb_pha),3) ' deg'],...
       ['Phase Margin: ' num2str(abs(-180 - carmugf_wb_pha),3) ' deg'],...
       'Location','North')

figure(2);
subplot(211);
ll = semilogx(SCRN0009(:,1),SCRN0009(:,2),...
         imcugf,imcugf_mag,'o');
title('IMC OLG TF');
ylabel('Magnitude dB([V/V])');
xlabel('Frequency [Hz]');
set(ll,'MarkerSize',12)
set(gca,'XTick',10.^(3:5),'YTick',-10:10:80)

subplot(212);
ll = semilogx(SCRN0010(:,1),SCRN0010(:,2),...
         imcugf,imcugf_pha,'o');
xlabel('Frequency [Hz]');
ylabel('Phase [deg]')
title(['No IMC Boost IMC UGF: ' num2str(imcugf/1000,3) ' kHz'])
set(ll,'MarkerSize',12)
set(gca,'XTick',10.^(3:5),'YTick',-180:45:180)
legend(['Phase Margin: ' num2str(abs(-180 - imcugf_pha),3) ' deg'])

%%
if printFigs
    figure(1)
    FillPage('w')
    IDfig(', J. Kissel')
    saveas(gcf,'2017-05-30_H1CARM_OLGTF.pdf')
    
    figure(2)
    FillPage('w')
    IDfig(', J. Kissel')
    saveas(gcf,'2017-05-30_H1IMC_OLGTF.pdf')
end