//====================================================== file = burst.c =====
//=  A simulation of burst errors in a bit stream                           =
//=   - Correlation of bit errors is included in this model                 =
//===========================================================================
//=  Notes:                                                                 =
//=    1) #define NUM_BITS tunes the execution time and #define MAX_BURST   =
//=       determines maximum burst size to be counted.                      =
//=    4) The "factor" input allows for modeling of correlation.  Given     =
//=       a bit error, the next bit has factor * p probability of being     =
//=       in error.                                                         =
//=-------------------------------------------------------------------------=
//= Example execution:                                                      =
//=                                                                         =
//=   Enter probability of a bit error ===================> 0.08            =
//=   Enter correlation factor (1.0 for independent) =====> 1.00            =
//=   ---------------------------------------------- burst.c -----          =
//=   -  Results for 10000000 bits...                                       =
//=   -    Pr[a given bit is good] = 0.9200142                              =
//=   -    Pr[a given bit is bad]  = 0.0799858                              =
//=   -    Total number of bursts  = 735790                                 =
//=   ------------------------------------------------------------          =
//=   -    Pr[a given bit is in a burst of size = 1] = 0.0676811            =
//=   -    Pr[a given bit is in a burst of size = 2] = 0.0054306            =
//=   -    Pr[a given bit is in a burst of size = 3] = 0.0004288            =
//=   -    Pr[a given bit is in a burst of size = 4] = 0.0000360            =
//=   -    Pr[a given bit is in a burst of size = 5] = 0.0000020            =
//=   ------------------------------------------------------------          =
//=   -    Pr[a given burst is of size = 1] = 0.9198426                     =
//=   -    Pr[a given burst is of size = 2] = 0.0738064                     =
//=   -    Pr[a given burst is of size = 3] = 0.0058277                     =
//=   -    Pr[a given burst is of size = 4] = 0.0004893                     =
//=   -    Pr[a given burst is of size = 5] = 0.0000272                     =
//=   ------------------------------------------------------------          =
//=-------------------------------------------------------------------------=
//=  Build: bcc32 burst.c                                                   =
//=-------------------------------------------------------------------------=
//=  Execute: burst                                                         =
//=-------------------------------------------------------------------------=
//=  Author: Kenneth J. Christensen                                         =
//=          University of South Florida                                    =
//=          WWW: http://www.csee.usf.edu/~christen                         =
//=          Email: christen@csee.usf.edu                                   =
//=-------------------------------------------------------------------------=
//=  History: KJC (08/17/02) - Genesis (from parity.c)                      =
//===========================================================================

//----- Include files -------------------------------------------------------
#include <stdio.h>                 // Needed for printf()
#include <stdlib.h>                // Needed for rand(), RAND_MAX, and ato*()

//----- Defines -------------------------------------------------------------
#define TRUE                    1  // Boolean true
#define FALSE                   0  // Boolean false
#define NUM_BITS         10000000  // Number of bits to simulate
#define MAX_BURST               5  // Maximum burst size to count

//===== Main program ========================================================
void main(void)
{
  double   p;                        // Probability of bit error
  double   factor;                   // Factor affecting subsequent bit
  int      burst[4096];              // Burst count vector
  int      burst_size;               // Burst size for this burst
  int      total_bursts;             // Total number of bursts
  int      burst_flag;               // Within a burst flag
  double   z;                        // Uniform RV between 0.0 and 1.0
  char     temp_string[80];          // Temporary string
  int      i, j;                     // Loop counters

  // Prompt for probability of a bit error (p)
  printf("Enter probability of a bit error ===================> ");
  scanf("%s", temp_string);
  p = atof(temp_string);

  // Prompt for correlation "factor" (factor)
  printf("Enter correlation factor (1.0 for independent) =====> ");
  scanf("%s", temp_string);
  factor = atof(temp_string);

  // Clear the burst count vector
  for (i=0; i<=MAX_BURST; i++)
    burst[i] = 0;

  // Simulate for NUM_BITS
  burst_size = total_bursts = 0;
  burst_flag = FALSE;
  for (i=0; i<NUM_BITS; i++)
  {
    // Pull a uniform RV between 0 and 1
    z = (double) rand() / RAND_MAX;

    // Determine if this bit is in error
    if ((z < p) && (burst_flag == FALSE))
    {
      burst_flag = TRUE;
      total_bursts++;
      burst_size++;
      burst[0]++;
    }
    else if ((z < (factor * p)) && (burst_flag == TRUE))
    {
      burst_size++;
    }
    else
    {
      burst_flag = FALSE;
      burst[burst_size]++;
      burst_size = 0;
    }
  }

  // Compute and output the results
  printf("---------------------------------------------- burst.c ----- \n");
  printf("-  Results for %d bits...            \n", NUM_BITS);
  printf("-    Pr[a given bit is good] = %3.7f  \n",
    (double) burst[0] / NUM_BITS);
  printf("-    Pr[a given bit is bad]  = %3.7f  \n",
    1.0 - (double) burst[0] / NUM_BITS);
  printf("-    Total number of bursts  = %d     \n", total_bursts);
  printf("------------------------------------------------------------ \n");
  for (i=1; i<=MAX_BURST; i++)
    printf("-    Pr[a given bit is in a burst of size = %d] = %3.7f \n",
      i, (double) burst[i] / NUM_BITS);
  printf("------------------------------------------------------------ \n");
  for (i=1; i<=MAX_BURST; i++)
    printf("-    Pr[a given burst is of size = %d] = %3.7f \n",
      i, (double) burst[i] / total_bursts);
  printf("------------------------------------------------------------ \n");
}

