clear all
for i=1:9
    load(strcat('SCRN000', num2str(i), '.TXT'));
end
for i=10:15
     load(strcat('SCRN00', num2str(i), '.TXT'));
end

for i=1:length(SCRN0003(:,1))
    if SCRN0003(1,1)< max(SCRN0004(:,1))
        SCRN0003(1, :)=[]; %delete low frequencies from 100kHz spans
        SCRN0008(1,:)=[];
        SCRN0009(1,:)=[];
        SCRN0013(1,:)=[];
    end
end
    
for i=1:length(SCRN0004(:,1))
    if SCRN0004(1,1)< max(SCRN0005(:,1))
        SCRN0004(1, :)=[]; %delete low frequencies from 3.2kHz spans
        SCRN0007(1,:)=[];
        SCRN0010(1,:)=[];
        SCRN0014(1,:)=[];
    end
end

        SCRN0005(1, :)=[]; %delete lowest frqeuency point
        SCRN0006(1,:)=[];
        SCRN0011(1,:)=[];
        SCRN0015(1,:)=[];

PFDcal=36*pi/180;%36degrees per volt D1002471-v2
%end station PLL Imon, MC unlocked, MPC off, PDH unlocked
PLLMPCoff=[SCRN0005;SCRN0004;SCRN0003];
calPLLoff=[PLLMPCoff(:, 1),PFDcal.*PLLMPCoff(:, 2)]; 
%end station PLL Imon, MC unlocked, MPC on, PDH locked
PLLMPCon=[SCRN0011;SCRN0010;SCRN0009]; 
calPLLon=[PLLMPCon(:,1),PFDcal.*PLLMPCon(:, 2)]; 
%end station PDH Imon, MC unlocked, MPC off
PDHMPCoff=[SCRN0006;SCRN0007;SCRN0008];
%end station PDH Imon, MC unlocked, MPC on
PDHMPCon=[SCRN0015;SCRN0014;SCRN0013];

data(:,:,1)=calPLLoff;
data(:,:,2)=calPLLon;
data(:,:,3)=PDHMPCoff;
data(:,:,4)=PDHMPCon;

for i=1:4
    rms(:, i)=[(-1.*cumtrapz(flipud(data(:,1, i)), flipud(data(:, 2,i)).^2)).^(0.5)]';
end
rms=flipud(rms);
%(cumtrapz(myfreqb,blrmsSqz(1:285).^2)).^(.5);  %my mehtod agrees with DTT
freq=data(:,1,1);
figure(1)
loglog(SCRN0001(:,1), SCRN0001(:,2), SCRN0002(:,1), SCRN0002(:,2)) %1 was COMM PLL Imon, with MCP off, not sure about 2, I thought it was MPC on
h2=figure(2);
loglog(freq, PLLMPCon(:,2), 'b',freq, PLLMPCoff(:,2), 'r', ...
    freq, rms(:,2), 'b--', freq, rms(:,1), 'r--');
ylabel('radians/rt Hz')
xlabel('Hz')
title('X end fiber locking PLL')
legend('Polarization Controler On', 'Polarization Controler Off', ...
     'RMS polarization Contoller on', 'RMS polarization Contoller off', 'Location', 'SouthWest')
xlim([1, 100000])
print(h2, '-djpeg', 'PLLPolCntrl')

h3=figure(3);
loglog(PDHMPCon(:,1), PDHMPCon(:,2),'b',PDHMPCoff(:,1), PDHMPCoff(:,2), 'r', ...
        freq, rms(:,4), 'b--', freq, rms(:,3), 'r--');
    xlabel('Hz')
    ylabel('Vrms/rt Hz at Imon')
    legend('Polarization Controler On', 'Polarization Controler Off', ...
     'RMS polarization Contoller on', 'RMS polarization Contoller off', 'Location', 'SouthWest')
 title('End station PDH signal')
xlim([1, 100000])
print(h3, '-djpeg', 'PDHPolCntrl')
