00001 // 00002 // Copyright(c) 1993-1996 Tony Kirke 00003 // author="Tony Kirke" * 00004 /* 00005 * SPUC - Signal processing using C++ - A DSP library 00006 * 00007 * This program is free software; you can redistribute it and/or modify 00008 * it under the terms of the GNU General Public License as published by 00009 * the Free Software Foundation; either version 2, or (at your option) 00010 * any later version. 00011 * 00012 * This program is distributed in the hope that it will be useful, 00013 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00015 * GNU General Public License for more details. 00016 * 00017 * You should have received a copy of the GNU General Public License 00018 * along with this program; if not, write to the Free Software 00019 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. 00020 */ 00021 #ifndef RUNAV 00022 #define RUNAV 00023 #include <delay.h> 00024 namespace SPUC { 00028 00029 00030 00031 00032 template <class Numeric> class running_average 00033 { 00034 protected: 00035 Numeric result; 00036 delay<Numeric>* z; 00037 long size; 00038 00039 public: 00041 running_average(long n=0) : z(NULL) { 00042 int i=0; 00043 size = n; 00044 if (n>1) { 00045 z = new delay<Numeric>(n-1); 00046 z->reset(); 00047 } 00048 result = 0; 00049 } 00051 running_average& operator=(const running_average &rhs) { 00052 z = rhs.z; 00053 result = rhs.result; 00054 return(*this); 00055 } 00057 void set_size(long n) { 00058 if (z) delete z; 00059 else { 00060 z = new delay<Numeric>(n-1); 00061 result = 0; 00062 size = n; 00063 } 00064 } 00066 ~running_average(void) { if (z) delete z;} 00068 void reset(void) { z->reset(); result = 0;} 00070 Numeric get_result() { return(result);} 00072 Numeric update(Numeric in) { 00073 result -= z->last(); 00074 z->input(in); 00075 result += in; 00076 return(result); 00077 } 00078 }; 00079 } // namespace SPUC 00080 #endif