PicoLowLevel
Loading...
Searching...
No Matches
MovingAvgFilter.h
Go to the documentation of this file.
1#ifndef MOVING_AVG_FILTER_H
2#define MOVING_AVG_FILTER_H
3
4#include "Filter.h"
5
10template<typename T>
11class MovingAvgFilter: public Filter<T> {
12 public:
17 MovingAvgFilter(int nsamples) {
18 this->nsamples = nsamples;
19 last = 0;
20 total = (T)0;
21 samples = new T[nsamples];
22 for(int i = 0; i<nsamples; i++) samples[i] = (T)0;
23 }
24
30 T filter(T value) {
31 total -= samples[last];
32 samples[last] = value;
33 total += value;
34
35 if(++last >= nsamples) last = 0;
36
37 return (T)(total/nsamples);
38 }
39
40 private:
41 T total;
42 T *samples;
43 int nsamples, last;
44};
45
46#endif
Interface for a generic filter, with parameterized type.
Definition Filter.h:9
Class for a moving average filter implementing the Filter interface.
Definition MovingAvgFilter.h:11
T filter(T value)
Computes the filtered output taking into consideration the last nsamples values.
Definition MovingAvgFilter.h:30
MovingAvgFilter(int nsamples)
Constructor for the moving average filter.
Definition MovingAvgFilter.h:17