OpenWareLaboratory
MovingAverage.h
Go to the documentation of this file.
1 #ifndef __MovingAverage_h__
2 #define __MovingAverage_h__
3 
4 #include "SignalProcessor.h"
5 
10 private:
11  float lambda;
12  float y;
13 public:
14  ExponentialMovingAverage(float lambda = 0.995, float y=0): lambda(lambda), y(y) {}
15  void set(float value){
16  y = value;
17  }
23  void setLambda(float value){
24  lambda = value;
25  }
26  float getLambda(){
27  return lambda;
28  }
35  void setSmoothing(size_t observations, float smoothing = 2){
36  lambda = smoothing / (observations + 1);
37  }
43  void setModifiedMovingAverage(size_t N){
44  lambda = 1/N;
45  }
51  void setCutoff(float fc){
52  float cfc = cosf(fc);
53  lambda = cfc - 1 + sqrtf(cfc*cfc - 4*cfc + 3);
54  }
59  float getNextAverage(float x){
60  y = y*lambda + x*(1.0f - lambda);
61  return y;
62  }
63  float getAverage(){
64  return y;
65  }
66  /* process a single sample and return the result */
67  float process(float in){
68  return getNextAverage(in);
69  }
70  void process(FloatArray input){
71  for(size_t i=0; i<input.getSize(); i++)
72  getNextAverage(input[i]);
73  }
75  static ExponentialMovingAverage* create(float lambda){
76  return new ExponentialMovingAverage(lambda);
77  }
78  static void destroy(ExponentialMovingAverage* obj){
79  delete obj;
80  }
81 };
82 
87 private:
88  FloatArray buffer;
89  size_t writeIndex = 0;
90  float sum = 0;
91 public:
93  SimpleMovingAverage(FloatArray buf) : buffer(buf), sum(buf.getSum()) {}
94  void reset(){
95  buffer.clear();
96  sum = 0;
97  }
98  void set(float value){
99  buffer.setAll(value);
100  sum = value * buffer.getSize();
101  }
102  float getNextSum(float value){
103  sum = sum - buffer[writeIndex] + value;
104  buffer[writeIndex] = value;
105  // sum = sum - head() + value;
106  // write(value);
107  if(++writeIndex == buffer.getSize())
108  writeIndex = 0;
109  return sum;
110  }
111  float getNextAverage(float push){
112  return getNextSum(push)/buffer.getSize();
113  }
114  float getAverage(){
115  return sum/buffer.getSize();
116  }
117  size_t getSize(){
118  return buffer.getSize();
119  }
120  float process(float in){
121  return getNextAverage(in);
122  }
123  void process(FloatArray input){
124  for(size_t i=0; i<input.getSize(); i++)
125  getNextSum(input[i]);
126  }
128  static SimpleMovingAverage* create(int samples){
129  return new SimpleMovingAverage(FloatArray::create(samples));
130  }
131  static void destroy(SimpleMovingAverage* buf){
132  FloatArray::destroy(buf->buffer);
133  delete buf;
134  }
135 };
136 
137 #endif // __MovingAverage_h__
Exponential Moving Average EMA.
Definition: MovingAverage.h:9
void setSmoothing(size_t observations, float smoothing=2)
Set lambda (time coefficient) as amount of smoothing over number of observations.
Definition: MovingAverage.h:35
void setModifiedMovingAverage(size_t N)
Set lambda (time coefficient) as 1/N.
Definition: MovingAverage.h:43
float getNextAverage(float x)
RMS detection with smoothing coefficient (Moving RMS) as per: R.
Definition: MovingAverage.h:59
static ExponentialMovingAverage * create(float lambda)
Definition: MovingAverage.h:75
void setLambda(float value)
Set the exponential decay time coefficient.
Definition: MovingAverage.h:23
ExponentialMovingAverage(float lambda=0.995, float y=0)
Definition: MovingAverage.h:14
float process(float in)
Definition: MovingAverage.h:67
static void destroy(ExponentialMovingAverage *obj)
Definition: MovingAverage.h:78
void set(float value)
Definition: MovingAverage.h:15
void setCutoff(float fc)
Set lambda (time coefficient) as the -3dB cutoff frequency, in radians.
Definition: MovingAverage.h:51
void process(FloatArray input)
Definition: MovingAverage.h:70
This class contains useful methods for manipulating arrays of floats.
Definition: FloatArray.h:12
void clear()
Clear the array.
Definition: FloatArray.h:29
static void destroy(FloatArray array)
Destroys a FloatArray created with the create() method.
Definition: FloatArray.cpp:472
static FloatArray create(int size)
Creates a new FloatArray.
Definition: FloatArray.cpp:466
void setAll(float value)
Set all the values in the array.
Definition: FloatArray.cpp:220
Base class for signal processors such as Filters.
virtual float process(float input)
size_t getSize() const
Definition: SimpleArray.h:31
Simple Moving Average SMA.
Definition: MovingAverage.h:86
float process(float in)
void set(float value)
Definition: MovingAverage.h:98
float getNextSum(float value)
static void destroy(SimpleMovingAverage *buf)
float getNextAverage(float push)
SimpleMovingAverage(FloatArray buf)
Definition: MovingAverage.h:93
static SimpleMovingAverage * create(int samples)
void process(FloatArray input)