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_ms_(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_ms_ += sample.InMillisecondsF() * sample.InMillisecondsF() - | |
DaleCurtis
2015/11/20 23:50:46
Why work in float here? Keep these as timedelta*ti
qiangchen
2015/11/25 18:06:33
I do not think there is a multiplication defined i
DaleCurtis
2015/11/25 21:29:24
They are defined, which is why I suggested it :) I
qiangchen
2015/12/01 19:37:53
There is multiplication defined between numeric (i
| |
23 oldest.InMillisecondsF() * oldest.InMillisecondsF(); | |
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 int size = std::min(static_cast<uint64_t>(depth_), count_); | |
40 const double average_ms = Average().InMillisecondsF(); | |
41 double sqr_deviation_ms = square_sum_ms_ / size - average_ms * average_ms; | |
DaleCurtis
2015/11/20 23:50:46
cast size to double and keep the rest as TimeDelta
qiangchen
2015/11/25 18:06:33
Perhaps, we can use int64 to store the square_sum_
DaleCurtis
2015/11/25 21:29:23
sgtm
qiangchen
2015/12/01 19:37:53
Done.
| |
42 if (sqr_deviation_ms < 0) | |
43 sqr_deviation_ms = 0; | |
44 | |
45 return base::TimeDelta::FromMillisecondsD(sqrt(sqr_deviation_ms)); | |
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_ms_ = 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 |