%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% .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 [] = CapacityDistance_fun(idx, delta_f, checkADSL1, checkADSL2, checkVDSL1, checkVDSL2, distance_start, distance_stop, TX_Power_all_db, 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");


Bandwidths = [];
Legend = {};
if checkADSL1 == 1
    Bandwidths = [Bandwidths, 1.1e6];
    Legend{length(Legend)+1}='ADSL 1.1 MHz';
end
if checkADSL1 == 1
    Bandwidths = [Bandwidths, 2.2e6];
    Legend{length(Legend)+1}='ADSL+ 2.2 MHz';
end
if checkVDSL1 == 1
    Bandwidths = [Bandwidths, 17.6e6];
    Legend{length(Legend)+1}='VDSL2 17.6 MHz';
end
if checkVDSL2 == 1
    Bandwidths = [Bandwidths, 30e6];
    Legend{length(Legend)+1}='VDSL2 30MHz';
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 = 45.15; % in dB
c0=299792458; %//exact



K=length(Distances);
M = length(Bandwidths);
for m=1:M
    Bandwidth = Bandwidths(m);
    TX_Power = 10*log10(((10^(TX_Power_all_db/10))/Bandwidth)*delta_f);

    Capacity_array=zeros(1,K);
    for k=1:K
        distance = Distances(k);
        
        %% Calculate frequencies
        freq = Annex_Freq_Offset:delta_f:(Annex_Freq_Offset+Bandwidth);

        %% get cable parameters
        F = freq;
        [Zs,Yp,Name] = get_cable_parameterG9071(F,idx);
        Zs = Zs*1000;
        Yp = Yp*1000;

        %% Telegraphen equation
        Gamma = sqrt(Zs.*Yp);
        alpha = real(Gamma);

        Amplitude = 1/2*exp(-1*distance*alpha);
        H_dB = 20*log10(Amplitude);
        SNR_dB = TX_Power+H_dB-ThermalNoisePerCarrier;
        SNR_dB(SNR_dB>MaxSNR_dB)=MaxSNR_dB;
        SNR_lin = 10.^((SNR_dB)/10);
        Capacity = log2(1+SNR_lin);
        Capacity_Mbitps = sum(Capacity*delta_f)/1e6;
        Capacity_array(k) = Capacity_Mbitps;
    end
    %% plot
    hold all;
    plot(Distances,Capacity_array);

end

 xlabel('Distance [km]')
 ylabel('Capacity [Mbit/s]')
 legend(Legend);
 legend boxoff;
 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
