/*******************************************************************************
 *
 *  processing of Monte Carlo simulation data for iterative decoding
 *  Copyright (C) 2006-2009 Misha Stepanov
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 ******************************************************************************/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <time.h>

typedef double real;

#define N_ITER 16384

int main(int argc, char **argv)
 {
  long long int NUM[N_ITER + 2], ERR[N_ITER + 2], TOT_NUM, TOT_ERR;
  int n_iter[16] = {0, 1, 2, 4, 8, 16, 32, 64, 128, 256,
                    512, 1024, 2048, 4096, 8192, 16384};
  real SNR2;

  int i, j;
  char filename[256];
  FILE *out;

  for (SNR2 = 1.; SNR2 <= 5.; SNR2 += 0.01)
   {
    sprintf(filename, "data_id_mc_awgn/num_%4.2f", SNR2);
    out = fopen(filename, "r");
    if (out != NULL)
     {
      for (i = 0; i <= (N_ITER + 1); i++)
        fscanf(out, "%d %Ld %Ld\n", &j, &(ERR[i]), &(NUM[i]));
      fclose(out);

      for (TOT_NUM = 0, i = 0; i <= (N_ITER + 1); i++) TOT_NUM += NUM[i];

      for (j = 0; j < 16; j++)
       {
        TOT_ERR = 0;
        for (i = 0; i <= n_iter[j]; i++) TOT_ERR += ERR[i];
        for (; i <= (N_ITER + 1); i++) TOT_ERR += NUM[i];

        sprintf(filename, "data_id_mc_awgn/plot/plot_%05d", n_iter[j]);
        out = fopen(filename, "a");
        fprintf(out, "%4.2f %Ld %Ld\n", SNR2, TOT_ERR, TOT_NUM);
        fclose(out);
       }
     }
   }

  return 0;
 }

