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 #include "content/browser/media/capture/time_weighted_average.h" | |
6 | |
7 #include <cmath> | |
8 | |
9 namespace content { | |
10 | |
11 TimeWeightedAverage::TimeWeightedAverage(base::TimeDelta half_life) | |
12 : half_life_(half_life) { | |
13 DCHECK(half_life_ > base::TimeDelta()); | |
14 } | |
15 | |
16 void TimeWeightedAverage::Reset(double starting_value, | |
17 base::TimeTicks timestamp) { | |
18 DCHECK(!timestamp.is_null()); | |
19 last_reset_time_ = timestamp; | |
20 average_ = starting_value; | |
21 most_recent_value_ = starting_value; | |
22 most_recent_count_ = 1; | |
23 most_recent_timestamp_ = timestamp; | |
24 second_most_recent_timestamp_ = timestamp; | |
25 } | |
26 | |
27 bool TimeWeightedAverage::Update(double value, base::TimeTicks timestamp) { | |
28 DCHECK(!last_reset_time_.is_null()); | |
29 | |
30 // Edge case: Multiple updates at reset timestamp. | |
31 if (timestamp == last_reset_time_) { | |
32 MergeWithMostRecentDataPoint(value); | |
33 average_ = most_recent_value_; | |
34 return true; | |
35 } | |
36 | |
37 if (timestamp <= second_most_recent_timestamp_) | |
38 return false; // Timestamp is too far out-of-order, or before last reset. | |
39 | |
40 if (timestamp <= most_recent_timestamp_) { | |
41 // |timestamp| is one step out-of-order: Undo the most recent computation | |
42 // and then apply the updates in-order. | |
43 Undo(most_recent_value_, | |
hubbe
2015/05/06 17:54:49
May I suggest that it might be easier to keep the
miu
2015/05/09 22:08:21
As discussed face-to-face, I removed all the out-o
| |
44 most_recent_timestamp_ - second_most_recent_timestamp_); | |
45 | |
46 if (timestamp < most_recent_timestamp_) { | |
47 Apply(value, timestamp - second_most_recent_timestamp_); | |
48 second_most_recent_timestamp_ = timestamp; | |
49 } else /* if (timestamp == most_recent_timestamp_) */ { | |
50 MergeWithMostRecentDataPoint(value); | |
51 } | |
52 } else { | |
53 // Typical case: |timestamp| is in-order, so the most-recent data point is | |
54 // set to the current update value and timestamp. | |
55 second_most_recent_timestamp_ = most_recent_timestamp_; | |
56 most_recent_value_ = value; | |
57 most_recent_count_ = 1; | |
58 most_recent_timestamp_ = timestamp; | |
59 } | |
60 | |
61 // Apply the update with the latest timestamp. | |
62 Apply(most_recent_value_, | |
63 most_recent_timestamp_ - second_most_recent_timestamp_); | |
64 | |
65 return true; | |
66 } | |
67 | |
68 void TimeWeightedAverage::Undo(double last_value, base::TimeDelta elapsed) { | |
69 const double elapsed_us = static_cast<double>(elapsed.InMicroseconds()); | |
70 const double weight = elapsed_us / (elapsed_us + half_life_.InMicroseconds()); | |
71 DCHECK_GE(weight, 0.0); | |
72 DCHECK_LT(weight, 1.0); | |
73 average_ -= weight * last_value; | |
74 average_ /= 1.0 - weight; | |
75 DCHECK(std::isfinite(average_)); | |
76 } | |
77 | |
78 void TimeWeightedAverage::Apply(double next_value, base::TimeDelta elapsed) { | |
79 const double elapsed_us = static_cast<double>(elapsed.InMicroseconds()); | |
80 const double weight = elapsed_us / (elapsed_us + half_life_.InMicroseconds()); | |
81 average_ = weight * next_value + (1.0 - weight) * average_; | |
82 DCHECK(std::isfinite(average_)); | |
83 } | |
84 | |
85 void TimeWeightedAverage::MergeWithMostRecentDataPoint(double value) { | |
86 most_recent_value_ = (most_recent_count_ * most_recent_value_ + value) / | |
87 (most_recent_count_ + 1); | |
88 ++most_recent_count_; | |
89 } | |
90 | |
91 } // namespace content | |
OLD | NEW |