Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 /* | |
| 2 * Copyright (c) 2016 The WebRTC project authors. All Rights Reserved. | |
| 3 * | |
| 4 * Use of this source code is governed by a BSD-style license | |
| 5 * that can be found in the LICENSE file in the root of the source | |
| 6 * tree. An additional intellectual property rights grant can be found | |
| 7 * in the file PATENTS. All contributing project authors may | |
| 8 * be found in the AUTHORS file in the root of the source tree. | |
| 9 */ | |
| 10 | |
| 11 #include "webrtc/modules/video_coding/utility/moving_average.h" | |
| 12 | |
| 13 #include <algorithm> | |
| 14 | |
| 15 namespace webrtc { | |
| 16 | |
| 17 MovingAverage::MovingAverage(size_t s) : sum_history_(s + 1, 0) {} | |
| 18 | |
| 19 void MovingAverage::AddSample(int sample) { | |
| 20 count_++; | |
| 21 sum_ += sample; | |
| 22 sum_history_[count_ % sum_history_.size()] = sum_; | |
| 23 } | |
| 24 | |
| 25 int MovingAverage::GetAverage() const { | |
| 26 return *GetAverage(size()); | |
| 27 } | |
| 28 | |
| 29 rtc::Optional<int> MovingAverage::GetAverage(size_t num_samples) const { | |
| 30 if (num_samples > size()) | |
| 31 return rtc::Optional<int>(); | |
| 32 if (num_samples == 0) | |
| 33 return rtc::Optional<int>(0); | |
|
magjed_webrtc
2016/09/07 11:55:16
I find it more intuitive if we return rtc::Optiona
| |
| 34 int sum = sum_ - sum_history_[(count_ - num_samples) % sum_history_.size()]; | |
| 35 if (sum == 0) | |
|
sakal
2016/09/07 09:47:21
What is this special case for? Looks like a bug to
kthelgason
2016/09/07 12:31:46
Well spotted. That was left in by accident when I
| |
| 36 sum = sum_; | |
| 37 return rtc::Optional<int>(sum / num_samples); | |
| 38 } | |
| 39 | |
| 40 void MovingAverage::Reset() { | |
| 41 count_ = 0; | |
| 42 sum_ = 0; | |
| 43 std::fill(sum_history_.begin(), sum_history_.end(), 0); | |
| 44 } | |
| 45 | |
| 46 size_t MovingAverage::size() const { | |
| 47 return std::min(count_, sum_history_.size() - 1); | |
| 48 } | |
| 49 | |
| 50 } // namespace webrtc | |
| OLD | NEW |