home *** CD-ROM | disk | FTP | other *** search
/ The C Users' Group Library 1994 August / wc-cdrom-cusersgrouplibrary-1994-08.iso / listings / v_07_08 / v7n8058a.txt < prev    next >
Text File  |  1989-03-22  |  635b  |  28 lines

  1.  
  2. #define BLOCK_SIZE 10
  3. #define NOT_YET -1.0
  4.  
  5. float mv_avg( unsigned int sample )
  6. {
  7.    static int ptr = 0;
  8.    static int count = 1;
  9.    static long total = 0;
  10.    static unsigned int block[BLOCK_SIZE - 1];
  11.  
  12.    if( count == BLOCK_SIZE ) /* start returning averaged
  13.                                     results on Nth sample */
  14.    {  
  15.      total += sample - block[ptr];   
  16.      block[ptr] = sample;
  17.      ptr = ( ptr < BLOCK_SIZE - 1 ) ? ptr++ : 0;
  18.      return ( ( float ) total / BLOCK_SIZE );    
  19.    }
  20.    else
  21.    { 
  22.      count++;
  23.      block[ptr++] = sample;
  24.      total += sample;
  25.      return ( NOT_YET );
  26.    } 
  27. }
  28.