Open Model Railroad Network (OpenMRN)
Loading...
Searching...
No Matches
Ewma.hxx
Go to the documentation of this file.
1
35#ifndef _UTILS_EWMA_HXX_
36#define _UTILS_EWMA_HXX_
37
38#include <time.h>
39#include <stdint.h>
40
56class Ewma
57{
58public:
63 Ewma(float alpha = 0.8)
64 : alpha_(alpha)
65 {
66 }
67
71 void add_absolute(uint32_t offset)
72 {
73 add_diff(offset - lastOffset_);
74 lastOffset_ = offset;
75 }
76
80 void add_diff(uint32_t bytes)
81 {
82 long long t = current_time();
84 {
85 float spd = bytes * 1e9 / (t - lastMeasurementTimeNsec_);
86 if (avg_)
87 {
88 avg_ = avg_ * alpha_ + spd * (1 - alpha_);
89 }
90 else
91 {
92 avg_ = spd;
93 }
94 }
96 }
97
99 float avg()
100 {
101 return avg_;
102 }
103
104private:
106 long long current_time()
107 {
108 long long t;
109#ifdef __linux__
110 struct timespec ts;
111 clock_gettime(CLOCK_REALTIME, &ts);
112 t = ts.tv_sec * 1000000000;
113 t += ts.tv_nsec;
114#else
116#endif
117 return t;
118 }
119
120 float alpha_;
121 float avg_{0.0};
125 uint32_t lastOffset_{0};
126};
127
137{
138public:
142 AbsEwma(float alpha)
143 : alpha_(alpha)
144 {
145 }
146
149 void set_alpha(float alpha)
150 {
151 alpha_ = alpha;
152 }
153
156 void add_value(float value)
157 {
158 if (avg_)
159 {
160 avg_ = avg_ * alpha_ + value * (1 - alpha_);
161 }
162 else
163 {
164 avg_ = value;
165 }
166 }
167
170 void reset_state(float value)
171 {
172 avg_ = value;
173 }
174
176 float avg()
177 {
178 return avg_;
179 }
180
181 float alpha_;
182 float avg_{0.0};
183};
184
185#endif // _UTILS_EWMA_HXX_
Exponentially weighted moving average.
Definition Ewma.hxx:137
void set_alpha(float alpha)
Sets the adjustment parameter.
Definition Ewma.hxx:149
float avg_
current state of EWMA
Definition Ewma.hxx:182
AbsEwma(float alpha)
Definition Ewma.hxx:142
float alpha_
coefficient for EWMA
Definition Ewma.hxx:181
void add_value(float value)
Adds a step to the EWMA average.
Definition Ewma.hxx:156
void reset_state(float value)
Clears the history and sets the state of the EWMA.
Definition Ewma.hxx:170
float avg()
Definition Ewma.hxx:176
Exponentially weighted moving average.
Definition Ewma.hxx:57
long long current_time()
Helper function to get the current time.
Definition Ewma.hxx:106
void add_diff(uint32_t bytes)
Notifies the average algorithm that since the last call ‘bytes’ additional bytes were transferred.
Definition Ewma.hxx:80
float alpha_
coefficient for EWMA
Definition Ewma.hxx:120
float avg()
Definition Ewma.hxx:99
uint32_t lastOffset_
What was the progress offset at the time of the last measurement taken.
Definition Ewma.hxx:125
float avg_
current state of EWMA
Definition Ewma.hxx:121
Ewma(float alpha=0.8)
Constructor.
Definition Ewma.hxx:63
void add_absolute(uint32_t offset)
Sets the absolute value where the transfer is.
Definition Ewma.hxx:71
long long lastMeasurementTimeNsec_
When did we take the last measurement.
Definition Ewma.hxx:123
long long os_get_time_monotonic(void)
Get the monotonic time since the system started.
Definition os.c:571