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 time_constant) |
| 12 : time_constant_(time_constant) { |
| 13 DCHECK(time_constant_ > 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 out-of-order, or before last reset. |
| 39 |
| 40 // If |timestamp| is one step out-of-order, step the moving average back to |
| 41 // its prior state, then forward with this update. |
| 42 if (timestamp <= most_recent_timestamp_) { |
| 43 StepBackward(most_recent_value_, |
| 44 most_recent_timestamp_ - second_most_recent_timestamp_); |
| 45 |
| 46 if (timestamp < most_recent_timestamp_) { |
| 47 StepForward(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 second_most_recent_timestamp_ = most_recent_timestamp_; |
| 54 most_recent_value_ = value; |
| 55 most_recent_count_ = 1; |
| 56 most_recent_timestamp_ = timestamp; |
| 57 } |
| 58 |
| 59 // Step the moving average forward using the latest update. |
| 60 StepForward(most_recent_value_, |
| 61 most_recent_timestamp_ - second_most_recent_timestamp_); |
| 62 |
| 63 return true; |
| 64 } |
| 65 |
| 66 void TimeWeightedAverage::StepBackward(double last_value, |
| 67 base::TimeDelta elapsed) { |
| 68 const double elapsed_us = static_cast<double>(elapsed.InMicroseconds()); |
| 69 const double weight = |
| 70 elapsed_us / (elapsed_us + time_constant_.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::StepForward(double next_value, |
| 79 base::TimeDelta elapsed) { |
| 80 const double elapsed_us = static_cast<double>(elapsed.InMicroseconds()); |
| 81 const double weight = |
| 82 elapsed_us / (elapsed_us + time_constant_.InMicroseconds()); |
| 83 average_ = weight * next_value + (1.0 - weight) * average_; |
| 84 DCHECK(std::isfinite(average_)); |
| 85 } |
| 86 |
| 87 void TimeWeightedAverage::MergeWithMostRecentDataPoint(double value) { |
| 88 most_recent_value_ = (most_recent_count_ * most_recent_value_ + value) / |
| 89 (most_recent_count_ + 1); |
| 90 ++most_recent_count_; |
| 91 } |
| 92 |
| 93 } // namespace content |
OLD | NEW |