% datos

H = [0 1   2   3    4   5   6    7    8;
     0 0.2 0.3 0.15 0.1 0.1 0.05 0.05 0.05];
R = 5;
periodo = 1;
buffer = 3;
random = false;

 
H = [  1,  3,   5,   7,   9,  11,  13,  15;
     0.1,0.2, 0.1, 0.3, 0.1, 0.1,0.05,0.05;];
R = 8;
periodo = 1;
buffer = 20;
random = true;
oversampling = 10;


H = [  46.0000  138.0000  230.0000  321.0000  413.0000  504.0000  596.0000  688.0000  779.0000  871.0000;
     0.0026 0.0046 0.0080 0.0133 0.0186 0.0224 0.0398 0.0701 0.3530 0.4676];
R = 700;
periodo = 1;
buffer = 50;
random = true;
oversampling = 10;


frames = 10000;

fprintf('R = %g, B = %g, T = %g, frames = %g\n\n', R, buffer, periodo, frames);

% Primero calcula el buffer por histogramas
H2 = HML_HStretch(H, oversampling);


fprintf('1... Calculando por histograma');
[RFBH, i] = HML_HBufferSP(H2, R, buffer, periodo);

fprintf(' OK! \n');


% Segunda calcula por buffer
fprintf('2... Calculando por M/D/1/K');
WB = HML_MD1KWeightedBlock(H, R, buffer);
WBH = [ linspace(0,length(WB)-1, length(WB)) ; WB]; 


fprintf(' OK! \n');


% Segunda simula el buffer
fprintf('3... Calculando por simulación');

% Genera trafico
T = HML_HGenTraffic(H, frames, random);
[B, CLR3] = HML_Buffer( T, R, periodo, buffer);
BS = HML_HistB( B, RFBH(1,:));
fprintf(' OK! CLR = %g\n', CLR3);

clf

semilogy(RFBH(1,:),RFBH(2,:));
hold all
semilogy(RFBH(1,:),BS(2,:));
hold all
semilogy(WBH(1,:),WBH(2,:));
hold all
grid on
legend('Histogram','Simulation','M/D/1/K');

pause

subplot(2,2,1)
bar(RFBH(1,:),RFBH(2,:));
title('Buffer Histogram');
xlabel('cells in Buffer');
subplot(2,2,2)
bar(WBH(1,:),WBH(2,:));
title('MD1K Queue Histogram');
xlabel('cells in Buffer');
subplot(2,2,3)
bar(RFBH(1,:),BS(2,:));
title('Simulation Histogram');
xlabel('cells in Buffer');
subplot(2,2,4)
semilogy(RFBH(1,:),RFBH(2,:));
hold all
semilogy(RFBH(1,:),BS(2,:));
hold all
semilogy(WBH(1,:),WBH(2,:));
hold all
title('Juntos');
xlabel('cells in buffer');


