% datos
H = [2.5, 7.5, 12.5, 17.5, 22.5, 27.5, 32.5, 37.5, 42.5, 47.5;
     0.1, 0.3,  0.2,  0.1,  0.1,  0.1, 0.05, 0.02, 0.02, 0.01;]
 
 
%H = [25, 75, 125, 175, 225, 275, 325, 375, 425, 475;
%     0.1, 0.3,  0.2,  0.1,  0.1,  0.1, 0.05, 0.02, 0.02, 0.01;]


%H = [  0,  1,   2,   3,   4,   5,   6,   7;
%     0.1,0.2, 0.1, 0.3, 0.1, 0.1,0.05,0.05;]

H = [2.5, 7.5, 12.5, 17.5, 22.5, 27.5, 32.5, 37.5;
     0.1,0.2, 0.1, 0.3, 0.1, 0.1,0.05,0.05;]

%H = [ 1000, 3000, 5000; 0.5, 0.3, 0.2];
%H = [ 500, 1500, 2500; 0.5, 0.3, 0.2];
%H = [ 100, 300, 500; 0.5, 0.3, 0.2];
%H = [ 1, 3, 5; 0.5, 0.3, 0.2];

R = 180;
periodo = 0.1;
buffer = 25;
frames = 20000;
oversampling = 1;
pruebas = 1;

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

% Primero calcula el buffer por histogramas
fprintf('1a... Calculando por histograma');
bins = length(H(1,:));
[RFBH, CLR1, i] = HML_HBufferSP(H, R, buffer, periodo);
fprintf(' OK! CLR = %g\n', CLR1);

% Primero calcula el buffer por histogramas
fprintf('1b... Calculando por histograma (con oversampling)');
H2 = HML_HStretch(H, oversampling);
binsH = length(H2(1,:));
[RFBHo, CLR2, i] = HML_HBufferSP(H2, R, buffer, periodo);
fprintf(' OK! CLR = %g\n', CLR2);


% Segunda simula el buffer
fprintf('2... Calculando por simulación %g pruebas\n\n', pruebas);
for i = 1:pruebas
    % Genera trafico
    T = HML_HGenTraffic(H, frames, true);
    %B = HML_Load( T, R, periodo, buffer);
    [B, CLR(i)] = HML_Buffer( T, R, periodo, buffer);
    BS = HML_HistB( B, RFBH(1,:));
    BSo = HML_HistB ( B, RFBHo(1,:));
    DIFF(i) = norm(RFBH(2,:)-BS(2,:));
    DIFFo(i) = norm(RFBHo(2,:)-BSo(2,:)); 
    fprintf(' [%g] CLR = %g Diff = %g Diffo = %g\n', i, CLR(i), DIFF(i), DIFFo(i));
end    
fprintf('\n OK! CLR = %g DIFF = %g DIFFo = %g\n', mean(CLR), mean(DIFF), mean(DIFFo));


[MUHAT, SIGMAHAT, MUCI, SIGMACI] = normfit(CLR);
fprintf('\n OK! CLR = %g +-  %g \n', MUHAT, MUHAT-MUCI(1));
[MUHAT, SIGMAHAT, MUCI, SIGMACI] = normfit(DIFF);
fprintf('      DIFF = %g +- %g \n', MUHAT, MUHAT-MUCI(1));
[MUHAT, SIGMAHAT, MUCI, SIGMACI] = normfit(DIFFo);
fprintf('      DIFFo = %g +- %g \n', MUHAT, MUHAT-MUCI(1));


clf
semilogy(BSo(1,:),BSo(2,:));
hold all
semilogy(RFBH(1,:),BS(2,:));
hold all
semilogy(RFBHo(1,:),RFBHo(2,:));
grid on
legend('Simulation','Histogram','Histogram x 5');
xlabel('bits in buffer');



todos = 0;
if todos
% Dibujamos
subplot(3,2,1)
bar(H(1,:), H(2,:));
title('Load Histogram');
xlabel('traffic load in period (bits)');
subplot(3,2,2)
plot(T)
title('Simulation traffic');
xlabel('frame number');
subplot(3,2,3)
bar(RFBH(1,:),RFBH(2,:));
title('Buffer Histogram');
xlabel('bits in buffer');
subplot(3,2,4)
bar(BS(1,:),BS(2,:));
title('Simulation Histogram');
xlabel('bits in buffer');
subplot(3,2,5)
bar(RFBH(1,:),RFBH(2,:)-BS(2,:));
title('Absolute Difference ');
xlabel('bits in buffer');
subplot(3,2,6)
%semilogy(RFBH(1,:),RFBH(2,:));
plot(RFBH(1,:),RFBH(2,:));
hold all
%semilogy(RFBH(1,:),BS(2,:));
plot(RFBH(1,:),BS(2,:));
grid on
legend('Histogram','Simulation');
xlabel('bits in buffer');
title('All');
end

