libwt  1.0.0
A C++ library for the continous wavelet transform.
movingaverage.hh
1 #ifndef __WT_MOVINGAVERAGE_HH__
2 #define __WT_MOVINGAVERAGE_HH__
3 
4 #include <Eigen/Eigen>
5 
6 namespace wt {
7 
9 template <class Scalar>
10 class MovingSum
11 {
12 public:
14  typedef Eigen::Matrix<Scalar, 1, Eigen::Dynamic> RowVector;
15 
16 public:
20  MovingSum(size_t N, size_t M)
21  : _hist(N,M), _current(N), _currentIdx(0)
22  {
23  _hist.setZero();
24  _current.setZero();
25  }
26 
30  void resize(size_t N, size_t M) {
31  _hist.resize(N,M); _hist.setZero();
32  _current.resize(N); _current.setZero();
33  _currentIdx=0;
34  }
35 
37  template <class Derived>
38  const RowVector &operator() (const Eigen::DenseBase<Derived> &in) {
39  _current -= _hist.row(_currentIdx);
40  _hist.row(_currentIdx) = in;
41  _current += _hist.row(_currentIdx);
42  _currentIdx++;
43  // modulo N
44  if (_hist.rows()==_currentIdx) { _currentIdx=0; }
45  return _current;
46  }
47 
49  const RowVector &addZero() {
50  _current -= _hist.row(_currentIdx);
51  _hist.row(_currentIdx).setZero();
52  _currentIdx++;
53  // modulo N
54  if (_hist.rows()==_currentIdx) { _currentIdx=0; }
55  return _current;
56  }
57 
58 protected:
60  Eigen::Matrix<Scalar, Eigen::Dynamic, Eigen::Dynamic> _hist;
62  Eigen::Matrix<Scalar, 1, Eigen::Dynamic> _current;
65  size_t _currentIdx;
66 };
67 
68 }
69 
70 #endif // __WT_MOVINGAVERAGE_HH__
void resize(size_t N, size_t M)
Resizes the moving sum operator.
Definition: movingaverage.hh:30
const RowVector & addZero()
Adds a vector and updates the moving sum.
Definition: movingaverage.hh:49
A simple vector-valued moving sum.
Definition: movingaverage.hh:10
Eigen::Matrix< Scalar, Eigen::Dynamic, Eigen::Dynamic > _hist
Holds the "history" of the last N vectors (stored in rows).
Definition: movingaverage.hh:60
Eigen::Matrix< Scalar, 1, Eigen::Dynamic > RowVector
Specifies the vector type to sum over.
Definition: movingaverage.hh:14
Eigen::Matrix< Scalar, 1, Eigen::Dynamic > _current
Holds the current sum.
Definition: movingaverage.hh:62
size_t _currentIdx
Holds the index, where the next vector should be inserted into _hist.
Definition: movingaverage.hh:65
MovingSum(size_t N, size_t M)
Constructor.
Definition: movingaverage.hh:20
Definition: convolution.hh:7
const RowVector & operator()(const Eigen::DenseBase< Derived > &in)
Adds a vector and updates the moving sum.
Definition: movingaverage.hh:38