Listing 6 Definition of MakeHistogram

/********************* DATACOMP.C *********************/
#include <alloc.h>
#include <conio.h>
#include "randefs.h"
#include "datacomp.h"
#include "displays.h"

int MakeHistogram(RAND *dat)
{ int    i, nbin = dat->nbin;
  index *hstogm;
  float *frqdst;

  NewScreen(*(dat->labls+nbin) );
  DrawAxes(dat);
  frqdst = (float *)calloc(nbin, sizeof(float) );
  hstogm = (index *)calloc(nbin, sizeof(index) );
  while ( (dat->ndat >= 0) && (kbhit() == 0) )
  {  dat->ev_indx = SumNPicks(dat);
     UpdateDistrib(dat, hstogm, frqdst);
     DisplayFreq(dat, frqdst);
  }
  if (ESC == getch())  /* User's chance to exit */
  {  free( (void *)frqdst );
     free( (void *)hstogm );
     return(1);
  }
  DisplayStats(dat, frqdst);
  free( (void *)frqdst );
  free( (void *)hstogm );
  if (ESC == getch())  /* User's chance to exit */
     return(1);

  return(0);
}

void UpdateDistrib(RAND *dat, index *hstogm, float *frqdst)
{ int  i, nd = ++dat->ndat, nbin = dat->nbin;
  int indx = dat->ev_indx;
  float k0 = 1/(nd*dat->delta);

  for (i = 0; i < nbin; i++)
     if (indx == i)
     {  ++(*(hstogm+i)); /* Add 1 to bin */
        break;
     }
  for (i = 0; i < nbin; i++)
     *(frqdst+i) = k0*(float)(*(hstogm+i));
}
void AddNoise(int *in, int *out, RAND *noise)
{ int i, nbin = noise->nbin;
  int *input = in, *noisydat = out;
  float sf;
  
  /******* Amplitude scaling factors ********/
  if (nbin < 6)         /* 1 and 2 coins    */
     sf = 5;
  else if (nbin < 11)   /* Single die       */
     sf = 2;
  else if (nbin == 24)  /* Uniform Circular */
     sf = 0.5;
  else                  /* Dice and normal  */
     sf = 0.8;
  
  *noisydat++ = *input++;    /* Copy the data size */
  for (i = 0; i < SPAN; i++)
     *noisydat++ = *input++ + sf*(2*SumNPicks(noise)-(nbin-1));
}

void MakeScopeSignals(RAND *dat, int wt_tbl[][MAXWTLEN])
{ int i, nbin = dat->nbin, *signalbuf, *auxbuf, *wts;

  /******** Simulated Oscilloscope Signals ********/
  NewScreen(*(dat->labls+nbin) );
  ScreenLabels();
  signalbuf = (int *)calloc(SPAN, sizeof(int) );
  auxbuf    = (int *)calloc(SPAN, sizeof(int) );
  /******** Generate Simulated Square Wave ********/
  MakeSqWave(signalbuf, 5);
  DrawData(signalbuf, 0.98*FS);     /* Display it */
  /************* Add Noise to Signal **************/
  AddNoise(signalbuf, auxbuf, dat);
  DrawData(auxbuf, 0.76*FS);        /* Display it */
  /************ Filter the Noisy Signal ***********/
  for (i = 0; i < 3; i++)
  {   Filter(auxbuf, signalbuf, wt_tbl[i] );
     DrawData(signalbuf, (0.54-i*0.22)*FS );
  }
  free((void *)auxbuf);
  free((void *)signalbuf);
}

void MakeSqWave(int *data, int n_halfcycls)
{ int  i, k, *dat = data;

  *dat++ = SPAN;      /* 1st entry = data size */
  for (k = 0; k < n_halfcycls; k++)
     for (i = 0; i < SPAN/n_halfcycls; i++)
        *dat++ = !(k%2) ? 0 : -50;
  
  while ( (dat-data-1) < SPAN )
     *dat++ = 0;       /* Clean-up */
}

void Filter(int *rawdat. int *filtrdat, int *wt_tbl)
{ int  *wt  = wt_tbl, *raw = rawdat, *new = filtrdat;
  int  i, i0, k, nwts, ndat, norm;
  
  nwts = *wt++;  /* size of wt vector = odd no. */
  norm = *wt++;  /* Normalization factor        */
  ndat = *raw++; /* size of data                */
  i0 = nwts/2;
  
  *new++ = ndat-2*i0;  /* Size of filtered data */
  
  for (i = i0; i < ndat-i0; i++)
  {  *new = 0;
     for (k = 0; k < nwts; k++)
        *new += *(raw+k)*(*(wt+k)); /* Get wt'd sum */
     
     *new++ /= norm;                 /* Normalize it */
     raw++;
  }
}

/* End of File */