pbos@webrtc.org | af8d5af | 2013-07-09 08:02:33 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2013 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 | */ |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 10 | #include "test/statistics.h" |
pbos@webrtc.org | af8d5af | 2013-07-09 08:02:33 +0000 | [diff] [blame] | 11 | |
| 12 | #include <math.h> |
| 13 | |
Sergey Silkin | 3be2a55 | 2018-01-17 15:11:44 +0100 | [diff] [blame] | 14 | #include <algorithm> |
| 15 | |
pbos@webrtc.org | af8d5af | 2013-07-09 08:02:33 +0000 | [diff] [blame] | 16 | namespace webrtc { |
| 17 | namespace test { |
| 18 | |
Sergey Silkin | 3be2a55 | 2018-01-17 15:11:44 +0100 | [diff] [blame] | 19 | Statistics::Statistics() |
| 20 | : sum_(0.0), |
| 21 | sum_squared_(0.0), |
| 22 | max_(std::numeric_limits<double>::min()), |
| 23 | min_(std::numeric_limits<double>::max()), |
| 24 | count_(0) {} |
pbos@webrtc.org | af8d5af | 2013-07-09 08:02:33 +0000 | [diff] [blame] | 25 | |
| 26 | void Statistics::AddSample(double sample) { |
| 27 | sum_ += sample; |
| 28 | sum_squared_ += sample * sample; |
Sergey Silkin | 3be2a55 | 2018-01-17 15:11:44 +0100 | [diff] [blame] | 29 | max_ = std::max(max_, sample); |
| 30 | min_ = std::min(min_, sample); |
pbos@webrtc.org | af8d5af | 2013-07-09 08:02:33 +0000 | [diff] [blame] | 31 | ++count_; |
| 32 | } |
| 33 | |
Sergey Silkin | 3be2a55 | 2018-01-17 15:11:44 +0100 | [diff] [blame] | 34 | double Statistics::Max() const { |
| 35 | return max_; |
| 36 | } |
| 37 | |
pbos@webrtc.org | af8d5af | 2013-07-09 08:02:33 +0000 | [diff] [blame] | 38 | double Statistics::Mean() const { |
| 39 | if (count_ == 0) |
| 40 | return 0.0; |
| 41 | return sum_ / count_; |
| 42 | } |
| 43 | |
Sergey Silkin | 3be2a55 | 2018-01-17 15:11:44 +0100 | [diff] [blame] | 44 | double Statistics::Min() const { |
| 45 | return min_; |
| 46 | } |
| 47 | |
pbos@webrtc.org | af8d5af | 2013-07-09 08:02:33 +0000 | [diff] [blame] | 48 | double Statistics::Variance() const { |
| 49 | if (count_ == 0) |
| 50 | return 0.0; |
| 51 | return sum_squared_ / count_ - Mean() * Mean(); |
| 52 | } |
| 53 | |
| 54 | double Statistics::StandardDeviation() const { |
| 55 | return sqrt(Variance()); |
| 56 | } |
| 57 | } // namespace test |
| 58 | } // namespace webrtc |