Embedded Template Library
1.0
Toggle main menu visibility
Loading...
Searching...
No Matches
rms.h
Go to the documentation of this file.
1
2
3
/******************************************************************************
4
The MIT License(MIT)
5
6
Embedded Template Library.
7
https://github.com/ETLCPP/etl
8
https://www.etlcpp.com
9
10
Copyright(c) 2021 John Wellbelove
11
12
Permission is hereby granted, free of charge, to any person obtaining a copy
13
of this software and associated documentation files(the "Software"), to deal
14
in the Software without restriction, including without limitation the rights
15
to use, copy, modify, merge, publish, distribute, sublicense, and / or sell
16
copies of the Software, and to permit persons to whom the Software is
17
furnished to do so, subject to the following conditions :
18
19
The above copyright notice and this permission notice shall be included in all
20
copies or substantial portions of the Software.
21
22
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE
25
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
26
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
27
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
28
SOFTWARE.
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
41
namespace
etl
42
{
43
namespace
private_rms
44
{
45
//***************************************************************************
47
//***************************************************************************
48
template
<
typename
TInput,
typename
TCalc>
49
struct
rms_traits
50
{
51
typedef
TCalc calc_t;
52
};
53
54
//***************************************************************************
56
//***************************************************************************
57
template
<
typename
TCalc>
58
struct
rms_traits
<float, TCalc>
59
{
60
typedef
float
calc_t;
61
};
62
63
//***************************************************************************
65
//***************************************************************************
66
template
<
typename
TCalc>
67
struct
rms_traits
<double, TCalc>
68
{
69
typedef
double
calc_t;
70
};
71
}
// namespace private_rms
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
83
typedef
typename
private_rms::rms_traits<TInput, TCalc>::calc_t calc_t;
84
85
public
:
86
87
//*********************************
89
//*********************************
90
rms
()
91
: recalculate(true)
92
{
93
clear
();
94
}
95
96
//*********************************
98
//*********************************
99
template
<
typename
TIterator>
100
rms
(TIterator first, TIterator last)
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>
144
void
operator()
(TIterator first, TIterator last)
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
}
// namespace etl
210
211
#endif
etl::rms::add
void add(TInput value)
Add a pair of values.
Definition
rms.h:110
etl::rms::add
void add(TIterator first, TIterator last)
Add a range.
Definition
rms.h:121
etl::rms::operator()
void operator()(TInput value)
Definition
rms.h:134
etl::rms::get_rms
double get_rms() const
Get the rms.
Definition
rms.h:152
etl::rms::operator()
void operator()(TIterator first, TIterator last)
Definition
rms.h:144
etl::rms::rms
rms(TIterator first, TIterator last)
Constructor.
Definition
rms.h:100
etl::rms::clear
void clear()
Clear the histogram.
Definition
rms.h:194
etl::rms::count
size_t count() const
Get the total number added entries.
Definition
rms.h:186
etl::rms::rms
rms()
Constructor.
Definition
rms.h:90
functional.h
math.h
etl
Definition
absolute.h:40
platform.h
etl::binary_function
binary_function
Definition
functional.h:192
etl::private_rms::rms_traits
Types for generic correlation.
Definition
rms.h:50
etl::sqrt
Definition
sqrt.h:48
type_traits.h
include
etl
rms.h
Generated on
for Embedded Template Library by
1.17.0