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

% 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_1_function(p_1,p_2,sic,cdma,fdma,tdma,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', 17);
%set(0, 'defaultaxesfontsize', 8);
%set(0, 'defaultaxesfontname', 'Times New Roman');
%set(gca, 'defaulttextfontsize', 8);
%set(gca, 'defaulttextfontname', 'Times New Roman');


% define format
%landscape = '-S930,350';
%portrait  = '-S640,480';

% select format
%output_format = portrait;

p_1=10.^(p_1/10);
p_2=10.^(p_2/10);
r_1=log2(1+p_1);
r_2=log2(1+p_2);
r_1_left=log2(1+p_1+p_2)-r_2;
r_2_down=log2(1+p_1+p_2)-r_1;
R_1=r_1_left:r_1/1000:r_1;
R_2=log2(1+p_1+p_2)-R_1;
% Prepare plot
xlabel('R_1 in bits/s/Hz');
ylabel('R_2 in bits/s/Hz');

if (r_1 > r_2)
    axis([0,r_1*1.5,0,r_1*1.5]);
else
    axis([0,r_2*1.5,0,r_2*1.5]);
end
grid on;
box on;
hold on;
% Plot into the figure
h1 = line([0,r_1_left],[r_2,r_2],'Color','k');
h2 = line([r_1,r_1],[0,r_2_down],'Color','k');
h3 = plot(R_1,R_2,'k');

if (sic)
    h3 = plot(R_1,R_2,'r');
    h4 = line([0,r_1],[r_2_down,r_2_down],'linestyle','--','color','r');
    h5 = line([r_1_left,r_1_left],[0,r_2],'linestyle','--','color','r');
    h6 = plot([r_1_left,r_1],[r_2,r_2_down],'r.','MarkerSize',30);
    A=['A(R_1=',num2str(r_1_left),', R_2=',num2str(r_2),')'];
    B=['B(R_2=',num2str(r_1),', R_1=',num2str(r_2_down),')'];
    %A=['A(R_1=',num2str(round(r_1_left,3)),', R_2=',num2str(round(r_2,3)),')'];
    %B=['B(R_2=',num2str(round(r_1,3)),', R_2=',num2str(round(r_2_down,3)),')'];
    text(r_1_left*1.03,r_2*1.03,A,'FontSize',15);
    text(r_1,r_2_down*1.1,B,'FontSize',15);
end

if (fdma)
    p_1_plot=p_1*ones(1,200);
    p_2_plot=p_2*ones(1,200);
    alpha=0.005:0.005:1; % 200 points
    r_1 = alpha.*log2(1+p_1_plot./alpha);
    r_2 = (1-alpha).*log2(1+p_2_plot./(1-alpha));
    h7 = plot(r_1,r_2,'b');
end

if (tdma)
    p_1_plot=p_1*ones(1,200);
    p_2_plot=p_2*ones(1,200);
    alpha=0.005:0.005:1;
    r_1 = alpha.*log2(1+p_1_plot);
    r_2 = (1-alpha).*log2(1+p_2_plot);
    h8 = plot(r_1,r_2,'m');
end

if (cdma)
    r_1=0.5*log2(1+2*p_1);
    r_2=0.5*log2(1+2*p_2);
    h9 = plot(r_1,r_2,'g.','MarkerSize',30);
    text(r_1*1.05,r_2*1.05,'C','FontSize',15);
    h10 = line([0,r_1],[r_2,r_2],'linestyle','--','color','g');
    h11 = line([r_1,r_1],[0,r_2],'linestyle','--','color','g');
end

if (webdemo_en)
    %set(fig, 'Position', [0 0 640 480])
    set(gca,'units','pixels','outerposition',[-15,-15,640,480])
    %set(gcf,'units','pixels','Position',[0 0 640 480])
    %set(fig, 'Position', [0 0 640 480])
    print(filename,'-dpng'); % generate the picture results
end

end
