OLD | NEW |
---|---|
(Empty) | |
1 // Copyright (c) 2015 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 #ifndef CONTENT_BROWSER_MEDIA_CAPTURE_TIME_WEIGHTED_AVERAGE_H_ | |
6 #define CONTENT_BROWSER_MEDIA_CAPTURE_TIME_WEIGHTED_AVERAGE_H_ | |
7 | |
8 #include "base/time/time.h" | |
9 #include "content/common/content_export.h" | |
10 | |
11 namespace content { | |
12 | |
13 // Utility class for maintaining an time-weighted moving average value of a | |
hubbe
2015/04/27 20:50:42
I think time-weighted might be an accurate but con
miu
2015/05/05 04:56:59
Done.
| |
14 // metric value. | |
hubbe
2015/04/27 20:50:42
It doesn't work with imperial values? :)
miu
2015/05/05 04:56:59
Done.
| |
15 // | |
16 // Usage note: Reset() must be called at least once before the first call to | |
17 // Update(). | |
18 class CONTENT_EXPORT TimeWeightedAverage { | |
19 public: | |
20 // |time_constant| is the amount of time that must pass between two data | |
21 // points to move the average value halfway in-between. Example: If | |
22 // |time_constant| is one second, then calling Reset(0.0, t=0s) and then | |
23 // Update(1.0, t=1s) will result in a moving average of 0.5. | |
24 explicit TimeWeightedAverage(base::TimeDelta time_constant); | |
hubbe
2015/04/27 20:50:42
maybe call time_constant half_life instead?
Althou
miu
2015/05/05 04:56:59
Done. Yeah, half_life is a great name for this!
| |
25 | |
26 // Erase all memory of historical values, re-starting with the given | |
27 // |starting_value|. | |
28 void Reset(double starting_value, base::TimeTicks timestamp); | |
29 base::TimeTicks last_reset_time() const { return last_reset_time_; } | |
30 | |
31 // Update the average using the given |value| which was observed at the given | |
32 // |timestamp|. If the timestamp was no more than one step out-of-order, the | |
hubbe
2015/04/27 20:50:42
Wait, what's a step?
miu
2015/05/05 04:56:59
Done. Explained this further in class-level comme
| |
33 // update succeeds and this method returns true. Otherwise the update has no | |
34 // effect and false is returned. | |
35 // | |
36 // Two or more updates at the same |timestamp| will have their values | |
37 // arithmetically averaged to form a single data point. This single data | |
38 // point is then rolled into the moving average. | |
39 bool Update(double value, base::TimeTicks timestamp); | |
40 | |
41 // Accessors to current state. | |
42 double current() const { return average_; } | |
43 base::TimeTicks latest_timestamp() const { return most_recent_timestamp_; } | |
44 | |
45 // Convenience methods. | |
46 bool was_recently_updated(base::TimeTicks now) const { | |
hubbe
2015/04/27 20:50:42
This seems like an odd function
I think "recent" n
miu
2015/05/05 04:56:59
I got rid of this one. I may or may not introduce
| |
47 return (now - latest_timestamp()) <= time_constant_; | |
48 } | |
49 base::TimeDelta elapsed_since_reset() const { | |
hubbe
2015/04/27 20:50:42
This also seems like an odd function.
From the nam
miu
2015/05/05 04:56:59
Removed this one too. I may or may not introduce
| |
50 return latest_timestamp() - last_reset_time(); | |
51 } | |
52 | |
53 private: | |
54 // Step the moving |average_| backward/forward. | |
55 void StepBackward(double last_value, base::TimeDelta elapsed); | |
56 void StepForward(double next_value, base::TimeDelta elapsed); | |
57 | |
58 // Modify |most_recent_value_| to an unweigthed arithmetic average that | |
59 // includes |value|. | |
60 void MergeWithMostRecentDataPoint(double value); | |
61 | |
62 const base::TimeDelta time_constant_; | |
63 base::TimeTicks last_reset_time_; | |
64 double average_; | |
65 double most_recent_value_; | |
66 int most_recent_count_; | |
67 base::TimeTicks most_recent_timestamp_; | |
68 base::TimeTicks second_most_recent_timestamp_; | |
69 }; | |
70 | |
71 } // namespace content | |
72 | |
73 #endif // CONTENT_BROWSER_MEDIA_CAPTURE_TIME_WEIGHTED_AVERAGE_H_ | |
OLD | NEW |