home *** CD-ROM | disk | FTP | other *** search
-
- #define BLOCK_SIZE 10
- #define NOT_YET -1.0
-
- float mv_avg( unsigned int sample )
- {
- static int ptr = 0;
- static int count = 1;
- static long total = 0;
- static unsigned int block[BLOCK_SIZE - 1];
-
- if( count == BLOCK_SIZE ) /* start returning averaged
- results on Nth sample */
- {
- total += sample - block[ptr];
- block[ptr] = sample;
- ptr = ( ptr < BLOCK_SIZE - 1 ) ? ptr++ : 0;
- return ( ( float ) total / BLOCK_SIZE );
- }
- else
- {
- count++;
- block[ptr++] = sample;
- total += sample;
- return ( NOT_YET );
- }
- }
-