Chromium Code Reviews| 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 exponentially-decaying average value of a | |
| 14 // metric whose updates occur at undetermined time intervals and are mostly in | |
| 15 // chronological order. If updates occur more than one stop out-of-sequence, | |
|
hubbe
2015/05/06 17:54:49
"one stop"
How much is that?
Is the "step size" re
miu
2015/05/09 22:08:21
As discussed face-to-face, I removed all the out-o
| |
| 16 // they are ignored. For example, updates for t=1,2,3,4 or t=1,2,4,3 are all | |
| 17 // accounted for in the average as if they had arrived in-order, but the update | |
| 18 // for t=2 in the sequence t=1,3,4,2 will be ignored. | |
| 19 // | |
| 20 // Usage note: Reset() must be called at least once before the first call to | |
| 21 // Update(). | |
| 22 class CONTENT_EXPORT TimeWeightedAverage { | |
| 23 public: | |
| 24 // |half_life| is the amount of time that must pass between two data points to | |
| 25 // move the average value halfway in-between. Example: If |half_life| is one | |
| 26 // second, then calling Reset(0.0, t=0s) and then Update(1.0, t=1s) will | |
| 27 // result in a moving average of 0.5. | |
| 28 explicit TimeWeightedAverage(base::TimeDelta half_life); | |
|
hubbe
2015/05/06 17:54:49
Actually, half_life might not be the right name, b
miu
2015/05/09 22:08:21
IMO, it works really well. It's describing the ha
| |
| 29 | |
| 30 // Erase all memory of historical values, re-starting with the given | |
| 31 // |starting_value|. | |
| 32 void Reset(double starting_value, base::TimeTicks timestamp); | |
| 33 base::TimeTicks last_reset_time() const { return last_reset_time_; } | |
| 34 | |
| 35 // Update the average using the given |value| which was observed at the given | |
| 36 // |timestamp|. If the timestamp is no more than one step out of | |
| 37 // chronological order, the update succeeds and this method returns true. | |
| 38 // Otherwise the update has no effect and false is returned. | |
| 39 // | |
| 40 // Two or more updates at the same |timestamp| will have their values | |
|
hubbe
2015/05/06 17:54:49
This kind of seems like an odd thing to do. Does t
miu
2015/05/09 22:08:21
As discussed face-to-face, I really don't want to
| |
| 41 // arithmetically averaged to form a single data point. This single data | |
| 42 // point is then rolled into the average. | |
| 43 bool Update(double value, base::TimeTicks timestamp); | |
| 44 | |
| 45 // Accessors to current state. | |
| 46 double current() const { return average_; } | |
| 47 base::TimeTicks latest_timestamp() const { return most_recent_timestamp_; } | |
| 48 | |
| 49 private: | |
| 50 // Step the computation of |average_| backward/forward. | |
| 51 void Undo(double last_value, base::TimeDelta elapsed); | |
| 52 void Apply(double next_value, base::TimeDelta elapsed); | |
| 53 | |
| 54 // Modify |most_recent_value_| to an unweigthed arithmetic average that | |
| 55 // includes |value|. | |
| 56 void MergeWithMostRecentDataPoint(double value); | |
| 57 | |
| 58 const base::TimeDelta half_life_; | |
|
hubbe
2015/05/06 17:54:49
These variables needs comments too.
In particular,
miu
2015/05/09 22:08:21
Done.
| |
| 59 base::TimeTicks last_reset_time_; | |
| 60 double average_; | |
| 61 double most_recent_value_; | |
| 62 int most_recent_count_; | |
| 63 base::TimeTicks most_recent_timestamp_; | |
| 64 base::TimeTicks second_most_recent_timestamp_; | |
| 65 }; | |
| 66 | |
| 67 } // namespace content | |
| 68 | |
| 69 #endif // CONTENT_BROWSER_MEDIA_CAPTURE_TIME_WEIGHTED_AVERAGE_H_ | |
| OLD | NEW |