Embedded Template Library 1.0
Loading...
Searching...
No Matches
rms.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) 2021 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_RMS_INCLUDED
32#define ETL_RMS_INCLUDED
33
34#include "platform.h"
35#include "functional.h"
36#include "type_traits.h"
37
38#include <math.h>
39#include <stdint.h>
40
41namespace etl
42{
43 namespace private_rms
44 {
45 //***************************************************************************
47 //***************************************************************************
48 template <typename TInput, typename TCalc>
50 {
51 typedef TCalc calc_t;
52 };
53
54 //***************************************************************************
56 //***************************************************************************
57 template <typename TCalc>
59 {
60 typedef float calc_t;
61 };
62
63 //***************************************************************************
65 //***************************************************************************
66 template <typename TCalc>
68 {
69 typedef double calc_t;
70 };
71 }
72
73 //***************************************************************************
75 //***************************************************************************
76 template <typename TInput, typename TCalc = TInput>
77 class rms
78 : public private_rms::rms_traits<TInput, TCalc>
79 , public etl::binary_function<TInput, TInput, void>
80 {
81 private:
82
84
85 public:
86
87 //*********************************
89 //*********************************
91 : recalculate(true)
92 {
93 clear();
94 }
95
96 //*********************************
98 //*********************************
99 template <typename TIterator>
101 : recalculate(true)
102 {
103 clear();
104 add(first, last);
105 }
106
107 //*********************************
109 //*********************************
110 void add(TInput value)
111 {
112 sum_of_squares += TCalc(value * value);
113 ++counter;
114 recalculate = true;
115 }
116
117 //*********************************
119 //*********************************
120 template <typename TIterator>
121 void add(TIterator first, TIterator last)
122 {
123 while (first != last)
124 {
125 add(*first);
126 ++first;
127 }
128 }
129
130 //*********************************
133 //*********************************
134 void operator ()(TInput value)
135 {
136 add(value);
137 }
138
139 //*********************************
142 //*********************************
143 template <typename TIterator>
145 {
146 add(first, last);
147 }
148
149 //*********************************
151 //*********************************
152 double get_rms() const
153 {
154 if (recalculate)
155 {
156 rms_value = 0.0;
157
158 if (counter != 0)
159 {
160 double n = double(counter);
161 double mean_of_squares = sum_of_squares / n;
162
163 if (mean_of_squares > 0)
164 {
165 rms_value = sqrt(mean_of_squares);
166 }
167 }
168
169 recalculate = false;
170 }
171
172 return rms_value;
173 }
174
175 //*********************************
177 //*********************************
178 operator double() const
179 {
180 return get_rms();
181 }
182
183 //*********************************
185 //*********************************
186 size_t count() const
187 {
188 return size_t(counter);
189 }
190
191 //*********************************
193 //*********************************
194 void clear()
195 {
196 sum_of_squares = calc_t(0);
197 counter = 0U;
198 rms_value = 0.0;
199 recalculate = true;
200 }
201
202 private:
203
204 calc_t sum_of_squares;
205 uint32_t counter;
206 mutable double rms_value;
207 mutable bool recalculate;
208 };
209}
210
211#endif
Standard Deviation.
Definition rms.h:80
void add(TInput value)
Add a pair of values.
Definition rms.h:110
void add(TIterator first, TIterator last)
Add a range.
Definition rms.h:121
void operator()(TInput value)
Definition rms.h:134
double get_rms() const
Get the rms.
Definition rms.h:152
rms(TIterator first, TIterator last)
Constructor.
Definition rms.h:100
void clear()
Clear the histogram.
Definition rms.h:194
size_t count() const
Get the total number added entries.
Definition rms.h:186
rms()
Constructor.
Definition rms.h:90
bitset_ext
Definition absolute.h:38
Definition functional.h:126
pair holds two objects of arbitrary type
Definition utility.h:164
Types for generic correlation.
Definition rms.h:50
Calculates the smallest value that, when squared, will be not greater than VALUE.
Definition sqrt.h:47