% Pruebas de la relación entre el numero de clases y la precision

%%%%precision1
R = 9000000;
buffer = inf;
%%%%precision2
%R = 7000000;
%buffer = 200000;
%%%%precision3
%R = 8000000;
%buffer = 400000;

periodo = 1/25;
load Mawi1999B_40ms1h
load Mawi1999B_T1h
tramas = length(Mawi1999B_40ms1h);
T = Mawi1999B_40ms1h(1:tramas);


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

clf

% Primero simula el buffer con llegada
fprintf('1... Calculando por simulación paquetes');
[B, CLRs] = HML_BufferT( Mawi1999B_T1h, R, buffer, periodo, tramas*periodo*1e6);
fprintf('\n OK! CLR =%g    \n', CLRs);
 

k = 1;
for bins = 6:2:100

    % Calculo por histograms sin oversampling
    LH = HML_Hist2( T, bins);
    [RFBH, CLR, i] = HML_HBufferSP(LH, R, buffer, periodo);
%    fprintf(' OK! CLR =%g    bins = %g\n', CLR1, binsr);
    CLR1(1,k) = bins;
    CLR1(2,k) = CLR;
    
    % Calculo por histogram con oversampling
    LH2 = HML_Hist2( T, bins);
    LHO = HML_HStretch(LH2, 5);
    [RFBH2, CLR, i] = HML_HBufferSP(LHO, R, buffer, periodo);
    RFBH2 = HML_HShorten(RFBH2, 5);
    CLR2(1,k) = bins;
    CLR2(2,k) = CLR;
    
    
    % Calculo por histogram con oversampling
    LH2 = HML_Hist2( T, bins);
    LHO = HML_HStretch(LH2, 10);
    [RFBH3, CLR, i] = HML_HBufferSP(LHO, R, buffer, periodo);
    RFBH3 = HML_HShorten(RFBH3, 10);
    CLR3(1,k) = bins;
    CLR3(2,k) = CLR;


    % Calculo por histogram con oversampling
    LH2 = HML_Hist2( T, bins);
    LHO = HML_HStretch(LH2, 20);
    [RFBH4, CLR, i] = HML_HBufferSP(LHO, R, buffer, periodo);
    RFBH4 = HML_HShorten(RFBH4, 20);
    CLR4(1,k) = bins;
    CLR4(2,k) = CLR;
    
    % Calcula diferencias
    HBS = HML_HistB( B, RFBH(1,:));
    D1(1,k) = bins;
    D1(2,k) = norm(RFBH(2,:)-HBS(2,:));
    HBS = HML_HistB( B, RFBH2(1,:));
    D2(1,k) = bins;
    D2(2,k) = norm(RFBH2(2,:)-HBS(2,:));
    HBS = HML_HistB( B, RFBH3(1,:));
    D3(1,k) = bins;
    D3(2,k) = norm(RFBH3(2,:)-HBS(2,:));
    HBS = HML_HistB( B, RFBH4(1,:));
    D4(1,k) = bins;
    D4(2,k) = norm(RFBH4(2,:)-HBS(2,:));

    fprintf('\nBins %d Diferencias x1= %d x10 = %d x20 = %d ', bins, D1(2,k), D2(2,k), D3(2,k));
    
    k = k + 1;
    
end




clf
semilogy(D1(1,:),D1(2,:),'LineStyle','-','LineWidth',1);
hold all
semilogy(D2(1,:),D2(2,:),'LineStyle',':','LineWidth',1);
hold all
semilogy(D3(1,:),D3(2,:),'LineStyle','-.','LineWidth',1);
hold all
semilogy(D4(1,:),D4(2,:),'LineStyle','--','LineWidth',1);
hold all
legend('x1','x5','x10','x20');
xlabel('classes');
ylabel('normalized difference')
   set(gca,'FontSize',22);


pause
clf
plot(CLR1(1,:),abs(CLR1(2,:)-CLRs)/CLRs,'LineStyle','-','LineWidth',1);
hold all
plot(CLR2(1,:),abs(CLR2(2,:)-CLRs)/CLRs,'LineStyle',':','LineWidth',1);
hold all
plot(CLR3(1,:),abs(CLR3(2,:)-CLRs)/CLRs,'LineStyle','-.','LineWidth',1);
hold all
plot(CLR4(1,:),abs(CLR4(2,:)-CLRs)/CLRs,'LineStyle','--','LineWidth',1);
hold all
legend('x1','x5','x10','x20');
xlabel('classes');
ylabel('cell loss ratio difference')
set(gca,'FontSize',22);


pause

clf
plot(CLR1(1,:),CLR1(2,:),'LineStyle','-','LineWidth',1);
hold all
plot(CLR2(1,:),CLR2(2,:),'LineStyle',':','LineWidth',1);
hold all
plot(CLR3(1,:),CLR3(2,:),'LineStyle','-.','LineWidth',1);
hold all
plot(CLR4(1,:),CLR4(2,:),'LineStyle','--','LineWidth',1);
hold all
plot(CLR1(1,:), ones(1,length(CLR1(1,:)))*CLRs);
legend('x1','x5','x10','x20','Simulation');
xlabel('classes');
ylabel('cell loss ratio')
set(gca,'FontSize',22);

