home *** CD-ROM | disk | FTP | other *** search
/ The C Users' Group Library 1994 August / wc-cdrom-cusersgrouplibrary-1994-08.iso / listings / v_10_05 / 1005052b < prev    next >
Text File  |  1992-03-11  |  1KB  |  37 lines

  1. Listing 4. Filter with circular buffer
  2.           
  3.           /* Filters the vector, v, of input values, putting the
  4.            * results back in the same vector.
  5.            */
  6.           void filter(Filter *f, double *v, int length)
  7.           {
  8.               /* Making a local copy saves execution time */
  9.               CircularBuffer y = *(f->y);
  10.           
  11.               /* Loop through the vector values */
  12.               for(; length--; v++)
  13.               {
  14.                  double
  15.                      sum = *v,      /* becomes filter output */
  16.                      *c = f->coef;
  17.                  int k = f->order,  /* The number of coefficients */
  18.                      i = y.index;   /* Index to previous outputs */
  19.           
  20.                  /* Loop through filter coefficients */
  21.                  for(; k--; c++)
  22.                  {
  23.                      i = (i - 1) & y.mask;
  24.                      sum -= *c * y.buffer[i];
  25.                  }
  26.                  /* Save output for next data point */
  27.                  y.buffer[y.index] = sum;
  28.                  y.index = (y.index + 1) & y.mask;
  29.           
  30.                  *v = sum; /* Set output value */
  31.               }
  32.               /* Store the local copy back in the filter structure */
  33.               *(f->y) = y;
  34.           }
  35.  
  36.  
  37.