clear all;

%Dioden Laser Kennlinie
%daten einlesen
[titel_d_k,d]=mhdrload('d_laser_kennlinie.dat');

Id=d(:,1);
A=d(:,2);
div=d(:,3);
gain=d(:,4);
d_A=0.1;

Pld = A.*div./gain /50;
d_Pld=d_A.*div./gain /50;
Ith =190;

%Fit
	lin=[5:11];

    [a,b,da,db,chi2] = linfit(Id(lin)-Ith,Pld(lin),d_Pld(lin))
    disp('diodenkennlinie, steigung, fehler, chi2: ');
    disp(b);
    disp(db);
    disp(chi2);
    
    a_diode=a;
    b_diode=b;
    Ith_diode=Ith;
    
	
f1=figure(1);clf;
hold on;
p1=errorbar(Id,Pld,d_Pld,'.r');
i=[0:max(Id)];
p2=plot(i,a+b.*(i-Ith),' b-');

ax1=gca;
set(ax1,'YLim',[0,max(Pld)*1.1]);
set(ax1,'XLim',[0,max(Id)*1.1]);
grid on;

le1=legend([p1(1),p2],'I_{PD}',['linearer Fit I_{LD}=a+b \cdot I_{PD} b=',num2str(b),'\pm ',num2str(db)],2);
xl1=xlabel('Strom der Laserdiode I_{LD} / mA');
yl1=ylabel('Strom der Photodiode I_{PD} / mA');

hold off;

print -deps2 -tiff -r300 d_laser_kennlinie

t=[titel_d_k,'& $I_{PD} /[mA]$ & $\Delta I_{PD}/[mA]$\\'];
d=latex([d,Pld,d_Pld],'%#.3g','nomath');
f = fopen('d_laser_kennlinie.tab','w');
fwrite(f,d);
fclose(f);
f = fopen('d_laser_kennlinie_h.tab','w');
fwrite(f,t);
fclose(f);



%Absorptionsspektrum
lit=load('YAG_ABS1.TXT');
lambda_lit = lit(:,1);
abs_lit = lit(:,3);





[titel_abs,d]=mhdrload('absorptionsspektrum.dat');
Id=440;
T=d(:,1);
A=d(:,2);
div=d(:,3);
gain=d(:,4);
d_A=0.1;


Pld = A.*div./gain/50;Id
d_Pld=d_A.*div./gain/50;
figure(5);clf;
hold on;
p1 = errorbar(T,Pld,d_Pld,'.r');
xi_trans=[min(T):0.1:max(T)];
yi_trans=interp1(T,Pld,xi_trans,'spline');
xi=xi_trans;
yi=yi_trans;
p2=plot(xi_trans,yi_trans,' -');
hold off;

ax1=gca;
grid on;
set(ax1,'YLim',[0,max(Pld)*1.1])
set(ax1,'XLim',[min(T)*0.9,max(T)*1.1])
le1=legend(p1(1),'I_{LD}',2);
xl1=xlabel('Temperatur der Laserdiode T / °C');
yl1=ylabel('Strom der Photodiode I_{PD} / mA');

print -deps2 -tiff -r300 absorptionsspektrum


figure(3);clf;
hold on;
%p1 = plot(lambda_lit,abs_lit,'.r');
xi2=[min(lambda_lit):0.1:max(lambda_lit)];
yi2=interp1(lambda_lit,abs_lit,xi2,'spline');
p2=plot(xi2,yi2,'-');
p3 = errorbar(T*0.31+801.3,Pld*0.222,d_Pld,'.r');
p4=plot(xi*0.31+801.3,yi*0.222,'k');

grid on;
hold off;

ax1=gca;
grid on;
le1=legend([p2,p3(1)],'Literaturwerte','Messwerte',2);
xl1=xlabel('Wellenlänge \lambda / nm');
yl1=ylabel('Transmission');

print -deps2 -tiff -r300 absorptionsspektrum_vergleich


t=[titel_abs,'& $I_{PD} /[mA]$ & $\Delta I_{PD}/[mA]$\\'];
d=latex([d,Pld,d_Pld],'%#.3g','nomath');
f = fopen('absorptionsspektrum.tab','w');
fwrite(f,d);
fclose(f)
f = fopen('absorptionsspektrum_h.tab','w');
fwrite(f,t);
fclose(f)






%Messung bei Konstantem Lambda des diodenlasers: Temperaturabhängigkeit
[titel_konst_l,d]=mhdrload('konstantes_lambda.dat');
Id=d(:,1);
T=d(:,2);
A=d(:,3);
div=d(:,4);
gain=d(:,5);
d_A=0.1;
d_T=0.5;
d_Id =2;



Pld = A.*div./gain/50;
d_Pld=d_A.*div./gain/50;
figure(7);clf;
hold on;
errorbar(Id,T,d_T*ones(size(T)),'*r');

	%Kurvenfit k(1)+U*k(2) a+b*Id
% 	X=[ones(size(Id)),Id];
% 	k=X\T;
% 	a=k(1);
% 	b=k(2);
% 	
% 	%Fehler von k(2)
%     s_T = 1./d_T.^2;
%     alpha11=sum(s_T)*numel(A);
% 	alpha12=sum(s_T.*(Id));
% 	alpha22=sum(s_T.*((Id).^2));
% 	D=alpha11*alpha22-alpha12^2;
% 	d_b=sqrt(alpha11/D);
% 	d_a=sqrt(alpha22/D);

    [a,b,da,db,chi2] = linfit(Id,T,d_T);
    
    
    
	disp('kalibration: a + b * Id');	
	a
	da
	b
	db
    chi2

p1=plot(Id,a+Id.*b,' r-');
hold off;	

grid on;

ax1=gca;
set(ax1,'YLim',[min(T)*0.9,max(T)*1.1]);
set(ax1,'XLim',[min(Id)*0.9,max(Id)*1.1]);
le1=legend(p1,'T_{LD} ... mit Linearfit',1);
yl1=ylabel('Temperatur der Laserdiode T / °C');
xl1=xlabel('Strom der Photodiode I_{PD} / mA');

print -deps2 -tiff -r300 konstantes_lambda

t=[titel_konst_l,'\\'];
d=latex([d],'%#.3g','nomath');
f = fopen('konstantes_lambda.tab','w');
fwrite(f,d);
fclose(f);
f = fopen('konstantes_lambda_h.tab','w');
fwrite(f,t);
fclose(f);





%Messung der Spontanen Emission
[titel_spontan,d]=mhdrload('spontane_emission.dat');
t=d(:,1)*100;
d(:,1)=d(:,1)*100;

A=d(:,2);
div=d(:,3);
gain=d(:,4);
d_A=0.1;

Pld = A.*div./gain/50;
d_Pld=d_A.*div./gain/50;
figure(9);clf;
hold on;
errorbar(t,Pld,d_Pld,'.r');
	%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
	% Kurvenfit
	%
	
	eps = 0.000001;     %max relativer fehler
	
	ndata = numel(t);	
	ma = 2;
	a = [0.004, 0.003];
	
	[a,sd_a,var,covar,c] = mrqmin(t,Pld,d_Pld,ndata,ma,a,eps);
	% Eingabe-Parameter:  x  Zeilenvektor der x-Komponenten der geg. Punkte
	%                     y  Zeilenvektor der y-Komponenten der geg. Punkte
	%                     sigig  Zeilenvektor der Standardabweichungen der geg. Punkte
	%                     ndata  Zahl der geg. Punkte
	%                     ma     Zahl der Fit-Parameter
	%                     a      Zeilenvektor der guessed values fuer die Fit-Paramter
	%                     eps    relativer Fehler der Fit-Parameter, der zu unterschreiten ist
	%
	% Ausgabe-Parameter:  a      Zeilenvektor der gefitteten Parameter
	%                     sd_a   Zeilenvektor der Standardabweichungen der Parameter
	%                     var    Varianz des Fits   Anfang und Ende des Sollbereichs
	%                     covar  Kovarianz-Matrix (normiert gemaess Skriptum, S. 109)
	ti = [0:0.1:max(t)*1.1];
	p2=plot(ti,a(1) * exp(-a(2)*ti),' r-');
	
    chi2=var(1)*(numel(t)-ma);
	
	F=1/a(2);
    df = 1/a(2)^2*sd_a(2);
    
	disp('y=a(1) * exp(-a(2)*t)');
	disp(['a1= ',num2str(a(1)), ' +- ',num2str(sd_a(1))]);
	disp(['a2= ',num2str(a(2)), ' +- ',num2str(sd_a(2))]);
	disp(['F= ',num2str(F), ' +- ',num2str(df)]);
	disp(['chi^2= ',num2str(chi2)]);
	
	
	hold off;
	%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
ax1=gca;
set(ax1,'YLim',[0,max(Pld)*1.1])
set(ax1,'XLim',[0,max(t)*1.1])
legend(p2,'I_{PD} ... mit fit: a \cdot e^{- \Gamma \cdot t}',1);
xlabel('Zeit t/µs');
ylabel('Strom der Photodiode I_{PD} / mA');
grid on;

print -deps2 -tiff -r300 spontane_emission

t=[titel_spontan,'& $I_{PD} /[mA]$ & $\Delta I_{PD}/[mA]$\\'];
d=latex([d,Pld,d_Pld],'%#.3g','nomath');
f = fopen('spontane_emission.tab','w');
fwrite(f,d);
fclose(f);
f = fopen('spontane_emission_h.tab','w');
fwrite(f,t);
fclose(f);





%Messung Nd YAG Laserkennlinie
[titel_kennlinie,d]=mhdrload('laserkennlinie.dat');
Id=d(:,1);
T=d(:,2);
A=d(:,3);
div=d(:,4);
gain=d(:,5);
d_A=0.1;
Ith=260;

Pl = A.*div./gain/50;
d_Pl=d_A.*div./gain/50;

%Fit        gegen I_inj
	lin=[5:numel(A)];
    [a,b,da,db,chi2] = linfit(Id(lin)-Ith,Pl(lin),d_Pl(lin))
    disp('ndyag laserkennlinie, steigung:');
    disp(b);
    disp(db);
    disp(chi2);
	
	

f1=figure(11);clf;
hold on;
p1=errorbar(Id,Pl,d_Pl,'.r');
i=[0:max(Id)];
p2=plot(i,a+b.*(i-Ith),' b-');

ax1=gca;
set(ax1,'YLim',[0,max(Pl)*1.1])
set(ax1,'XLim',[0,max(Id)*1.1])

le1=legend([p1(1),p2],'I_{PD}',['linearer Fit I_{PD}=a+b \cdot I_{LD} b=',num2str(b)],2);
xl1=xlabel('Strom der Laserdiode I_{LD} / mA');
yl1=ylabel('Strom der Photodiode I_{PD} / mA');
grid on;

hold off;

print -deps2 -tiff -r300 laser_kennlinie


%Fit        gegen P_ld
P_ld_th=2.6;
P_ld=a_diode+b_diode.*(Id-Ith_diode);
P_ld(P_ld<0)=0;

 lin=[5:numel(A)];
     [a,b,da,db,chi2] = linfit(P_ld(lin)-P_ld_th,Pl(lin),d_Pl(lin))
     disp('ndyag laserkennlinie vs. Pld, steigung:');
     disp(b);
     disp(db);
     disp(chi2);
	
	

f1=figure(11);clf;
hold on;
p1=errorbar(P_ld,Pl,d_Pl,'.r');
i=[0:max(P_ld)];
p2=plot(P_ld,a+b.*(P_ld-P_ld_th),' b-');

ax1=gca;
set(ax1,'YLim',[0,max(Pl)*1.1])
set(ax1,'XLim',[0,max(P_ld)*1.1])

le1=legend([p1(1),p2],'I_{PD}',['linearer Fit I_{PD}=a+b \cdot I_{PD-Diodenlaser} b=',num2str(b)],2);
xl1=xlabel('Strom der Laserdiode I_{PD-Diodenlaser} / mA');
yl1=ylabel('Strom der Photodiode I_{PD} / mA');
grid on;

hold off;

print -deps2 -tiff -r300 laser_kennlinie2


t=[titel_kennlinie,'& $I_{PD-Diodenlaser} / [mA]$ & $I_{PD} /[mA]$ & $\Delta I_{PD}/[mA]$\\'];
d=latex([d,P_ld,Pl,d_Pl],'%#.3g','nomath');
f = fopen('laser_kennlinie.tab','w');
fwrite(f,d);
fclose(f);
f = fopen('laser_kennlinie_h.tab','w');
fwrite(f,t);
fclose(f);









%Messung Nd YAG bei verschiedenen Pumpfrequenzen
[titel_scan,d]=mhdrload('frequenzscan.dat');
T=d(:,1);
A=d(:,2);
div=d(:,3);
gain=d(:,4);
d_A=0.1;

Pld = A.*div./gain/50;
d_Pld=d_A.*div./gain/50;



figure(13);clf;
hold on;
p1 = errorbar(T,Pld,d_Pld,'.r');
xi=[min(T):0.1:max(T)];
yi=interp1(T,Pld,xi,'spline');
p2=plot(xi,yi,' -');
p3=plot(xi_trans,yi_trans/4,'-.');
hold off;

ax1=gca;
set(ax1,'YLim',[0,max(Pld)*1.1])
set(ax1,'XLim',[min(T)*0.9,max(T)*1.1])
le1=legend([p1(1),p3],'I_{LD}','1/4 * I_{LD-Transmissionsspektrum}',1);
xl1=xlabel('Temperatur der Laserdiode T / °C');
yl1=ylabel('Strom der Photodiode I_{PD} / mA');
grid on;

print -deps2 -tiff -r300 frequenzscan

t=[titel_scan,'& $I_{PD} /[mA]$ & $\Delta I_{PD}/[mA]$\\'];
d=latex([d,Pld,d_Pld],'%#.3g','nomath');
f = fopen('frequenzscan.tab','w');
fwrite(f,d);
fclose(f);
f = fopen('frequenzscan_h.tab','w');
fwrite(f,t);
fclose(f);

















%Messung Nd YAG mit Frequenzverdoppler Kristal
[titel_doppel1,d]=mhdrload('doppel1.dat');
Id=d(:,1);
T=d(:,2);
A_yag=d(:,3);
div_yag=d(:,4);
gain_yag=d(:,5);
A_kts=d(:,6);
div_kts=d(:,7);
gain_kts=d(:,8);
d_A=0.1;

Pld_yag = A_yag.*div_yag./gain_yag/50;
d_Pld_yag=d_A.*div_yag./gain_yag/50;

Pld_kts = A_kts.*div_kts./gain_kts/50;
d_Pld_kts=d_A.*div_kts./gain_kts/50;

figure(15);clf;
hold on;
p1=errorbar(Pld_yag,Pld_kts,d_Pld_kts,'.r');




%Fit
	lin=[1:numel(T)];
	%Kurvenfit Pld = a + b * Id
	X=[Pld_yag(lin).^2];
	k=X\Pld_kts(lin);
	a=k(1);
	%Fehler
	%alpha11=sum(d_Pld(lin));
	%alpha12=sum(d_Pld(lin).*Id(lin));
	%alpha22=sum(d_Pld(lin).*(Id(lin).^2));
	%D=alpha11*alpha22-alpha12^2;
	%sigma_b=alpha11/D;
	%sigma_a=alpha22/D;
	

i=[0:0.001:max(Pld_yag)];
p2=plot(i,a.*i.^2,' b-');

ax1=gca;
set(ax1,'YLim',[0,max(Pld_kts)*1.1])
set(ax1,'XLim',[0,max(Pld_yag)*1.1])

le1=legend([p1(1),p2],'I_{PD}',['quadratischer Fit I_{PD}=a \cdot I^2_{LD} a=',num2str(a)],2);
ylabel('Strom der Laserdiode mit Filter für grüne Frequenz I_{PD}  / mA');
xlabel('Strom der Photodiode mit Filter für IR Frequenz I_{PD} / mA');
grid on;

hold off;

print -deps2 -tiff -r300 doppel1

t=[titel_doppel1,'& $I_{YAG} /[mA]$ & $\Delta I_{YAG}/[mA]$ & $I_{KTS} /[mA]$ & $\Delta I_{KTS}/[mA]$\\'];
d=latex([d,Pld_yag,d_Pld_yag,Pld_kts,d_Pld_kts],'%#.3g','nomath');
f = fopen('doppel1.tab','w');
fwrite(f,d);
fclose(f);
f = fopen('doppel1_h.tab','w');
fwrite(f,t);
fclose(f);










%Messung Nd YAG mit Frequenzverdoppler Kristal
[titel_doppel2,d]=mhdrload('doppel2.dat');
Id=d(:,1);
T=d(:,2);
A_yag=d(:,3);
div_yag=d(:,4);
gain_yag=d(:,5);
A_kts=d(:,6);
div_kts=d(:,7);
gain_kts=d(:,8);
d_A=0.1;


Pld_kts = A_kts.*div_kts./gain_kts/50;
d_Pld_kts=d_A.*div_kts./gain_kts/50;

Pld_yag = A_yag.*div_yag./gain_yag/50;
d_Pld_yag=d_A.*div_yag./gain_yag/50;

%Pld_yag = sqrt(Pld_yag.^2 - Pld_kts.^2); 


figure(17);clf;
hold on;
p1=errorbar(Pld_yag,Pld_kts,d_Pld_kts,'.r');




%Fit
	lin=[1:numel(T)];
	%Kurvenfit Pld = a + b * Id
	X=[Pld_yag(lin).^2];
	k=X\Pld_kts(lin);
	a=k(1);
	%Fehler
	%alpha11=sum(d_Pld(lin));
	%alpha12=sum(d_Pld(lin).*Id(lin));
	%alpha22=sum(d_Pld(lin).*(Id(lin).^2));
	%D=alpha11*alpha22-alpha12^2;
	%sigma_b=alpha11/D;
	%sigma_a=alpha22/D;
	

i=[0:0.001:max(Pld_yag)];
p2=plot(i,a.*i.^2,' b-');

ax1=gca;
set(ax1,'YLim',[0,max(Pld_kts)*1.1])
set(ax1,'XLim',[0,max(Pld_yag)*1.1])

le1=legend([p1(1),p2],'I_{PD}',['quadratischer Fit I_{PD}=a \cdot I^2_{LD} a=',num2str(a)],2);
ylabel('Strom der Laserdiode mit Filter für grüne Frequenz I_{PD}  / mA');
xlabel('Strom der Photodiode mit Filter für IR Frequenz I_{PD} / mA');
grid on;

hold off;

print -deps2 -tiff -r300 doppel2

t=[titel_doppel1,'& $I_{YAG} /[mA]$ & $\Delta I_{YAG}/[mA]$ & $I_{KTS} /[mA]$ & $\Delta I_{KTS}/[mA]$\\'];
d=latex([d,Pld_yag,d_Pld_yag,Pld_kts,d_Pld_kts],'%#.3g','nomath');
f = fopen('doppel2.tab','w');
fwrite(f,d);
fclose(f);
f = fopen('doppel2_h.tab','w');
fwrite(f,t);
fclose(f);
