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 "iterator.h"
36#include "type_traits.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 } // namespace private_pseudo_moving_average
87
88 //***************************************************************************
93 //***************************************************************************
94 template <typename T, const size_t SAMPLE_SIZE, const size_t SCALING = 1U, const bool IsIntegral = etl::is_integral<T>::value,
95 const bool IsFloat = etl::is_floating_point<T>::value>
97
98 //***************************************************************************
104 //***************************************************************************
105 template <typename T, const size_t SAMPLE_SIZE_, const size_t SCALING_>
106 class pseudo_moving_average<T, SAMPLE_SIZE_, SCALING_, true, false>
107 {
108 private:
109
111
112 typedef typename etl::conditional<etl::is_signed<T>::value, int32_t, uint32_t>::type scale_t;
113 typedef typename etl::conditional<etl::is_signed<T>::value, int32_t, uint32_t>::type sample_t;
114
115 static ETL_CONSTANT sample_t SAMPLES = static_cast<sample_t>(SAMPLE_SIZE_);
116 static ETL_CONSTANT scale_t SCALE = static_cast<scale_t>(SCALING_);
117
118 public:
119
120 typedef T value_type;
122
123 static ETL_CONSTANT size_t SAMPLE_SIZE = SAMPLE_SIZE_;
124 static ETL_CONSTANT size_t SCALING = SCALING_;
125
126 //*************************************************************************
129 //*************************************************************************
130 pseudo_moving_average(const T initial_value)
131 : average(initial_value * SCALE)
132 {
133 }
134
135 //*************************************************************************
138 //*************************************************************************
139 void clear(const T initial_value)
140 {
141 average = (initial_value * SCALE);
142 }
143
144 //*************************************************************************
147 //*************************************************************************
148 void add(T new_value)
149 {
150 average *= SAMPLES;
151 average += SCALE * new_value;
152 average /= SAMPLES + sample_t(1);
153 }
154
155 //*************************************************************************
158 //*************************************************************************
159 T value() const
160 {
161 return average;
162 }
163
164 //*************************************************************************
167 //*************************************************************************
168 add_insert_iterator input()
169 {
170 return add_insert_iterator(*this);
171 }
172
173 private:
174
175 T average;
176 };
177
178 template <typename T, const size_t SAMPLE_SIZE_, const size_t SCALING_>
180
181 template <typename T, const size_t SAMPLE_SIZE_, const size_t SCALING_>
183
184 //***************************************************************************
189 //***************************************************************************
190 template <typename T, const size_t SCALING_>
191 class pseudo_moving_average<T, 0, SCALING_, true, false>
192 {
194
195 typedef typename etl::conditional<etl::is_signed<T>::value, int32_t, uint32_t>::type scale_t;
196 typedef typename etl::conditional<etl::is_signed<T>::value, int32_t, uint32_t>::type sample_t;
197
198 static ETL_CONSTANT scale_t SCALE = static_cast<scale_t>(SCALING_);
199
200 public:
201
202 typedef T value_type;
204
205 static ETL_CONSTANT size_t SCALING = SCALING_;
206
207 //*************************************************************************
210 //*************************************************************************
211 pseudo_moving_average(const T initial_value, const size_t sample_size)
212 : average(initial_value * SCALE)
213 , samples(sample_t(sample_size))
214 {
215 }
216
217 //*************************************************************************
220 //*************************************************************************
221 void clear(const T initial_value)
222 {
223 average = (initial_value * SCALE);
224 }
225
226 //*************************************************************************
229 //*************************************************************************
230 void set_sample_size(const size_t sample_size)
231 {
232 samples = sample_t(sample_size);
233 }
234
235 //*************************************************************************
238 //*************************************************************************
239 void add(T new_value)
240 {
241 average *= samples;
242 average += SCALE * new_value;
243 average /= samples + sample_t(1);
244 }
245
246 //*************************************************************************
249 //*************************************************************************
250 T value() const
251 {
252 return average;
253 }
254
255 //*************************************************************************
258 //*************************************************************************
259 add_insert_iterator input()
260 {
261 return add_insert_iterator(*this);
262 }
263
264 private:
265
266 T average;
267 sample_t samples;
268 };
269
270 template <typename T, const size_t SCALING_>
272
273 //***************************************************************************
278 //***************************************************************************
279 template <typename T, const size_t SAMPLE_SIZE_>
280 class pseudo_moving_average<T, SAMPLE_SIZE_, 1U, false, true>
281 {
283
284 public:
285
286 typedef T value_type;
288
289 static ETL_CONSTANT size_t SAMPLE_SIZE = SAMPLE_SIZE_;
290
291 //*************************************************************************
294 //*************************************************************************
295 pseudo_moving_average(const T initial_value)
296 : reciprocal_samples_plus_1(T(1.0) / T(SAMPLE_SIZE_ + 1U))
297 , average(initial_value)
298 {
299 }
300
301 //*************************************************************************
304 //*************************************************************************
305 void clear(const T initial_value)
306 {
307 average = initial_value;
308 }
309
310 //*************************************************************************
313 //*************************************************************************
314 void add(const T new_value)
315 {
316 average += (new_value - average) * reciprocal_samples_plus_1;
317 }
318
319 //*************************************************************************
322 //*************************************************************************
323 T value() const
324 {
325 return average;
326 }
327
328 //*************************************************************************
331 //*************************************************************************
332 add_insert_iterator input()
333 {
334 return add_insert_iterator(*this);
335 }
336
337 private:
338
339 const T reciprocal_samples_plus_1;
341 T average;
342 };
343
344 template <typename T, const size_t SAMPLE_SIZE_>
345 ETL_CONSTANT size_t pseudo_moving_average<T, SAMPLE_SIZE_, 1U, false, true>::SAMPLE_SIZE;
346
347 //***************************************************************************
351 //***************************************************************************
352 template <typename T>
353 class pseudo_moving_average<T, 0U, 1U, false, true>
354 {
356
357 public:
358
359 typedef T value_type;
361
362 //*************************************************************************
365 //*************************************************************************
366 pseudo_moving_average(const T initial_value, const size_t sample_size)
367 : reciprocal_samples_plus_1(T(1.0) / T(sample_size + 1U))
368 , average(initial_value)
369 {
370 }
371
372 //*************************************************************************
375 //*************************************************************************
376 void clear(const T initial_value)
377 {
378 average = initial_value;
379 }
380
381 //*************************************************************************
384 //*************************************************************************
385 void set_sample_size(const size_t sample_size)
386 {
387 reciprocal_samples_plus_1 = T(1.0) / (T(sample_size) + T(1));
388 }
389
390 //*************************************************************************
393 //*************************************************************************
394 void add(const T new_value)
395 {
396 average += (new_value - average) * reciprocal_samples_plus_1;
397 }
398
399 //*************************************************************************
402 //*************************************************************************
403 T value() const
404 {
405 return average;
406 }
407
408 //*************************************************************************
411 //*************************************************************************
412 add_insert_iterator input()
413 {
414 return add_insert_iterator(*this);
415 }
416
417 private:
418
419 T reciprocal_samples_plus_1;
421 T average;
422 };
423} // namespace etl
424
425#endif
Definition pseudo_moving_average.h:48
pseudo_moving_average(const T initial_value, const size_t sample_size)
Definition pseudo_moving_average.h:211
void clear(const T initial_value)
Definition pseudo_moving_average.h:221
void add(T new_value)
Definition pseudo_moving_average.h:239
T value() const
Definition pseudo_moving_average.h:250
void set_sample_size(const size_t sample_size)
Definition pseudo_moving_average.h:230
add_insert_iterator input()
Definition pseudo_moving_average.h:259
static ETL_CONSTANT size_t SCALING
The sample scaling factor.
Definition pseudo_moving_average.h:205
add_insert_iterator input()
Definition pseudo_moving_average.h:412
void set_sample_size(const size_t sample_size)
Definition pseudo_moving_average.h:385
pseudo_moving_average(const T initial_value, const size_t sample_size)
Definition pseudo_moving_average.h:366
T value() const
Definition pseudo_moving_average.h:403
void add(const T new_value)
Definition pseudo_moving_average.h:394
void clear(const T initial_value)
Definition pseudo_moving_average.h:376
void clear(const T initial_value)
Definition pseudo_moving_average.h:305
T value() const
Definition pseudo_moving_average.h:323
add_insert_iterator input()
Definition pseudo_moving_average.h:332
void add(const T new_value)
Definition pseudo_moving_average.h:314
pseudo_moving_average(const T initial_value)
Definition pseudo_moving_average.h:295
void add(T new_value)
Definition pseudo_moving_average.h:148
pseudo_moving_average(const T initial_value)
Definition pseudo_moving_average.h:130
T value() const
Definition pseudo_moving_average.h:159
static ETL_CONSTANT size_t SAMPLE_SIZE
The number of samples averaged over.
Definition pseudo_moving_average.h:123
void clear(const T initial_value)
Definition pseudo_moving_average.h:139
add_insert_iterator input()
Definition pseudo_moving_average.h:168
static ETL_CONSTANT size_t SCALING
The sample scaling factor.
Definition pseudo_moving_average.h:124
Definition pseudo_moving_average.h:96
bitset_ext
Definition absolute.h:40
iterator
Definition iterator.h:424