%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% .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 [] = Attenuation_G9701_fun(checkcable1, checkcable2, checkcable3, checkcable4, checkcable5, freq1, freq2, distance_start, distance_stop, filename)


% 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");

Cables = [];
Legend = {};
if checkcable1 == 1
    Cables = [Cables, 1];
end
if checkcable2 == 1
    Cables = [Cables, 2];
end
if checkcable3 == 1
    Cables = [Cables, 3];
end
if checkcable4 == 1
    Cables = [Cables, 4];
end
if checkcable5 == 1
    Cables = [Cables, 5];
end


Distances = linspace(distance_start,distance_stop,100);

%% Parameter
%delta_f = 4.3125e3;
%Bandwidth = 17.664e6;
%Annex_Freq_Offset = 138e3;
KilometerPerMiles = 1.609344;
%TX_Power_all_db = 14.5;%dBm
%TX_Power=1; % in dBm/deltaf ~ -36dBm/Hz
%ThermalNoisePerCarrier = -174+10*log10(delta_f); %in dB
%distance = 3.7; %in km
%MaxSNR_dB = 50; % in dB


freq = [freq1,freq2];

K = length(Distances);
M = length(Cables);
L = length(freq);
Legend = {};
Attenuation = zeros(K,L);
hold all;
for m=1:M
    cableidx = Cables(m);

    Capacity_array=zeros(1,K);
    for k=1:K
        distance = Distances(k);
        
        %% Calculate frequencies
        
        %% get cable parameters
        F = freq;
        [Zs,Yp,Name] = get_cable_parameterG9071(F,cableidx);
        Zs = Zs*1000;
        Yp = Yp*1000;
        
        gamma = sqrt(Zs.*Yp);
        ZL = sqrt(Zs./Yp);
        
        reflection_r1=0;
        reflection_r2=0;
        Z1 = ZL*(1+reflection_r1)/(1-reflection_r1);
        Z2 = ZL*(1+reflection_r2)/(1-reflection_r2);

        totaldistance = distance;
        distance=totaldistance;

        F_omega= (ZL./(Z1+ZL)).*(1./(1-reflection_r1.*reflection_r2.*exp(-2.*gamma.*totaldistance))).*(exp(-1.*gamma.*distance)+reflection_r2.*exp(-1.*gamma.*(2.*totaldistance-distance)));

        Amplitude = abs(F_omega);
        H_dB = 20*log10(Amplitude);
        Attenuation(k,:)=H_dB;

    end
    %% plot
   % for l =1:L
   %     if freq(l) >= 1e6
   %         Legend{length(Legend)+1}=sprintf('%s %.1f MHz',Name,freq(l)/1e6);
   %     elseif freq(l) >= 1e3
   %         Legend{length(Legend)+1}=sprintf('%s %.1f kHz',Name,freq(l)/1e3);
   %     else
   %         Legend{length(Legend)+1}=sprintf('%s %.1f Hz',Name,freq(l));
   %     end
   % end
    
    plot(Distances,Attenuation(:,1),'-');
    Legend{length(Legend)+1}='';
    plot(Distances,Attenuation(:,2),'--');
    Legend{length(Legend)+1}='';
    idizes2plot = 1:round(K*(1-0.02*m));
    filler=fill([Distances(idizes2plot)';flipud(Distances(idizes2plot)')],[Attenuation(idizes2plot,1);flipud(Attenuation(idizes2plot,2))],m)
    Legend{length(Legend)+1}=sprintf('%s',Name);

end
 xlabel('Distance [km]');
 ylabel('Attenuation [dB]');
h=legend(Legend,'location','southwest');
 legend boxoff
 set (h, "fontsize", 10);
 grid on;
 




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


end
