%% load data

CLF_FAST_lowpower = load('CLF_FAST_lowpower.TXT');
CLF_FAST_lowpower_x = CLF_FAST_lowpower(:,1);
CLF_FAST_lowpower_y = CLF_FAST_lowpower(:,2);
CLF_QMON_lowpower = load('CLF_QMON_lowpower.TXT');
CLF_QMON_lowpower_x = CLF_QMON_lowpower(:,1);
CLF_QMON_lowpower_y = CLF_QMON_lowpower(:,2);

CLF_FAST_10Xpower = load('CLF_FAST_10xpower.TXT');
CLF_FAST_10Xpower_x = CLF_FAST_10Xpower(:,1);
CLF_FAST_10Xpower_y = CLF_FAST_10Xpower(:,2);
CLF_QMON_10Xpower = load('CLF_QMON_10xpower.TXT');
CLF_QMON_10Xpower_x = CLF_QMON_10Xpower(:,1);
CLF_QMON_10Xpower_y = CLF_QMON_10Xpower(:,2);

CLF_FAST_100Xpower = load('CLF_FAST_100xpower_noRFamp.TXT');
CLF_FAST_100Xpower_x = CLF_FAST_100Xpower(:,1);
CLF_FAST_100Xpower_y = CLF_FAST_100Xpower(:,2);
CLF_QMON_100Xpower = load('CLF_QMON_100xpower_noRFamp.TXT');
CLF_QMON_100Xpower_x = CLF_QMON_100Xpower(:,1);
CLF_QMON_100Xpower_y = CLF_QMON_100Xpower(:,2);



CLF_DN = load('SCRN0136.TXT');
CLF_DN_x = CLF_DN(:,1);
CLF_DN_y = CLF_DN(:,2);

%% Sensing noise
fudgefactor = 1.15;
gain = 1.2e6;
DN = 8.9e-6;
%I_DN = (3*0.0186)-(3*0.0045);
%I_DN1 = ((DN/(5*sqrt(2)*gain)) * (1/sqrt(2*1.6e-19)))^2;

SN_lowpower = sqrt(2*1.6e-19*(3*0.005e-3))*gain; 
Sens_lowpower = sqrt(SN_lowpower^2+DN^2); 

SN_10Xpower = sqrt(2*1.6e-19* (3*0.05e-3) )*gain; 
%SN_10Xpower = sqrt(DNSN_10Xpower^2 - DN^2);
Sens_10Xpower = sqrt((SN_10Xpower^2)+DN^2);

%SN_100Xpower = 35000*sqrt(2*1.6e-19*0.076e-3)*5*sqrt(2)*fudgefactor;
%DN_100Xpower = 35000*sqrt(2*1.6e-19*0.0135e-3)*5*sqrt(2)*fudgefactor;
SN_100Xpower = sqrt(2*1.6e-19*3*0.314e-3)*gain/25;
%SN_100Xpower = DNSN_100Xpower - (DN/40);
%DN_100Xpower = (35000)*sqrt(2*1.6e-19*0.0135e-3)*5*sqrt(2)*fudgefactor;

Sens_100Xpower = sqrt((SN_100Xpower^2)+(DN/25)^2);

%sens_10xpower = 
%sens_100xpower = 


%% rms

QMON_cal_lowpower = 328e-3; %Vpp
QMON_cal_10Xpower = 10*328e-3; %Vpp
QMON_cal_100Xpower = 4.9; %Vpp
QMON_cal_100Xpower_noRFamp = 500e-3; %Vpp


VCXOV2Hz = 925; %Hz/V

%CLF QMON lowpower rms
flipdiff3 = flip(diff(CLF_QMON_lowpower_x));
flipPSD3 = flip((1e3.*CLF_QMON_lowpower_y/(QMON_cal_lowpower)).^2);
meansquare3 = cumsum(flipPSD3(1:end-1).*flipdiff3);
CLF_QMON_lowpower_rms = flip(sqrt(meansquare3));

%CLF FAST lowpower rms
flipdiff4 = flip(diff(CLF_FAST_lowpower_x));
flipPSD4 = flip((1e3.*CLF_FAST_lowpower_y.*VCXOV2Hz./CLF_FAST_lowpower_x).^2);
meansquare4 = cumsum(flipPSD4(1:end-1).*flipdiff4);
CLF_FAST_lowpower_rms = flip(sqrt(meansquare4));

%CLF QMON 10Xpower rms
flipdiff5 = flip(diff(CLF_QMON_10Xpower_x));
flipPSD5 = flip((1e3.*CLF_QMON_10Xpower_y/(QMON_cal_10Xpower)).^2);
meansquare5 = cumsum(flipPSD5(1:end-1).*flipdiff5);
CLF_QMON_10Xpower_rms = flip(sqrt(meansquare5));

%CLF FAST 10Xpower rms
flipdiff6 = flip(diff(CLF_FAST_10Xpower_x));
flipPSD6 = flip((1e3.*CLF_FAST_10Xpower_y.*VCXOV2Hz./CLF_FAST_10Xpower_x).^2);
meansquare6 = cumsum(flipPSD6(1:end-1).*flipdiff6);
CLF_FAST_10Xpower_rms = flip(sqrt(meansquare6));

%CLF QMON 100Xpower rms
flipdiff7 = flip(diff(CLF_QMON_100Xpower_x));
flipPSD7 = flip((1e3.*CLF_QMON_100Xpower_y/(QMON_cal_100Xpower_noRFamp)).^2);
meansquare7 = cumsum(flipPSD7(1:end-1).*flipdiff7);
CLF_QMON_100Xpower_rms = flip(sqrt(meansquare7));

%CLF FAST 100Xpower rms
flipdiff8 = flip(diff(CLF_FAST_100Xpower_x));
flipPSD8 = flip((1e3.*CLF_FAST_100Xpower_y.*VCXOV2Hz./CLF_FAST_100Xpower_x).^2);
meansquare8 = cumsum(flipPSD8(1:end-1).*flipdiff8);
CLF_FAST_100Xpower_rms = flip(sqrt(meansquare8));


%%
figure(200)
p5 = loglog(CLF_FAST_lowpower_x, 1e3.*CLF_FAST_lowpower_y.*VCXOV2Hz./CLF_FAST_lowpower_x ,'LineWidth', 2, 'Color', [0.9290, 0.6940, 0.1250]);
xlabel('Frequency, Hz'), ylabel('mrad/\surd Hz');
grid on;
hold on
p6 = loglog(CLF_QMON_lowpower_x, 1e3.*CLF_QMON_lowpower_y/(QMON_cal_lowpower),'LineWidth', 2, 'Color',[0.8500, 0.3250, 0.0980]);
p7 = loglog(CLF_DN_x, 1e3.*CLF_DN_y/(QMON_cal_lowpower) , '--','LineWidth', 2, 'Color', [0.25, 0.25, 0.25]);
p7.Color(4) =  0.5;

p9 = loglog(CLF_QMON_lowpower_x(1:end-1), CLF_QMON_lowpower_rms, '--', 'LineWidth', 1.5, 'Color', [0.8500, 0.3250, 0.0980]); 
p10 = loglog(CLF_FAST_lowpower_x(1:end-1), CLF_FAST_lowpower_rms, '--', 'LineWidth', 1.5, 'Color', [0.9290, 0.6940, 0.1250]); 

p11 = loglog(CLF_QMON_lowpower_x, 1e3.*ones(size(CLF_QMON_lowpower_x)).*Sens_lowpower/(QMON_cal_lowpower) ,'LineWidth', 1.5, 'Color' ,[0.6350, 0.0780, 0.1840]);
p11.Color(4) = 0.5;

set(gca,'FontSize',18);
hTitle = title('CLF noise budget, 0.5uW trans, 1.3kHz UGF (Jan 24, 2019)');
xlim([10,1e5])
ylim([1e-4,1e2])
legend({'CLF control signal', 'CLF QMON (in-loop)', 'CLF QMON Darknoise', 'CLF QMON rms', 'CLF control signal rms', 'Sensing Noise'},'FontSize',12, 'Location', 'Northeast')
hold off
%% plot 10X power
figure(201)
loglog(CLF_FAST_10Xpower_x, 1e3.*CLF_FAST_10Xpower_y.*VCXOV2Hz./CLF_FAST_10Xpower_x ,'LineWidth', 2, 'Color', [0.9290, 0.6940, 0.1250]);
xlabel('Frequency, Hz'), ylabel('mrad/\surd Hz');
grid on;
hold on
loglog(CLF_QMON_10Xpower_x, 1e3.*CLF_QMON_10Xpower_y/(QMON_cal_10Xpower),'LineWidth', 2, 'Color',[0.8500, 0.3250, 0.0980]);
p12 = loglog(CLF_DN_x, 1e3.*CLF_DN_y/(QMON_cal_10Xpower) , '--','LineWidth', 2, 'Color', [0.25, 0.25, 0.25]);
p12.Color(4) =  0.5;
loglog(CLF_QMON_10Xpower_x(1:end-1), CLF_QMON_10Xpower_rms, '--', 'LineWidth', 1.5, 'Color', [0.8500, 0.3250, 0.0980]); 
loglog(CLF_FAST_10Xpower_x(1:end-1), CLF_FAST_10Xpower_rms, '--', 'LineWidth', 1.5, 'Color', [0.9290, 0.6940, 0.1250]); 

p14 = loglog(CLF_QMON_10Xpower_x, 1e3.*ones(size(CLF_QMON_10Xpower_x)).*Sens_10Xpower/(QMON_cal_10Xpower) ,'LineWidth', 1.5, 'Color', [0.6350, 0.0780, 0.1840]);
p14.Color(4) = 0.5;

set(gca,'FontSize',18);
hTitle = title('CLF noise budget, 4uW trans, 3.9kHz UGF (Jan 24, 2019)');
xlim([10,1e5])
ylim([1e-4,1e2])
legend({'CLF control signal', 'CLF QMON (in-loop)', 'CLF QMON Darknoise', 'CLF QMON rms', 'CLF control signal rms', 'Sensing Noise'},'FontSize',12, 'Location', 'Northeast')
hold off

%% Plot 100X power

figure(202)
loglog(CLF_FAST_100Xpower_x, 1e3.*CLF_FAST_100Xpower_y.*VCXOV2Hz./CLF_FAST_100Xpower_x ,'LineWidth', 2, 'Color', [0.9290, 0.6940, 0.1250]);
xlabel('Frequency, Hz'), ylabel('mrad/\surd Hz');
grid on;
hold on
loglog(CLF_QMON_100Xpower_x, 1e3.*CLF_QMON_100Xpower_y/(QMON_cal_100Xpower_noRFamp),'LineWidth', 2, 'Color',[0.8500, 0.3250, 0.0980]);
p13 = loglog(CLF_DN_x, 1e3.*CLF_DN_y/(25.*QMON_cal_100Xpower_noRFamp) , '--','LineWidth', 2, 'Color', [0.25, 0.25, 0.25]); %DN taken before RF amp out divided by 15
p13.Color(4) =  0.5;
loglog(CLF_QMON_100Xpower_x(1:end-1), CLF_QMON_100Xpower_rms, '--', 'LineWidth', 1.5, 'Color', [0.8500, 0.3250, 0.0980]); 
loglog(CLF_FAST_100Xpower_x(1:end-1), CLF_FAST_100Xpower_rms, '--', 'LineWidth', 1.5, 'Color', [0.9290, 0.6940, 0.1250]); 

p15 = loglog(CLF_QMON_100Xpower_x, 1e3.*ones(size(CLF_QMON_100Xpower_x)).*Sens_100Xpower/(QMON_cal_100Xpower_noRFamp) ,'LineWidth', 1.5, 'Color', [0.6350, 0.0780, 0.1840]);
p15.Color(4) = 0.5;

set(gca,'FontSize',18);
hTitle = title('CLF noise budget, 20uW trans, 2.48kHz UGF (Jan 24, 2019)');
xlim([10,1e5])
ylim([1e-4,1e2])
legend({'CLF control signal', 'CLF QMON (in-loop)', 'CLF QMON Darknoise', 'CLF QMON rms', 'CLF control signal rms', 'Sensing Noise'},'FontSize',12, 'Location', 'Northeast')
hold off

%% plot 10X power freq noise
figure(203)
loglog(CLF_FAST_10Xpower_x, 1e3.*CLF_FAST_10Xpower_y.*VCXOV2Hz ,'LineWidth', 2, 'Color', [0.9290, 0.6940, 0.1250]);
xlabel('Frequency, Hz'), ylabel('mHz/\surd Hz');
grid on;
hold on
loglog(CLF_QMON_10Xpower_x, CLF_QMON_10Xpower_x.*1e3.*CLF_QMON_10Xpower_y/(QMON_cal_10Xpower),'LineWidth', 2, 'Color',[0.8500, 0.3250, 0.0980]);
loglog(CLF_DN_x, CLF_DN_x.*1e3.*CLF_DN_y/(QMON_cal_10Xpower) , '--','LineWidth', 2, 'Color', [0.8500, 0.3250, 0.0980]);
%p7.Color(4) =  0.5;
%loglog(CLF_QMON_10Xpower_x(1:end-1), CLF_QMON_10Xpower_rms, '--', 'LineWidth', 1.5, 'Color', [0.8500, 0.3250, 0.0980]); 
%loglog(CLF_FAST_10Xpower_x(1:end-1), CLF_FAST_10Xpower_rms, '--', 'LineWidth', 1.5, 'Color', [0.9290, 0.6940, 0.1250]); 

set(gca,'FontSize',18);
hTitle = title('CLF noise budget, 4uW trans, 3.9kHz UGF (Jan 24, 2019)');
xlim([10,1e5])
%ylim([1e-4,1e2])
legend({'CLF control signal', 'CLF QMON (in-loop)', 'CLF QMON Darknoise', 'CLF QMON rms', 'CLF control signal rms'},'FontSize',12, 'Location', 'Northeast')
hold off