blob: dfaf6ce81b87d868e0a03dec7a0985ceb0b53585 [file] [log] [blame]
Ilya Nikolaevskiy26341992018-11-05 12:55:18 +01001/*
2 * Copyright (c) 2018 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#ifndef RTC_BASE_NUMERICS_MOVING_AVERAGE_H_
12#define RTC_BASE_NUMERICS_MOVING_AVERAGE_H_
13
Yves Gerey3e707812018-11-28 16:47:49 +010014#include <stddef.h>
15#include <stdint.h>
Jonas Olssona4d87372019-07-05 19:08:33 +020016
Ilya Nikolaevskiy26341992018-11-05 12:55:18 +010017#include <vector>
18
19#include "absl/types/optional.h"
20
21namespace rtc {
22
23// Calculates average over fixed size window. If there are less than window
24// size elements, calculates average of all inserted so far elements.
25//
26class MovingAverage {
27 public:
28 // Maximum supported window size is 2^32 - 1.
29 explicit MovingAverage(size_t window_size);
30 ~MovingAverage();
31 // MovingAverage is neither copyable nor movable.
32 MovingAverage(const MovingAverage&) = delete;
33 MovingAverage& operator=(const MovingAverage&) = delete;
34
35 // Adds new sample. If the window is full, the oldest element is pushed out.
36 void AddSample(int sample);
37
38 // Returns rounded down average of last |window_size| elements or all
39 // elements if there are not enough of them. Returns nullopt if there were
40 // no elements added.
41 absl::optional<int> GetAverageRoundedDown() const;
42
43 // Same as above but rounded to the closest integer.
44 absl::optional<int> GetAverageRoundedToClosest() const;
45
46 // Returns unrounded average over the window.
47 absl::optional<double> GetUnroundedAverage() const;
48
49 // Resets to the initial state before any elements were added.
50 void Reset();
51
52 // Returns number of elements in the window.
53 size_t Size() const;
54
55 private:
56 // Total number of samples added to the class since last reset.
57 size_t count_ = 0;
58 // Sum of the samples in the moving window.
59 int64_t sum_ = 0;
60 // Circular buffer for all the samples in the moving window.
61 // Size is always |window_size|
62 std::vector<int> history_;
63};
64
65} // namespace rtc
66#endif // RTC_BASE_NUMERICS_MOVING_AVERAGE_H_