%
% Network Lab session data processing
%
% author:
%   Christos Choutouridis AEM:8997
%   cchoutou@ece.auth.gr

% Select title font size
titleFontSize   =10;
max_echoDist    =350;
max_arqDist     =600;

% Select witch session using MATLAB comments. Ctrl-T/CtrlR
% session 1
% Ecode   = 'E7837';
% Qcode   = 'Q5137';
% Rcode   = 'R8316';
% Etime   = '2020-04-11-19:00:52';
% ARQtime = '2020-04-12-12:46:05';

% session 2
Ecode   = 'E1510';
Qcode   = 'Q1643';
Rcode   = 'R7877';
Etime   = '2020-04-14-16:12:13';
ARQtime = '2020-04-15-11:00:14';

% make titles and filenames
file_echo       = sprintf('%s-%s.log', Ecode, Etime);
file_arq        = sprintf('%s-%s-%s.log', Qcode, Rcode, ARQtime);

title_Echo      = sprintf('\\fontsize{%d}Code:%s  Timestamp:%s', titleFontSize, Ecode, Etime);
title_echoDist  = sprintf('\\fontsize{%d}%s: Response time distribution', titleFontSize, Ecode);
title_ARQ       = sprintf('\\fontsize{%d}Code:%s/%s  Timestamp:%s', titleFontSize, Qcode, Rcode, ARQtime);
title_ARQDist   = sprintf('\\fontsize{%d}%s/%s: Response time distribution', titleFontSize, Qcode, Rcode);
title_ARQ_bar   = sprintf('\\fontsize{%d}Distribution of ARQ Transmissions', titleFontSize);
title_ARQ_norm  = sprintf('\\fontsize{%d}Propability Distribution of ARQ Re-transmissions', titleFontSize);

% Echo mechanism
[Fe Te] = Echo_time (file_echo);    % analyze Echo timing

figure('Position', [0 0 1024 600]); % G1: response time
stairs (Te);                        
title (title_Echo);
xlabel('Package Nbr');
ylabel('Response time [msec]');

figure('Position', [0 0 1024 600]); % G1 helper: Response time distribution
bar (Fe(1:max_echoDist));            
title (title_echoDist);
xlabel('Response time [msec]');
ylabel('Packages');


% ARQ mechanism - time analysis
[Fqr, Tqr] = ARQ_time (file_arq);   % analyze ARQ timing

figure('Position', [0 0 1024 600]); % G2: response time
stairs(Tqr);
title (title_ARQ);
xlabel('Package Nbr');
ylabel('Response time [msec]');

figure('Position', [0 0 1024 600]); % G2 helper: Response time distribution
bar (Fqr(1:max_arqDist));
title (title_ARQDist);
xlabel('Response time [msec]');
ylabel('Packages');

% ARQ mechanism - error analysis
[x, Fe, l, ber] = ARQ_error (file_arq);  % analyze ARQ error
Fe_norm = Fe / sum(Fe);             % normalize ARQ error
ft = fittype('a*exp(-b*x)');        % curve fiting
[fitFe] = fit (x, Fe_norm, ft);

figure('Position', [0 0 1024 900]); % Error distribution analysis
subplot(2,1,1);
b = bar(Fe);
xtips = b.XEndPoints;
ytips = b.YEndPoints;
labels = string(b.YData);
text(xtips,ytips,labels,'HorizontalAlignment','center','VerticalAlignment','bottom');
title (title_ARQ_bar);
xlabel('Number of Transmissions');
ylabel('Number of packages');
t = sprintf ('Average tries / pkg = %g', l);
text (8, max(Fe), t);
t = sprintf ('Bit error rate = %g', ber);
text (8, max(Fe)*0.92, t);

subplot(2,1,2); stem(x, Fe_norm);   % G3 helper: Normalized and curve fiting
hold on
plot (fitFe);
title (title_ARQ_norm);
xlabel('Number of Re-transmissions');
ylabel('Package %');
txt = sprintf ('\\leftarrow %gexp(-%gx)', fitFe.a, fitFe.b);
text(0.5, fitFe.a*exp(-fitFe.b*0.5), txt);

