%
% Spracovanie vysledkov zo simulacie algoritmu Vivaldi
% 
% function simRealNet(filename, num_of_nodes, time_interval)
% filename - vstupny subor s datami zo simulacie
% num_of_nodes - pocet pouzitych uzlov pri simulacii
% time_interval - casovy interval zbieranych dat

function simRealNet(filename, num_of_nodes, time_interval)

home;
help(mfilename);

% Definujem si farby pre vykreslovanie podla poctu pouzitych uzlov.
% Pre moje simulacie je postacujucich 8 farieb v RGB
myColors = [0 0 1;		% blue
            0 1 0;		% green
            1 0 0;		% red
            0 1 1;		% cyan
            1 0 1;		% magenta
            1 0.722 0; 	% yellow
            0 0 0;		% black
            0.53 0 1;	% lila
            1 0.384 0];	% orange
        

% spravne formatovanie vystupu
% format long g
format short g

data = load(filename);
[m, n] = size(data);

%najprv zistim maximalny pocet zaznamov
max_items = 0;
for node = 0:num_of_nodes-1
    num_of_items = 0;
    for i = 1:1:m
        if (data(i,1) == node)
            num_of_items = num_of_items + 1;
        end
    end
    if max_items < num_of_items
        max_items = num_of_items;
    end
end


sum_relative_error = [ ; ];
%% postupne pre kazdy uzol budem vykreslovat absolutnu aj relativnu chybu
for node = 0:num_of_nodes-1
    relative_error = [];
    num_of_items = 0;
    for i = 1:1:m
        if (data(i,1) == node)
            num_of_items = num_of_items + 1;
            relative_error = [relative_error data(i,2)];
        end
    end
    
    % doplnim nuly, aby bola rovnaka velkost vektoru
    doplmek = max_items - num_of_items;
    relative_error = [relative_error zeros(doplmek,1)];
    
    time = 0:time_interval:(num_of_items - 1 + doplmek)*time_interval;
    
    sum_relative_error = [sum_relative_error; relative_error];
    
    % vykreslenie grafu s relativnou chybou
    figure(1);
    plot(time,relative_error, 'Color', myColors(mod(node,9)+1,:));
    %gtext(num2str(node));
    hold on;
    
    % string pre vytvorenie legendy
    legend_str{node+1}=sprintf('Uzol č.%g',node);
end


%vytvorenie premernej hodnoty relativnej chyby

avr_relative_error = [];
len = length(sum_relative_error);
for i=0:len-1
    tmp = 0;
    data = sum_relative_error(i*num_of_nodes+1:num_of_nodes*(i+1));    
    for node = 1:num_of_nodes
         tmp = tmp + data(node);
    end;
    avr_relative_error = [ avr_relative_error tmp/num_of_nodes];
end;

len = length(avr_relative_error);
time = 0.5:time_interval:len*time_interval;

figure(2);
plot(time, avr_relative_error);
hold on;


% legendu mozem zobrazit az na konci
% pre oba grafy pouzivam rovanku legendu
figure(1);
	handle = title('Časový priebeh relatívnej chyby');
	set(handle, 'FontName', 'Times New Roman CE')
	handle = xlabel('Čas simulácie [s]');
	set(handle, 'FontName', 'Times New Roman CE')
	handle = ylabel('Súčet relatívnej chyby [-]');
	set(handle, 'FontName', 'Times New Roman CE')
	handle = legend(legend_str);
	set(handle, 'FontName', 'Times New Roman CE')

figure(2);
	handle = title('Časový priebeh relatívnej chyby');
	set(handle, 'FontName', 'Times New Roman CE')
	handle = xlabel('Čas simulácie [s]');
	set(handle, 'FontName', 'Times New Roman CE')
	handle = ylabel('Priemerna relatívna chyba uzlov v systeme [-]');
	set(handle, 'FontName', 'Times New Roman CE')
   