OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "media/base/moving_average.h" | 5 #include "media/base/moving_average.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 | 8 |
9 namespace media { | 9 namespace media { |
10 | 10 |
11 MovingAverage::MovingAverage(size_t depth) | 11 MovingAverage::MovingAverage(size_t depth) |
12 : depth_(depth), count_(0), samples_(depth_) { | 12 : depth_(depth), count_(0), samples_(depth_), square_sum_us_(0) {} |
13 } | |
14 | 13 |
15 MovingAverage::~MovingAverage() { | 14 MovingAverage::~MovingAverage() { |
16 } | 15 } |
17 | 16 |
18 void MovingAverage::AddSample(base::TimeDelta sample) { | 17 void MovingAverage::AddSample(base::TimeDelta sample) { |
19 // |samples_| is zero-initialized, so |oldest| is also zero before |count_| | 18 // |samples_| is zero-initialized, so |oldest| is also zero before |count_| |
20 // exceeds |depth_|. | 19 // exceeds |depth_|. |
21 base::TimeDelta& oldest = samples_[count_++ % depth_]; | 20 base::TimeDelta& oldest = samples_[count_++ % depth_]; |
22 total_ += sample - oldest; | 21 total_ += sample - oldest; |
| 22 square_sum_us_ += sample.InMicroseconds() * sample.InMicroseconds() - |
| 23 oldest.InMicroseconds() * oldest.InMicroseconds(); |
23 oldest = sample; | 24 oldest = sample; |
24 } | 25 } |
25 | 26 |
26 base::TimeDelta MovingAverage::Average() const { | 27 base::TimeDelta MovingAverage::Average() const { |
27 DCHECK_GT(count_, 0u); | 28 DCHECK_GT(count_, 0u); |
28 | 29 |
29 // TODO(dalecurtis): Consider limiting |depth| to powers of two so that we can | 30 // TODO(dalecurtis): Consider limiting |depth| to powers of two so that we can |
30 // replace the integer divide with a bit shift operation. | 31 // replace the integer divide with a bit shift operation. |
31 | 32 |
32 return total_ / std::min(static_cast<uint64_t>(depth_), count_); | 33 return total_ / std::min(static_cast<uint64_t>(depth_), count_); |
33 } | 34 } |
34 | 35 |
| 36 base::TimeDelta MovingAverage::Deviation() const { |
| 37 DCHECK_GT(count_, 0u); |
| 38 |
| 39 const double size = std::min(static_cast<uint64_t>(depth_), count_); |
| 40 const double average_us = total_.InMicroseconds() / size; |
| 41 double sqr_deviation_us = square_sum_us_ / size - average_us * average_us; |
| 42 if (sqr_deviation_us < 0) |
| 43 sqr_deviation_us = 0; |
| 44 |
| 45 return base::TimeDelta::FromMicroseconds(sqrt(sqr_deviation_us)); |
| 46 } |
| 47 |
35 void MovingAverage::Reset() { | 48 void MovingAverage::Reset() { |
36 count_ = 0; | 49 count_ = 0; |
37 total_ = base::TimeDelta(); | 50 total_ = base::TimeDelta(); |
| 51 square_sum_us_ = 0; |
38 std::fill(samples_.begin(), samples_.end(), base::TimeDelta()); | 52 std::fill(samples_.begin(), samples_.end(), base::TimeDelta()); |
39 } | 53 } |
40 | 54 |
41 } // namespace media | 55 } // namespace media |
OLD | NEW |