Embedded Template Library 1.0
Loading...
Searching...
No Matches
pseudo_moving_average.h
Go to the documentation of this file.
1
2
3/******************************************************************************
4The MIT License(MIT)
5
6Embedded Template Library.
7https://github.com/ETLCPP/etl
8https://www.etlcpp.com
9
10Copyright(c) 2018 John Wellbelove
11
12Permission is hereby granted, free of charge, to any person obtaining a copy
13of this software and associated documentation files(the "Software"), to deal
14in the Software without restriction, including without limitation the rights
15to use, copy, modify, merge, publish, distribute, sublicense, and / or sell
16copies of the Software, and to permit persons to whom the Software is
17furnished to do so, subject to the following conditions :
18
19The above copyright notice and this permission notice shall be included in all
20copies or substantial portions of the Software.
21
22THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE
25AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
26LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
27OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
28SOFTWARE.
29******************************************************************************/
30
31#ifndef ETL_CUMULATIVE_MOVING_AVERAGE_INCLUDED
32#define ETL_CUMULATIVE_MOVING_AVERAGE_INCLUDED
33
34#include "platform.h"
35#include "type_traits.h"
36#include "iterator.h"
37
38namespace etl
39{
40 namespace private_pseudo_moving_average
41 {
42 //***************************************************
45 //***************************************************
46 template <typename TPseudo_Moving_Average>
47 class add_insert_iterator : public etl::iterator<ETL_OR_STD::output_iterator_tag, typename TPseudo_Moving_Average::value_type, void, void, void>
48 {
49 public:
50
51 //***********************************
52 explicit add_insert_iterator(TPseudo_Moving_Average& pma) ETL_NOEXCEPT
53 : p_pma(&pma)
54 {
55 }
56
57 //***********************************
58 add_insert_iterator& operator*() ETL_NOEXCEPT
59 {
60 return *this;
61 }
62
63 //***********************************
64 add_insert_iterator& operator++() ETL_NOEXCEPT
65 {
66 return *this;
67 }
68
69 //***********************************
70 add_insert_iterator& operator++(int) ETL_NOEXCEPT
71 {
72 return *this;
73 }
74
75 //***********************************
76 add_insert_iterator& operator =(typename TPseudo_Moving_Average::value_type value)
77 {
78 p_pma->add(value);
79 return *this;
80 }
81
82 private:
83
84 TPseudo_Moving_Average* p_pma;
85 };
86 }
87
88 //***************************************************************************
93 //***************************************************************************
94 template <typename T,
95 const size_t SAMPLE_SIZE,
96 const size_t SCALING = 1U,
97 const bool IsIntegral = etl::is_integral<T>::value,
98 const bool IsFloat = etl::is_floating_point<T>::value>
100
101 //***************************************************************************
107 //***************************************************************************
108 template <typename T, const size_t SAMPLE_SIZE_, const size_t SCALING_>
109 class pseudo_moving_average<T, SAMPLE_SIZE_, SCALING_, true, false>
110 {
111 private:
112
114
115 typedef typename etl::conditional<etl::is_signed<T>::value, int32_t, uint32_t>::type scale_t;
116 typedef typename etl::conditional<etl::is_signed<T>::value, int32_t, uint32_t>::type sample_t;
117
118 static ETL_CONSTANT sample_t SAMPLES = static_cast<sample_t>(SAMPLE_SIZE_);
119 static ETL_CONSTANT scale_t SCALE = static_cast<scale_t>(SCALING_);
120
121 public:
122
123 typedef T value_type;
125
126 static ETL_CONSTANT size_t SAMPLE_SIZE = SAMPLE_SIZE_;
127 static ETL_CONSTANT size_t SCALING = SCALING_;
128
129 //*************************************************************************
132 //*************************************************************************
133 pseudo_moving_average(const T initial_value)
134 : average(initial_value * SCALE)
135 {
136 }
137
138 //*************************************************************************
141 //*************************************************************************
142 void clear(const T initial_value)
143 {
144 average = (initial_value * SCALE);
145 }
146
147 //*************************************************************************
150 //*************************************************************************
151 void add(T new_value)
152 {
153 average *= SAMPLES;
154 average += SCALE * new_value;
155 average /= SAMPLES + sample_t(1);
156 }
157
158 //*************************************************************************
161 //*************************************************************************
162 T value() const
163 {
164 return average;
165 }
166
167 //*************************************************************************
170 //*************************************************************************
171 add_insert_iterator input()
172 {
173 return add_insert_iterator(*this);
174 }
175
176 private:
177
178 T average;
179 };
180
181 template <typename T, const size_t SAMPLE_SIZE_, const size_t SCALING_>
183
184 template <typename T, const size_t SAMPLE_SIZE_, const size_t SCALING_>
186
187 //***************************************************************************
192//***************************************************************************
193 template <typename T, const size_t SCALING_>
194 class pseudo_moving_average<T, 0, SCALING_, true, false>
195 {
197
198 typedef typename etl::conditional<etl::is_signed<T>::value, int32_t, uint32_t>::type scale_t;
199 typedef typename etl::conditional<etl::is_signed<T>::value, int32_t, uint32_t>::type sample_t;
200
201 static ETL_CONSTANT scale_t SCALE = static_cast<scale_t>(SCALING_);
202
203 public:
204
205 typedef T value_type;
207
208 static ETL_CONSTANT size_t SCALING = SCALING_;
209
210 //*************************************************************************
213 //*************************************************************************
214 pseudo_moving_average(const T initial_value, const size_t sample_size)
215 : average(initial_value * SCALE)
216 , samples(sample_t(sample_size))
217 {
218 }
219
220 //*************************************************************************
223 //*************************************************************************
224 void clear(const T initial_value)
225 {
226 average = (initial_value * SCALE);
227 }
228
229 //*************************************************************************
232 //*************************************************************************
233 void set_sample_size(const size_t sample_size)
234 {
235 samples = sample_t(sample_size);
236 }
237
238 //*************************************************************************
241 //*************************************************************************
242 void add(T new_value)
243 {
244 average *= samples;
245 average += SCALE * new_value;
246 average /= samples + sample_t(1);
247 }
248
249 //*************************************************************************
252 //*************************************************************************
253 T value() const
254 {
255 return average;
256 }
257
258 //*************************************************************************
261 //*************************************************************************
262 add_insert_iterator input()
263 {
264 return add_insert_iterator(*this);
265 }
266
267 private:
268
269 T average;
270 sample_t samples;
271 };
272
273 template <typename T, const size_t SCALING_>
275
276 //***************************************************************************
281 //***************************************************************************
282 template <typename T, const size_t SAMPLE_SIZE_>
283 class pseudo_moving_average<T, SAMPLE_SIZE_, 1U, false, true>
284 {
286
287 public:
288
289 typedef T value_type;
291
292 static ETL_CONSTANT size_t SAMPLE_SIZE = SAMPLE_SIZE_;
293
294 //*************************************************************************
297 //*************************************************************************
298 pseudo_moving_average(const T initial_value)
299 : reciprocal_samples_plus_1(T(1.0) / T(SAMPLE_SIZE_ + 1U))
300 , average(initial_value)
301 {
302 }
303
304 //*************************************************************************
307 //*************************************************************************
308 void clear(const T initial_value)
309 {
310 average = initial_value;
311 }
312
313 //*************************************************************************
316 //*************************************************************************
317 void add(const T new_value)
318 {
319 average += (new_value - average) * reciprocal_samples_plus_1;
320 }
321
322 //*************************************************************************
325 //*************************************************************************
326 T value() const
327 {
328 return average;
329 }
330
331 //*************************************************************************
334 //*************************************************************************
335 add_insert_iterator input()
336 {
337 return add_insert_iterator(*this);
338 }
339
340 private:
341
342 const T reciprocal_samples_plus_1;
343 T average;
344 };
345
346 template <typename T, const size_t SAMPLE_SIZE_>
347 ETL_CONSTANT size_t pseudo_moving_average<T, SAMPLE_SIZE_, 1U, false, true>::SAMPLE_SIZE;
348
349 //***************************************************************************
353 //***************************************************************************
354 template <typename T>
355 class pseudo_moving_average<T, 0U, 1U, false, true>
356 {
358
359 public:
360
361 typedef T value_type;
363
364 //*************************************************************************
367 //*************************************************************************
368 pseudo_moving_average(const T initial_value, const size_t sample_size)
369 : reciprocal_samples_plus_1(T(1.0) / T(sample_size + 1U))
370 , average(initial_value)
371 {
372 }
373
374 //*************************************************************************
377 //*************************************************************************
378 void clear(const T initial_value)
379 {
380 average = initial_value;
381 }
382
383 //*************************************************************************
386 //*************************************************************************
387 void set_sample_size(const size_t sample_size)
388 {
389 reciprocal_samples_plus_1 = T(1.0) / (T(sample_size) + T(1));
390 }
391
392 //*************************************************************************
395 //*************************************************************************
396 void add(const T new_value)
397 {
398 average += (new_value - average) * reciprocal_samples_plus_1;
399 }
400
401 //*************************************************************************
404 //*************************************************************************
405 T value() const
406 {
407 return average;
408 }
409
410 //*************************************************************************
413 //*************************************************************************
414 add_insert_iterator input()
415 {
416 return add_insert_iterator(*this);
417 }
418
419 private:
420
421 T reciprocal_samples_plus_1;
422 T average;
423 };
424}
425
426#endif
Definition pseudo_moving_average.h:48
pseudo_moving_average(const T initial_value, const size_t sample_size)
Definition pseudo_moving_average.h:214
void clear(const T initial_value)
Definition pseudo_moving_average.h:224
void add(T new_value)
Definition pseudo_moving_average.h:242
T value() const
Definition pseudo_moving_average.h:253
void set_sample_size(const size_t sample_size)
Definition pseudo_moving_average.h:233
add_insert_iterator input()
Definition pseudo_moving_average.h:262
static ETL_CONSTANT size_t SCALING
The sample scaling factor.
Definition pseudo_moving_average.h:208
add_insert_iterator input()
Definition pseudo_moving_average.h:414
void set_sample_size(const size_t sample_size)
Definition pseudo_moving_average.h:387
pseudo_moving_average(const T initial_value, const size_t sample_size)
Definition pseudo_moving_average.h:368
T value() const
Definition pseudo_moving_average.h:405
void add(const T new_value)
Definition pseudo_moving_average.h:396
void clear(const T initial_value)
Definition pseudo_moving_average.h:378
void clear(const T initial_value)
Definition pseudo_moving_average.h:308
T value() const
Definition pseudo_moving_average.h:326
add_insert_iterator input()
Definition pseudo_moving_average.h:335
void add(const T new_value)
Definition pseudo_moving_average.h:317
pseudo_moving_average(const T initial_value)
Definition pseudo_moving_average.h:298
void add(T new_value)
Definition pseudo_moving_average.h:151
pseudo_moving_average(const T initial_value)
Definition pseudo_moving_average.h:133
T value() const
Definition pseudo_moving_average.h:162
static ETL_CONSTANT size_t SAMPLE_SIZE
The number of samples averaged over.
Definition pseudo_moving_average.h:126
void clear(const T initial_value)
Definition pseudo_moving_average.h:142
add_insert_iterator input()
Definition pseudo_moving_average.h:171
static ETL_CONSTANT size_t SCALING
The sample scaling factor.
Definition pseudo_moving_average.h:127
Definition pseudo_moving_average.h:99
conditional
Definition type_traits_generator.h:1223
is_floating_point
Definition type_traits_generator.h:1094
bitset_ext
Definition absolute.h:39
iterator
Definition iterator.h:399