clear all
close all
W=logspace(0,4,1000)*2*pi;
sys_Mir=-0.66*1e-6*tf([1],[1/0.41^2/4/pi/pi,1/50/0.41/2/pi,1])*tf([1],[1/0.763^2/4/pi/pi,1/50/0.763/2/pi,1])*tf([1/0.665^2/4/pi/pi,1/80/0.665/2/pi,1],[1]);
sys_Mar=-1.55*1e-6*tf([1],[1/0.41^2/4/pi/pi,1/50/0.41/2/pi,1])*tf([1],[1/0.763^2/4/pi/pi,1/50/0.763/2/pi,1])*tf([1/0.95^2/4/pi/pi,1/20/0.95/2/pi,1],[1])*tf([1],[1/4/pi/pi,1/20/2/pi,1])*tf([1],[1/1.23^2/4/pi/pi,1/20/1.23/2/pi,1])*tf([1/1.21^2/4/pi/pi,1/20/1.21/2/pi,1],[1]);
H_Mir=freqresp(sys_Mir,W);
H_Mar=freqresp(sys_Mar,W);

bodemag(sys_Mir)
hold on
bodemag(sys_Mar,'r')
legend('Mirror z [m/V]','Marionette z [m/V]')

%Board Noise (Several board versions)
D_N_4=sqrt(80e-9^2+8e-7^2./W);  %p4
D_N_5=sqrt(110e-9^2+8e-7^2./W); %p5 
D_N_6=sqrt(4e-9^2+8e-7^2./W);   %p6
P41stOrd        
D_N_4_1=data(:,2)'; %p4 + 1st order shaping filter
P61stOrd
D_N_6_1=data(:,2)'; %p6 + 1st order shaping filter
P62ndOrd
D_N_6_2=data(:,2)'; %p6 + 2nd order shaping filter
f=data(:,1)';

figure
loglog(W/2/pi,D_N_4)
hold on
grid on
loglog(W/2/pi,D_N_5,'r')
loglog(f,D_N_4_1,'k')
loglog(W/2/pi,D_N_6,'g')
loglog(f,D_N_6_1,'m')
loglog(f,D_N_6_2,'y')
xlim([1 1e4])
xlabel('Frequency [Hz]')
ylabel('Noise [V/sqrt(Hz)]')
legend('p4','p5','p4 + 1st order shaping filter','p6','p6 + 1st order shaping filter','p6 + 2nd order shaping filter')
PlotEPS('DAC_Noise')

H_Mir2=freqresp(sys_Mir,f*2*pi);
H_Mar2=freqresp(sys_Mar,f*2*pi);

aa=load('12523_advsens_officialearly.txt');
bb=load('12523_3mm.txt');
cc=load('O1_sensitivity.txt'); %LIGO O1 sensitivities
dd=load('12580_srtuned25w.txt');
f1=aa(:,1);
f2=bb(:,1);
f3=cc(:,1);
f4=dd(:,1);
h=aa(:,2);
h_steel=bb(:,2);
h_O1=cc(:,2);
h_SR=dd(:,2);
R_CD=18.5+10;
R_CDM=6+4.5+10;
R_1=5000+R_CD;
D_N_5_R1=sqrt(D_N_5.^2+1.6e-19/40*4*R_1); %Resistor Johnson Noise
D_N_6_R1=sqrt(D_N_6.^2+1.6e-19/40*4*R_1);
D_N_6_2_R1=sqrt(D_N_6_2.^2+1.6e-19/40*4*R_1);
D_N_4_1_R1=sqrt(D_N_4_1.^2+1.6e-19/40*4*R_1);
R_2=50000+R_CD;
D_N_5_R2=sqrt(D_N_5.^2+1.6e-19/40*4*R_2);
D_N_6_R2=sqrt(D_N_6.^2+1.6e-19/40*4*R_2);
D_N_4_1_R2=sqrt(D_N_4_1.^2+1.6e-19/40*4*R_2);
D_N_6_2_R2=sqrt(D_N_6_2.^2+1.6e-19/40*4*R_2);
R_3=500+R_CD;
D_N_6_2_R3=sqrt(D_N_6_2.^2+1.6e-19/40*4*R_3);
R_4=300+R_CD;
D_N_4_R4=sqrt(D_N_4.^2+1.6e-19/40*4*R_4);
D_N_4_1_R4=sqrt(D_N_4_1.^2+1.6e-19/40*4*R_4);
alpha=3.4e-3/2;
M=42;
L=3000;
W0=2*pi*0.6;
R=logspace(0,5,1000);
D_N_5_100=ones(1,1000)*sqrt(110e-9^2+8e-7^2./2/pi/100);
D_N_6_100=ones(1,1000)*sqrt(4e-9^2+8e-7^2./2/pi/100);
figure
loglog(R,R_CD./R.*sqrt(D_N_5_100.^2+1.6e-19/40*4.*R))
hold on
grid on
loglog(R,R_CD./R.*sqrt(D_N_6_100.^2+1.6e-19/40*4.*R),'r')
xlabel('Resistance [Ohm]')
ylabel('Noise [V/sqrt(Hz)]')

figure
loglog(W/2/pi,D_N_5.*abs(squeeze(H_Mir))'/L*2,'r')
grid on
hold on
loglog(W/2/pi,D_N_5.*abs(squeeze(H_Mar))'/L*2,'r--')
% loglog(W/2/pi,D_N_5_R1/R_1*alpha/M./W.^2/L*4,'r')
loglog(W/2/pi,R_CD/R_1*D_N_5_R1.*abs(squeeze(H_Mir))'/L*2,'m')
% loglog(W/2/pi,R_CD/R_2*D_N_5_R2.*abs(squeeze(H_Mir))'/L*2,'g')
loglog(f,R_CD/R_3*D_N_6_2_R3.*abs(squeeze(H_Mir2))'/L*2,'g')
loglog(f,R_CD/R_1*D_N_6_2_R1.*abs(squeeze(H_Mir2))'/L*2,'c')
loglog(f,R_CD/R_2*D_N_6_2_R2.*abs(squeeze(H_Mir2))'/L*2,'y')
% loglog(W/2/pi,R_CD/R_4*D_N_4_R4.*abs(squeeze(H_Mir))'/L*2,'c')
% loglog(f,R_CD/R_4*D_N_4_1_R4.*abs(squeeze(H_Mir2))'/L*2,'y')
loglog(f2,h_steel)
loglog(f1,h,'k')
%loglog(f4,h_SR,'k')
% loglog(f3,h_O1)
set(gca,'YTick',10.^[-35:1:-10])
xlim([1 1e4])
xlabel('Frequency [Hz]')
ylabel('h [1/sqrt(Hz)]')
legend('Board Noise (p5, FP Mirrors)','Board Noise (p5, FP Marionette)',['Board Noise (p5 + ' num2str((R_1-R_CD)/1000) ' kOhm, FP Mirrors)'],['Board Noise (p6 + ' num2str((R_3-R_CD)/1000) ' kOhm + 2nd order shaping filter, FP Mirrors)'],['Board Noise (p6 + ' num2str((R_1-R_CD)/1000) ' kOhm + 2nd order shaping filter, FP Mirrors)'],['Board Noise (p6 + ' num2str((R_2-R_CD)/1000) ' kOhm + 2nd order shaping filter, FP Mirrors)'],'AdV Sensitivity (Steel Suspensions (0.3 mm, phi=1e-3), 25 W)','AdV Sensitivity (Silica Suspensions, SR, 25 W)')
PlotEPS('CD_Sens')

% figure
% loglog(W/2/pi,D_N_6.*abs(squeeze(H))'/L*2,'r')
% grid on
% hold on
% % loglog(W/2/pi,D_N/R_CD*alpha/M./W.^2/L*4,'r')
% loglog(W/2/pi,R_CD/R_1*D_N_3.*abs(squeeze(H))'/L*2,'m')
% loglog(W/2/pi,R_CD/R_2*D_N_4.*abs(squeeze(H))'/L*2,'g')
% loglog(f2,h_steel)
% % loglog(f2,h_steel2,'r')
% loglog(f1,h,'k')
% set(gca,'YTick',10.^[-30:1:-12])
% xlabel('Frequency [Hz]')
% ylabel('h [1/sqrt(Hz)]')
% legend('Board Noise (p5, FP Mirrors)',['Board Noise (p6 + ' num2str((R_1-R_CD)/1000) ' kOhm, FP Mirrors)'],['Board Noise (p6 + ' num2str((R_2-R_CD)/1000) ' kOhm, FP Mirrors)'],'AdV Sensitivity (Steel Suspensions (0.3 mm, phi=1e-3), 25 W)','AdV Sensitivity (Silica Suspensions, 25 W)')
% PlotEPS('CD_Sens_p6')

