%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% .m script of web demo "Insert title here" 
% Written for GNU Octave
% 
% INSTITUTE OF TELECOMMUNICATIONS  
% University of Stuttgart 
% www.inue-uni-stuttgart.de 
% author: Your Name   
% date: DD.MM.YYYY   
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% This example script just prints the values of the given parameters.
% Make sure that the function prototype matches the prototype defined
% in the XML config file. (Parameter names are arbitrary but should be the same
% for better readability)

function [reflection_r2] = FrequencyDomain_fun(R,G,L,C, reflection_r1, reflection_r2, Length, X, Fmax, display, diameter, skin_effect, H_instead_of_F, filename)
points_to_plot = 1000;

c0=299792458; %//exact
mu0 =  4*pi*1e-7; %// not exact... 
roh_copper= 1.7*10^-2; %// Ohm*mm²/m
C=C*1e-12;
L=L*1e-6;
R=R*1e-3;
G=G*1e-3;
if G==0
    G=1e-900;
end
warning_message_str="";
if and(H_instead_of_F ==1,reflection_r2~=0)
    warning_message_str='For {H(x,\omega)}: r_2 is set to 0 (no reflection)';
    reflection_r2=0;
end
% define formats
landscape = "-S930,350";
portrait  = "-S640,480";

% select format
output_format = portrait;

% Create an invisible figure.
fh = figure(1);
set(fh, "visible", "off");

% define style
set(0, "defaultlinelinewidth", 5);
set(0, "defaultaxesfontsize", 16);
set(0, "defaultaxesfontname", "Helvetica");
set(0, "defaulttextfontsize", 16);
set(0, "defaulttextfontname", "Helvetica");
if display==9
    f = Fmax*1e6;
else    
    f=linspace(0,Fmax*1e6,points_to_plot);
end
omega = 2*pi*f;

skin_effect
if skin_effect ==1
    delta_f = sqrt(2*roh_copper./(omega*mu0));
    delta_f(delta_f > (diameter/2))=diameter/2
    R=roh_copper./(pi*(diameter/2).^2-pi*(diameter/2-delta_f).^2)
end



%% Telegraphen equation
gamma = sqrt((R+1i*omega*L).*(G+1i*omega*C));


ZL = sqrt((R+1i*omega*L)./(G+1i*omega*C));


if reflection_r1 ==1
    Z1=10^90;
else
    Z1 = ZL*(1+reflection_r1)/(1-reflection_r1);
end

if reflection_r2 ==1
    Z2=10^90;
else
    Z2 = ZL*(1+reflection_r2)/(1-reflection_r2);
end
F_omega= (ZL./(Z1+ZL)).*(1./(1-reflection_r1.*reflection_r2.*exp(-2.*gamma.*Length))).*(exp(-1.*gamma.*X)+reflection_r2.*exp(-1.*gamma.*(2.*Length-X)));
F_omega_0 = (ZL./(Z1+ZL)).*(1./(1-reflection_r1.*reflection_r2.*exp(-2.*gamma.*Length))).*(exp(-1.*gamma.*0)+reflection_r2.*exp(-1.*gamma.*(2.*Length-0)));

if H_instead_of_F ==1
   % plot
    %SNR
    if display==1
    plot(f/1e6,20*log10(abs(F_omega./F_omega_0)));
    xlabel('f [MHz]')
    ylabel('|H(x,2\pi f)|^2 [dB]')
    end

    if or(display==7,display==8)
    X = linspace(0,Length,points_to_plot);
    F_omega=zeros(points_to_plot,points_to_plot);
    H_omega=zeros(points_to_plot,points_to_plot);
    for k =1:length(f)
        F_omega(k,:)= (ZL./(Z1+ZL)).*(1./(1-reflection_r1.*reflection_r2.*exp(-2.*gamma(k).*Length))).*(exp(-1.*gamma(k).*X)+reflection_r2.*exp(-1.*gamma(k).*(2.*Length-X)));
        H_omega(k,:)= F_omega(k,:)/F_omega(k,1);
    end
    F_omega(:,1)
    end
    if display==7
    surf(X,f/1e6,20*log10(abs(F_omega)));
    xlabel('x [m]')
    ylabel('f [MHz]')
    zlabel('|F(x,2\pi f)|^2 [dB]')
    shading interp
    view([45 45]);
    hcb=colorbar;
    title(hcb,'|F(x,2\pi f)|^2 [dB]');
    end
    if display==8
    Data=20*log10(abs(H_omega));
    f_axis =f/1e6; 
    imagesc(X,f_axis,(Data));
    set(gca,'ydir','normal');
    xlabel('x [m]')
    ylabel('f [MHz]')
    hcb=colorbar;
    title(hcb,'|H(x,2\pi f)|^2 [dB]');
    end
    if display==9
    X= linspace(0,Length,points_to_plot);
    F_omega= (ZL./(Z1+ZL)).*(1./(1-reflection_r1.*reflection_r2.*exp(-2.*gamma.*Length))).*(exp(-1.*gamma.*X)+reflection_r2.*exp(-1.*gamma.*(2.*Length-X)));
    plot(X,20*log10(abs(F_omega./F_omega(1))));
    title(sprintf('Considered Frequency: %.0f MHz',Fmax));
    xlabel('x [m]')
    ylabel('|H(x,2\pi f)|^2 [dB]')
    end 
else
    %% plot
    %SNR
    if display==1
    plot(f/1e6,20*log10(abs(F_omega)));
    xlabel('f [MHz]')
    ylabel('|F(x,2\pi f)|^2 [dB]')
    end
    if or(display==7,display==8)
    x = linspace(0,Length,points_to_plot);
    F_omega=zeros(points_to_plot,points_to_plot);
    
    for k =1:length(x)
        X=x(k);
        F_omega(:,k)= (ZL./(Z1+ZL)).*(1./(1-reflection_r1.*reflection_r2.*exp(-2.*gamma.*Length))).*(exp(-1.*gamma.*X)+reflection_r2.*exp(-1.*gamma.*(2.*Length-X)));
    end
    end
    if display==7
    surf(x,f/1e6,20*log10(abs(F_omega)));
    xlabel('x [m]')
    ylabel('f [MHz]')
    zlabel('|F(x,2\pi f)|^2 [dB]')
    shading interp
    view([45 45]);
    hcb=colorbar;
    title(hcb,'|F(x,2\pi f)|^2 [dB]');
    end
    if display==8
    Data=20*log10(abs(F_omega));
    f_axis =f/1e6; 
    imagesc(x,f_axis,(Data));
    set(gca,'ydir','normal');
    xlabel('x [m]')
    ylabel('f [MHz]')
    hcb=colorbar;
    title(hcb,'|F(x,2\pi f)|^2 [dB]');
    end
    if display==9
    X= linspace(0,Length,points_to_plot);
    F_omega= (ZL./(Z1+ZL)).*(1./(1-reflection_r1.*reflection_r2.*exp(-2.*gamma.*Length))).*(exp(-1.*gamma.*X)+reflection_r2.*exp(-1.*gamma.*(2.*Length-X)));
    plot(X,20*log10(abs(F_omega)));
    title(sprintf('Considered Frequency: %.0f MHz',Fmax));
    xlabel('x [m]')
    ylabel('|F(x,2\pi f)|^2 [dB]')
    end
end
if display==2
plot(f/1e6,ZL);
xlabel('f [MHz]')
ylabel('Z_L in Ohm')
end
if display==3
plot(f/1e6,omega./imag(gamma)/c0);
xlabel('f [MHz]')
ylabel('v_{ph}/c_0')
end
if display==4
plot(f/1e6,Z1);
xlabel('f [MHz]')
ylabel('Z_1 [Ohm]')
end
if display==5
plot(f/1e6,Z2);
xlabel('f [MHz]')
ylabel('Z_2 [Ohm]')
end
if display==6
semilogy(f/1e6,omega./imag(gamma)./f);
xlabel('f [MHz]')
ylabel('\lambda [m]')
end
grid on;
magenta = [[51,0,51]; [102,0,102]; [153,0,153]; [204,0,204]; [255,0,255]; [255,51,255]; [255,102,255]; [255,153,255]; [255,204,255]]/255;
  
text(sum(xlim)*0.5,sum(ylim)*0.5, warning_message_str, 'fontsize', 20,'color', magenta(6,:), 'horizontalalignment', 'center','interpreter', 'tex');



% Create an image file. This png image is displayed on the webpage. Its size 
% must be 640x480.
print(filename, "-dpng", output_format);


end
