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 "type_traits.h"
35#include "iterator.h"
36
37namespace etl
38{
39 namespace private_pseudo_moving_average
40 {
41 //***************************************************
44 //***************************************************
45 template <typename TPseudo_Moving_Average>
46 class add_insert_iterator : public etl::iterator<ETL_OR_STD::output_iterator_tag, typename TPseudo_Moving_Average::value_type, void, void, void>
47 {
48 public:
49
50 //***********************************
51 explicit add_insert_iterator(TPseudo_Moving_Average& pma) ETL_NOEXCEPT
52 : p_pma(&pma)
53 {
54 }
55
56 //***********************************
57 add_insert_iterator& operator*() ETL_NOEXCEPT
58 {
59 return *this;
60 }
61
62 //***********************************
63 add_insert_iterator& operator++() ETL_NOEXCEPT
64 {
65 return *this;
66 }
67
68 //***********************************
69 add_insert_iterator& operator++(int) ETL_NOEXCEPT
70 {
71 return *this;
72 }
73
74 //***********************************
75 add_insert_iterator& operator =(typename TPseudo_Moving_Average::value_type value)
76 {
77 p_pma->add(value);
78 return *this;
79 }
80
81 private:
82
84 };
85 }
86
87 //***************************************************************************
92 //***************************************************************************
93 template <typename T,
94 const size_t SAMPLE_SIZE,
95 const size_t SCALING = 1U,
99
100 //***************************************************************************
106 //***************************************************************************
107 template <typename T, const size_t SAMPLE_SIZE_, const size_t SCALING_>
109 {
110 private:
111
113
114 typedef typename etl::conditional<etl::is_signed<T>::value, int32_t, uint32_t>::type scale_t;
115 typedef typename etl::conditional<etl::is_signed<T>::value, int32_t, uint32_t>::type sample_t;
116
117 static ETL_CONSTANT sample_t SAMPLES = static_cast<sample_t>(SAMPLE_SIZE_);
118 static ETL_CONSTANT scale_t SCALE = static_cast<scale_t>(SCALING_);
119
120 public:
121
122 typedef T value_type;
124
125 static ETL_CONSTANT size_t SAMPLE_SIZE = SAMPLE_SIZE_;
126 static ETL_CONSTANT size_t SCALING = SCALING_;
127
128 //*************************************************************************
131 //*************************************************************************
133 : average(initial_value * SCALE)
134 {
135 }
136
137 //*************************************************************************
140 //*************************************************************************
142 {
143 average = (initial_value * SCALE);
144 }
145
146 //*************************************************************************
149 //*************************************************************************
151 {
152 average *= SAMPLES;
153 average += SCALE * new_value;
154 average /= SAMPLES + sample_t(1);
155 }
156
157 //*************************************************************************
160 //*************************************************************************
161 T value() const
162 {
163 return average;
164 }
165
166 //*************************************************************************
169 //*************************************************************************
171 {
172 return add_insert_iterator(*this);
173 }
174
175 private:
176
177 T average;
178 };
179
180 template <typename T, const size_t SAMPLE_SIZE_, const size_t SCALING_>
181 ETL_CONSTANT size_t pseudo_moving_average<T, SAMPLE_SIZE_, SCALING_, true, false>::SAMPLE_SIZE;
182
183 template <typename T, const size_t SAMPLE_SIZE_, const size_t SCALING_>
184 ETL_CONSTANT size_t pseudo_moving_average<T, SAMPLE_SIZE_, SCALING_, true, false>::SCALING;
185
186 //***************************************************************************
191//***************************************************************************
192 template <typename T, const size_t SCALING_>
193 class pseudo_moving_average<T, 0, SCALING_, true, false>
194 {
196
197 typedef typename etl::conditional<etl::is_signed<T>::value, int32_t, uint32_t>::type scale_t;
199
200 static ETL_CONSTANT scale_t SCALE = static_cast<scale_t>(SCALING_);
201
202 public:
203
204 typedef T value_type;
206
207 static ETL_CONSTANT size_t SCALING = SCALING_;
208
209 //*************************************************************************
212 //*************************************************************************
214 : average(initial_value * SCALE)
215 , samples(sample_t(sample_size))
216 {
217 }
218
219 //*************************************************************************
222 //*************************************************************************
224 {
225 average = (initial_value * SCALE);
226 }
227
228 //*************************************************************************
231 //*************************************************************************
232 void set_sample_size(const size_t sample_size)
233 {
234 samples = sample_t(sample_size);
235 }
236
237 //*************************************************************************
240 //*************************************************************************
242 {
243 average *= samples;
244 average += SCALE * new_value;
245 average /= samples + sample_t(1);
246 }
247
248 //*************************************************************************
251 //*************************************************************************
252 T value() const
253 {
254 return average;
255 }
256
257 //*************************************************************************
260 //*************************************************************************
262 {
263 return add_insert_iterator(*this);
264 }
265
266 private:
267
268 T average;
269 sample_t samples;
270 };
271
272 template <typename T, const size_t SCALING_>
273 ETL_CONSTANT size_t pseudo_moving_average<T, 0, SCALING_, true, false>::SCALING;
274
275 //***************************************************************************
280 //***************************************************************************
281 template <typename T, const size_t SAMPLE_SIZE_>
282 class pseudo_moving_average<T, SAMPLE_SIZE_, 1U, false, true>
283 {
285
286 public:
287
288 typedef T value_type;
290
291 static ETL_CONSTANT size_t SAMPLE_SIZE = SAMPLE_SIZE_;
292
293 //*************************************************************************
296 //*************************************************************************
298 : reciprocal_samples_plus_1(T(1.0) / T(SAMPLE_SIZE_ + 1U))
299 , average(initial_value)
300 {
301 }
302
303 //*************************************************************************
306 //*************************************************************************
308 {
309 average = initial_value;
310 }
311
312 //*************************************************************************
315 //*************************************************************************
316 void add(const T new_value)
317 {
318 average += (new_value - average) * reciprocal_samples_plus_1;
319 }
320
321 //*************************************************************************
324 //*************************************************************************
325 T value() const
326 {
327 return average;
328 }
329
330 //*************************************************************************
333 //*************************************************************************
335 {
336 return add_insert_iterator(*this);
337 }
338
339 private:
340
341 const T reciprocal_samples_plus_1;
342 T average;
343 };
344
345 template <typename T, const size_t SAMPLE_SIZE_>
346 ETL_CONSTANT size_t pseudo_moving_average<T, SAMPLE_SIZE_, 1U, false, true>::SAMPLE_SIZE;
347
348 //***************************************************************************
352 //***************************************************************************
353 template <typename T>
354 class pseudo_moving_average<T, 0U, 1U, false, true>
355 {
357
358 public:
359
360 typedef T value_type;
362
363 //*************************************************************************
366 //*************************************************************************
368 : reciprocal_samples_plus_1(T(1.0) / T(sample_size + 1U))
369 , average(initial_value)
370 {
371 }
372
373 //*************************************************************************
376 //*************************************************************************
378 {
379 average = initial_value;
380 }
381
382 //*************************************************************************
385 //*************************************************************************
386 void set_sample_size(const size_t sample_size)
387 {
388 reciprocal_samples_plus_1 = T(1.0) / (T(sample_size) + T(1));
389 }
390
391 //*************************************************************************
394 //*************************************************************************
395 void add(const T new_value)
396 {
397 average += (new_value - average) * reciprocal_samples_plus_1;
398 }
399
400 //*************************************************************************
403 //*************************************************************************
404 T value() const
405 {
406 return average;
407 }
408
409 //*************************************************************************
412 //*************************************************************************
414 {
415 return add_insert_iterator(*this);
416 }
417
418 private:
419
420 T reciprocal_samples_plus_1;
421 T average;
422 };
423}
424
425#endif
Definition pseudo_moving_average.h:47
Definition pseudo_moving_average.h:194
pseudo_moving_average(const T initial_value, const size_t sample_size)
Definition pseudo_moving_average.h:213
void clear(const T initial_value)
Definition pseudo_moving_average.h:223
void add(T new_value)
Definition pseudo_moving_average.h:241
T value() const
Definition pseudo_moving_average.h:252
void set_sample_size(const size_t sample_size)
Definition pseudo_moving_average.h:232
add_insert_iterator input()
Definition pseudo_moving_average.h:261
Definition pseudo_moving_average.h:355
add_insert_iterator input()
Definition pseudo_moving_average.h:413
void set_sample_size(const size_t sample_size)
Definition pseudo_moving_average.h:386
pseudo_moving_average(const T initial_value, const size_t sample_size)
Definition pseudo_moving_average.h:367
T value() const
Definition pseudo_moving_average.h:404
void add(const T new_value)
Definition pseudo_moving_average.h:395
void clear(const T initial_value)
Definition pseudo_moving_average.h:377
void clear(const T initial_value)
Definition pseudo_moving_average.h:307
T value() const
Definition pseudo_moving_average.h:325
add_insert_iterator input()
Definition pseudo_moving_average.h:334
void add(const T new_value)
Definition pseudo_moving_average.h:316
pseudo_moving_average(const T initial_value)
Definition pseudo_moving_average.h:297
void add(T new_value)
Definition pseudo_moving_average.h:150
pseudo_moving_average(const T initial_value)
Definition pseudo_moving_average.h:132
T value() const
Definition pseudo_moving_average.h:161
void clear(const T initial_value)
Definition pseudo_moving_average.h:141
add_insert_iterator input()
Definition pseudo_moving_average.h:170
Definition pseudo_moving_average.h:98
is_floating_point
Definition type_traits_generator.h:1026
is_integral
Definition type_traits_generator.h:996
bitset_ext
Definition absolute.h:38
iterator
Definition iterator.h:399
pair holds two objects of arbitrary type
Definition utility.h:164