blob: 192366aef8ea927223864004f038188d58451693 [file] [log] [blame]
pbos@webrtc.orgaf8d5af2013-07-09 08:02:33 +00001/*
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 Bonadei92ea95e2017-09-15 06:47:31 +020010#include "test/statistics.h"
pbos@webrtc.orgaf8d5af2013-07-09 08:02:33 +000011
12#include <math.h>
13
Sergey Silkin3be2a552018-01-17 15:11:44 +010014#include <algorithm>
15
pbos@webrtc.orgaf8d5af2013-07-09 08:02:33 +000016namespace webrtc {
17namespace test {
18
Sergey Silkin3be2a552018-01-17 15:11:44 +010019Statistics::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.orgaf8d5af2013-07-09 08:02:33 +000025
26void Statistics::AddSample(double sample) {
27 sum_ += sample;
28 sum_squared_ += sample * sample;
Sergey Silkin3be2a552018-01-17 15:11:44 +010029 max_ = std::max(max_, sample);
30 min_ = std::min(min_, sample);
pbos@webrtc.orgaf8d5af2013-07-09 08:02:33 +000031 ++count_;
32}
33
Sergey Silkin3be2a552018-01-17 15:11:44 +010034double Statistics::Max() const {
35 return max_;
36}
37
pbos@webrtc.orgaf8d5af2013-07-09 08:02:33 +000038double Statistics::Mean() const {
39 if (count_ == 0)
40 return 0.0;
41 return sum_ / count_;
42}
43
Sergey Silkin3be2a552018-01-17 15:11:44 +010044double Statistics::Min() const {
45 return min_;
46}
47
pbos@webrtc.orgaf8d5af2013-07-09 08:02:33 +000048double Statistics::Variance() const {
49 if (count_ == 0)
50 return 0.0;
51 return sum_squared_ / count_ - Mean() * Mean();
52}
53
54double Statistics::StandardDeviation() const {
55 return sqrt(Variance());
56}
57} // namespace test
58} // namespace webrtc