movingAverage.c

00001 /*
00002   移動平均
00003   Satofumi KAMIMURA
00004   $Id$
00005 */
00006 
00007 #include "movingAverage.h"
00008 
00009 
00010 void initMovingAverage(movingAverage_t *ma, int *buffer, int size_shift) {
00011   int i;
00012 
00013   ma->size = 1 << size_shift;
00014   ma->size_shift = size_shift;
00015   ma->index = 0;
00016   ma->buffer = buffer;
00017   ma->total = 0;
00018   for (i = 0; i < ma->size; ++i) {
00019     ma->buffer[i] = 0;
00020   }
00021 }
00022 
00023 
00024 void setMovingAverage(movingAverage_t *ma, int value) {
00025   ma->total += -ma->buffer[ma->index] + value;
00026   ma->buffer[ma->index] = value;
00027   ma->index = (ma->index + 1) & (ma->size - 1);
00028 }
00029 
00030 
00031 int getAverageValue(movingAverage_t *ma) {
00032   return ma->total >> ma->size_shift;
00033 }
00034 

Generated on Mon Apr 13 22:52:05 2009 by  doxygen 1.5.7.1