%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% .m script of web demo 'Multiuser Capacity' 
% Written for Matlab
% 
% INSTITUTE OF TELECOMMUNICATIONS  
% University of Stuttgart 
% www.inue-uni-stuttgart.de 
% author: Wei Wang, Jintong JU   
% date: 14.12.2020  
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% 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 [] = demo_4_function(P_0,K,webdemo_en,filename)

% Create an invisible figure.
if (webdemo_en)
    fig = figure(1); set(fig, 'visible', 'off');
else
    fig = figure(1); set(fig, 'visible', 'on');
end

% set default Fontsize and name
set(gca, 'defaultlinelinewidth', 2);
set(gca, 'fontname','Helvetica');
set(gca, 'fontsize', 14);

% dBm -> milliwatt
p = 10.^(P_0/10);
k = 1:1:K; % 50 users
r_noma = log2(1 + p*k); % NOMA & FDMA
r_tdma = log2(1 + p); % TDMA
r_cdma = k.*log2(1 + p./(1+p.*(k-1))); % TIN

% Prepare plot
xlabel('k (Number of User)');
ylabel('Sum Capacity in bits/s/Hz');
grid on;
box on;
hold on;
% Plot into the figure
h1 = plot(k,r_noma,'color','k','DisplayName','NOMA/FDMA');
h2 = line([1,k(end)],[r_tdma,r_tdma],'color','b','DisplayName','TDMA');
h3 = plot(k,r_cdma,'color','r','DisplayName','CDMA(TIN)');
legend('location','northwest');

if (webdemo_en)
    print(filename, "-dpng", '-r84');% generate the picture results
end

end