RunningAverage< T > Class Template Reference

Implements a running average filter. More...

#include <RunningAverage.h>

List of all members.

Public Member Functions

Get ()
 Gives the current running average.
int GetLength () const
 Tells the length in samples of this running average filter.
void Put (T sample)
 Feeds a sample into the running average filter.
void Reset ()
 Resets this running average filter.
 RunningAverage (int nLength)
 Constructor.
 ~RunningAverage ()
 Destructor.

Protected Attributes

int _iPos
 Store position in the buffer.
int _nLength
 Length in samples of the running average filter.
T * _pBuffer
 Pointer to the internal buffer used to store the past samples.
_sum
 Up-to-date sum of the past samples.


Detailed Description

template<typename T>
class RunningAverage< T >

Implements a running average filter.

Parameters:
T Defines the sample type for the running average.
Remarks:
This class was optimized for use with float: using an integer 16-bits sample type can cause precision loss or overflow.

Definition at line 45 of file RunningAverage.h.


Constructor & Destructor Documentation

template<typename T>
RunningAverage< T >::RunningAverage int  nLength  )  [inline]
 

Constructor.

Parameters:
nLength Length in samples of the running average filter.

Definition at line 85 of file RunningAverage.h.

References RunningAverage< T >::_nLength, and RunningAverage< T >::_pBuffer.

00086         : _nLength(nLength),
00087         _sum(0),
00088         _iPos(0)
00089     {
00090         _pBuffer = new T[_nLength];
00091         memset(_pBuffer, 0, _nLength * sizeof(T));
00092     };

template<typename T>
RunningAverage< T >::~RunningAverage  )  [inline]
 

Destructor.

Definition at line 98 of file RunningAverage.h.

References RunningAverage< T >::_pBuffer.

00098                       {
00099         delete _pBuffer;
00100     };


Member Function Documentation

template<typename T>
T RunningAverage< T >::Get  )  [inline]
 

Gives the current running average.

Returns:
Current value of the running average.

Definition at line 145 of file RunningAverage.h.

References RunningAverage< T >::_nLength, and RunningAverage< T >::_sum.

00145                    {
00146         return _sum / (T)_nLength;
00147     }

template<typename T>
int RunningAverage< T >::GetLength  )  const [inline]
 

Tells the length in samples of this running average filter.

Returns:
Length in samples of this running average filter.

Definition at line 110 of file RunningAverage.h.

References RunningAverage< T >::_nLength.

00110 { return _nLength; };

template<typename T>
void RunningAverage< T >::Put sample  )  [inline]
 

Feeds a sample into the running average filter.

Parameters:
sample The sample to be fed in.

Definition at line 130 of file RunningAverage.h.

References RunningAverage< T >::_iPos, RunningAverage< T >::_nLength, RunningAverage< T >::_pBuffer, and RunningAverage< T >::_sum.

00130                               {
00131         _sum -= _pBuffer[_iPos];        // Subtract the last sample
00132         _sum += sample;                 // Add the new sample to the total sum
00133         _pBuffer[_iPos] = sample;       // Store the new sample
00134         _iPos = (_iPos+1) % _nLength;   // Advance write position
00135     }

template<typename T>
void RunningAverage< T >::Reset  )  [inline]
 

Resets this running average filter.

Definition at line 116 of file RunningAverage.h.

References RunningAverage< T >::_iPos, RunningAverage< T >::_nLength, RunningAverage< T >::_pBuffer, and RunningAverage< T >::_sum.

00116                         {
00117         memset(_pBuffer, 0, _nLength * sizeof(T));
00118         _sum = 0;
00119         _iPos = 0;
00120     };


Member Data Documentation

template<typename T>
int RunningAverage< T >::_iPos [protected]
 

Store position in the buffer.

Definition at line 66 of file RunningAverage.h.

Referenced by RunningAverage< T >::Put(), and RunningAverage< T >::Reset().

template<typename T>
int RunningAverage< T >::_nLength [protected]
 

Length in samples of the running average filter.

Definition at line 54 of file RunningAverage.h.

Referenced by RunningAverage< T >::Get(), RunningAverage< T >::GetLength(), RunningAverage< T >::Put(), RunningAverage< T >::Reset(), and RunningAverage< T >::RunningAverage().

template<typename T>
T* RunningAverage< T >::_pBuffer [protected]
 

Pointer to the internal buffer used to store the past samples.

Definition at line 60 of file RunningAverage.h.

Referenced by RunningAverage< T >::Put(), RunningAverage< T >::Reset(), RunningAverage< T >::RunningAverage(), and RunningAverage< T >::~RunningAverage().

template<typename T>
T RunningAverage< T >::_sum [protected]
 

Up-to-date sum of the past samples.

Definition at line 72 of file RunningAverage.h.

Referenced by RunningAverage< T >::Get(), RunningAverage< T >::Put(), and RunningAverage< T >::Reset().


Generated on Mon Jan 15 17:13:41 2007 for "ag-works goodies" by  doxygen 1.4.5