%% Hochladen der Dateien

clear all
clc
close all

q = 1.60217663e-19; % C
k_B = 1.380649e-23; % J/K

opts = delimitedTextImportOptions("NumVariables", 2);
opts.DataLines = [2, Inf];
opts.Delimiter = ";";
opts.VariableNames = ["VoltageV", "CurrentA"];
opts.VariableTypes = ["double", "double"];
opts.ExtraColumnsRule = "ignore";
opts.EmptyLineRule = "read";

% Import the data
Temp10 = readtable("C:\Users\mfoet\OneDrive - Universität Graz\Desktop\Uni\Semiconductor Lab\Cleaned up\Schottky_IV_rangesens_daylight_10degrees_2024_04_23_090913_cleanup.csv", opts);
Temp12 = readtable("C:\Users\mfoet\OneDrive - Universität Graz\Desktop\Uni\Semiconductor Lab\Cleaned up\Schottky_IV_rangesens_daylight_10degrees_2024_04_23_090913_cleanup.csv", opts);
Temp14 = readtable("C:\Users\mfoet\OneDrive - Universität Graz\Desktop\Uni\Semiconductor Lab\Cleaned up\Schottky_IV_rangesens_daylight_14degrees_2024_04_23_090913_cleanup.csv", opts);
Temp16 = readtable("C:\Users\mfoet\OneDrive - Universität Graz\Desktop\Uni\Semiconductor Lab\Cleaned up\Schottky_IV_rangesens_daylight_16degrees_2024_04_23_090913_cleanup.csv", opts);
Temp18 = readtable("C:\Users\mfoet\OneDrive - Universität Graz\Desktop\Uni\Semiconductor Lab\Cleaned up\Schottky_IV_rangesens_daylight_18degrees_2024_04_23_090913_cleanup.csv", opts);
Temp20 = readtable("C:\Users\mfoet\OneDrive - Universität Graz\Desktop\Uni\Semiconductor Lab\Cleaned up\Schottky_IV_rangesens_daylight_20degrees_2024_04_23_090913_cleanup.csv", opts);
Temp22 = readtable("C:\Users\mfoet\OneDrive - Universität Graz\Desktop\Uni\Semiconductor Lab\Cleaned up\Schottky_IV_rangesens_daylight_22degrees_2024_04_23_090913_cleanup.csv", opts);
Temp24 = readtable("C:\Users\mfoet\OneDrive - Universität Graz\Desktop\Uni\Semiconductor Lab\Cleaned up\Schottky_IV_rangesens_daylight_24degrees_2024_04_23_090913_cleanup.csv", opts);
Temp26 = readtable("C:\Users\mfoet\OneDrive - Universität Graz\Desktop\Uni\Semiconductor Lab\Cleaned up\Schottky_IV_rangesens_daylight_26degrees_2024_04_23_090913_cleanup.csv", opts);
Temp28 = readtable("C:\Users\mfoet\OneDrive - Universität Graz\Desktop\Uni\Semiconductor Lab\Cleaned up\Schottky_IV_rangesens_daylight_28degrees_2024_04_23_090913_cleanup.csv", opts);
Temp30 = readtable("C:\Users\mfoet\OneDrive - Universität Graz\Desktop\Uni\Semiconductor Lab\Cleaned up\Schottky_IV_rangesens_daylight_30degrees_2024_04_23_090913_cleanup.csv", opts);
Temp32 = readtable("C:\Users\mfoet\OneDrive - Universität Graz\Desktop\Uni\Semiconductor Lab\Cleaned up\Schottky_IV_rangesens_daylight_32degrees_2024_04_23_090913_cleanup.csv", opts);
Temp34 = readtable("C:\Users\mfoet\OneDrive - Universität Graz\Desktop\Uni\Semiconductor Lab\Cleaned up\Schottky_IV_rangesens_daylight_34degrees_2024_04_23_090913_cleanup.csv", opts);
Temp36 = readtable("C:\Users\mfoet\OneDrive - Universität Graz\Desktop\Uni\Semiconductor Lab\Cleaned up\Schottky_IV_rangesens_daylight_36degrees_2024_04_23_110212_cleanup.csv", opts);
Temp38 = readtable("C:\Users\mfoet\OneDrive - Universität Graz\Desktop\Uni\Semiconductor Lab\Cleaned up\Schottky_IV_rangesens_daylight_38degrees_2024_04_23_110212_cleanup.csv", opts);
Temp40 = readtable("C:\Users\mfoet\OneDrive - Universität Graz\Desktop\Uni\Semiconductor Lab\Cleaned up\Schottky_IV_rangesens_daylight_40degrees_2024_04_23_110212_cleanup.csv", opts);
Temp42 = readtable("C:\Users\mfoet\OneDrive - Universität Graz\Desktop\Uni\Semiconductor Lab\Cleaned up\Schottky_IV_rangesens_daylight_42degrees_2024_04_23_110212_cleanup.csv", opts);
Temp44 = readtable("C:\Users\mfoet\OneDrive - Universität Graz\Desktop\Uni\Semiconductor Lab\Cleaned up\Schottky_IV_rangesens_daylight_44degrees_2024_04_23_110212_cleanup.csv", opts);
Temp46 = readtable("C:\Users\mfoet\OneDrive - Universität Graz\Desktop\Uni\Semiconductor Lab\Cleaned up\Schottky_IV_rangesens_daylight_46degrees_2024_04_23_110212_cleanup.csv", opts);
Temp48 = readtable("C:\Users\mfoet\OneDrive - Universität Graz\Desktop\Uni\Semiconductor Lab\Cleaned up\Schottky_IV_rangesens_daylight_48degrees_2024_04_23_110212_cleanup.csv", opts);
Temp50 = readtable("C:\Users\mfoet\OneDrive - Universität Graz\Desktop\Uni\Semiconductor Lab\Cleaned up\Schottky_IV_rangesens_daylight_50degrees_2024_04_23_110212_cleanup.csv", opts);



% Convert to output type
Temp10 = table2array(Temp10);
Temp12 = table2array(Temp12);
Temp14 = table2array(Temp14);
Temp16 = table2array(Temp16);
Temp18 = table2array(Temp18);
Temp20 = table2array(Temp20);
Temp22 = table2array(Temp22);
Temp24 = table2array(Temp24);
Temp26 = table2array(Temp26);
Temp28 = table2array(Temp28);
Temp30 = table2array(Temp30);
Temp32 = table2array(Temp32);
Temp34 = table2array(Temp34);
Temp36 = table2array(Temp36);
Temp38 = table2array(Temp38);
Temp40 = table2array(Temp40);
Temp42 = table2array(Temp42);
Temp44 = table2array(Temp44);
Temp46 = table2array(Temp46);
Temp48 = table2array(Temp48);
Temp50 = table2array(Temp50);

clear opts

filename = 'C:\Users\mfoet\OneDrive - Universität Graz\Desktop\Uni\Semiconductor Lab\t_v1_v2_shottky.txt';
formatSpec = '%18f%18f%18f%[^\n\r]';
fileID = fopen(filename,'r');
dataArray = textscan(fileID, formatSpec, 'Delimiter', '', 'WhiteSpace', '', 'TextType', 'string',  'ReturnOnError', false);
fclose(fileID);
V_limits = [dataArray{1:end-1}];
clearvars filename formatSpec fileID dataArray ans;

%% Sortieren:

x=Temp10(:,1);
y=Temp10(:,2);
[x10, sortIndex] = sort(x);
y10 = y(sortIndex);

x=Temp12(:,1);
y=Temp12(:,2);
[x12, sortIndex] = sort(x);
y12 = y(sortIndex);

x=Temp14(:,1);
y=Temp14(:,2);
[x14, sortIndex] = sort(x);
y14 = y(sortIndex);

x=Temp16(:,1);
y=Temp16(:,2);
[x16, sortIndex] = sort(x);
y16 = y(sortIndex);

x=Temp18(:,1);
y=Temp18(:,2);
[x18, sortIndex] = sort(x);
y18 = y(sortIndex);

x=Temp20(:,1);
y=Temp20(:,2);
[x20, sortIndex] = sort(x);
y20 = y(sortIndex);

x=Temp22(:,1);
y=Temp22(:,2);
[x22, sortIndex] = sort(x);
y22 = y(sortIndex);

x=Temp24(:,1);
y=Temp24(:,2);
[x24, sortIndex] = sort(x);
y24 = y(sortIndex);

x=Temp26(:,1);
y=Temp26(:,2);
[x26, sortIndex] = sort(x);
y26 = y(sortIndex);

x=Temp28(:,1);
y=Temp28(:,2);
[x28, sortIndex] = sort(x);
y28 = y(sortIndex);

x=Temp30(:,1);
y=Temp30(:,2);
[x30, sortIndex] = sort(x);
y30 = y(sortIndex);

x=Temp32(:,1);
y=Temp32(:,2);
[x32, sortIndex] = sort(x);
y32 = y(sortIndex);

x=Temp34(:,1);
y=Temp34(:,2);
[x34, sortIndex] = sort(x);
y34 = y(sortIndex);

x=Temp36(:,1);
y=Temp36(:,2);
[x36, sortIndex] = sort(x);
y36 = y(sortIndex);

x=Temp38(:,1);
y=Temp38(:,2);
[x38, sortIndex] = sort(x);
y38 = y(sortIndex);

x=Temp40(:,1);
y=Temp40(:,2);
[x40, sortIndex] = sort(x);
y40 = y(sortIndex);

x=Temp42(:,1);
y=Temp42(:,2);
[x42, sortIndex] = sort(x);
y42 = y(sortIndex);

x=Temp44(:,1);
y=Temp44(:,2);
[x44, sortIndex] = sort(x);
y44 = y(sortIndex);

x=Temp46(:,1);
y=Temp46(:,2);
[x46, sortIndex] = sort(x);
y46 = y(sortIndex);

x=Temp48(:,1);
y=Temp48(:,2);
[x48, sortIndex] = sort(x);
y48 = y(sortIndex);

x=Temp50(:,1);
y=Temp50(:,2);
[x50, sortIndex] = sort(x);
y50 = y(sortIndex);

y = {y10,y12,y14,y16,y18,y20,y22,y24,y26,y28,y30,y32,y34,y36,y38,y40,y42,y44,y46,y48,y50};
x = {x10,x12,x14,x16,x18,x20,x22,x24,x26,x28,x30,x32,x34,x36,x38,x40,x42,x44,x46,x48,x50};

temp = [10:2:50];
% transform cells to numeric matrix
rows = cellfun(@numel,x);
data = nan(max(rows),numel(temp));
for m = 1:numel(temp)
    data(1:rows(m),m) = x{m};
end

x = data; % = U


rows = cellfun(@numel,y);
data = nan(max(rows),numel(temp));
for m = 1:numel(temp)
    data(1:rows(m),m) = y{m};
end

y = data; % = I

%% Plot all data
figure
plot(x,y)
legend(string([10:2:50])+ "°C", 'Location', 'Northwest')
title('Schottky Diode')
xlabel('U / V')
ylabel('I / A')
xlim([0,0.4])

%% Fit for ideality factor and saturation current

% Initialize variables
temp = [10:2:50];
d = nan(1,numel(temp));
k = d;
dd = d;
dk = d;
R_square = d;


for m = 1:numel(temp)
    yf = y(:,m);
    xf = x(:,m);

    % fit over log(I)
    f=log(yf);

    % select right range for fit
    index = find(xf>V_limits(m,2) &  xf < V_limits(m,3));
    yf=f(index);
    xf=xf(index);

    % Linear fit: log(I) = d + k * U
    [c,gof] = fit(xf,yf,'poly1');
    d(m)=c.p2;
    k(m)=c.p1;
    R_square(m) = gof.rsquare;


    % get uncertainties from confidence_interval
    confidence_interval = confint(c);
    dk(m) = (confidence_interval(2,1) - confidence_interval(1,1)) / 2;
    dd(m) = (confidence_interval(2,2) - confidence_interval(1,2)) / 2;
end




%% Plots

T_plot = 20; % number of the element in temperature - array
T = temp(T_plot);

% select data for the right temperature
yf = y(:,T_plot);
xf = x(:,T_plot);

% fit over log(I)
f=log(abs(yf));

% select right range for fit
index = find(xf>=V_limits(T_plot,2) &  xf <= V_limits(T_plot,3));
yf=f(index);
xf=xf(index);


% plot
figure
hold on
plot(x(:,T_plot),log(abs(y(:,T_plot))),'b')

x_plot = linspace(min(xf),max(xf),100);
plot(x_plot, d(T_plot) + x_plot *k(T_plot),'r')

title(['Fit   log(I) = d + k * U     for    T = ', num2str(T), ' °C'])
subtitle(['R^2 = ', num2str(R_square(T_plot))])
xlabel('V')
ylabel('log(I)')
legend('Data','Fit','Location','Northwest')
hold off




%% Calculate Parameters

T = [10:2:50];
n = (q ./ (k_B .* (T +273.15) .* k));
dn =  q ./ (k_B .* (T +273.15) .* k.^2) .*dk; % using Größtunsicherheitsmethode
I_s = exp(d);
d_I_s = exp(d) .* dd;

subplot(1,2,1);
errorbar(T,n,dn,'x')
xlabel('T / °C')
ylabel('n / 1')
title('Ideality Factor')

subplot(1,2,2);
errorbar(T,I_s, d_I_s,'x')
ylabel('I_{s} / A')
title('Saturation Current')
xlabel('T / °C')

%% compare saturation current to measured data
T_plot = 2; % number of the element in temperature - array
T = temp(T_plot);

% select data for the right temperature
yf = y(:,T_plot);
xf = x(:,T_plot);

% plot
figure
hold on
plot(x(:,T_plot),y(:,T_plot),'b')
plot(x(:,T_plot), - I_s(T_plot) * ones(size(x(:,T_plot))),'r') % Attention: -I_s !

title(['Comparison I_s to Data for T = ', num2str(T), ' °C'])
xlabel('V')
ylabel('log(I)')
xlim([-1.5,0])
%ylim([-2e-8, 0])
legend('Data','I_s','Location','Northwest')
hold off


%% output
values = [temp;k;dk;n;dn;d;dd;I_s;d_I_s]



%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%


%% Energy Bandgap - 1. Fit
% for I over U


% Initialize variables
temp = [10:2:50];
d = nan(1,numel(temp));
k = d;

for m = 1:numel(temp)

    % Select right data for current temperature
    yf = y(:,m);
    xf = x(:,m);

    % Select range, which should be used for fit
    T = temp(m);
    T_k=273.15+T;
    f=log(yf/(T_k^2));
    index = find(xf>=V_limits(T_plot,2) &  xf <= V_limits(T_plot,3));
    yf=f(index);
    xf=xf(index);

    % Linear fit for log(I) = d + k * U
    c = polyfit(xf,yf,1);

    % Save fitparameter
    d(m)=c(2);
    k(m)=c(1);
end


%%

T_plot = 20; % number of the element in temp-array
T = temp(T_plot);

% select right measurement for selected temperature
yf = y(:,T_plot);
xf = x(:,T_plot);

% Fit over log(I/T^2)
f=log(yf/(T_k^2));

% Select range, which was used for fit
index = find(xf>=V_limits(T_plot,2) &  xf <= V_limits(T_plot,3));
yf=f(index);
xf=xf(index);

% Plot
figure
hold on
plot(x(:,T_plot),log(abs(y(:,T_plot))/(273.15+T)^2),'b')
x_plot = linspace(min(xf),max(xf),100);
plot(x_plot, d(T_plot) + x_plot * k(T_plot),'r')
title(['Fit    log(I/T^2)= d + k * V    for    T = ', num2str(T), ' °C'])
xlabel('V')
ylabel('log(I/T^2)')
legend('Data', 'Fit','Location','Northwest')
hold off

%% Energy band gap - 2. Fit
% for intercept d over 1/T


% Linear fit for y = d + k * 1/(k_B * T)
xfit = 1./ (k_B*(temp + 273.15)); % T in Kelvin!
[parameters2,gof] = fit(xfit.',d.','poly1');
y_0 = parameters2.p2;
m = parameters2.p1;
R_square_E_g = gof.rsquare;


% get uncertainties from confidence_interval (95% confidence)
confidence_interval = confint(parameters2);
dm = (confidence_interval(2,1) - confidence_interval(1,1)) / 2;
dy_0 = (confidence_interval(2,2) - confidence_interval(1,2)) / 2;


% Calculate E_g
E_g = - m / q ;
dE_g = dm / q;

% Plot
x_plot = linspace(min(xfit),max(xfit),100);

figure
hold on
plot(xfit,d,'bx')
plot(x_plot, x_plot*m + y_0,'r')

title('Fit:   d = y_0 + m * 1/(k_B*T)')
xlabel('1/(k_B*T)')
ylabel('d')
subtitle(['E_g = ',num2str(E_g), ' \pm ', num2str(dE_g),' eV,  R^4 = ', num2str(R_square_E_g)])
legend('Data','Fit','Location','Northeast')
hold off