1*d9f75844SAndroid Build Coastguard Worker /* 2*d9f75844SAndroid Build Coastguard Worker * Copyright (c) 2018 The WebRTC project authors. All Rights Reserved. 3*d9f75844SAndroid Build Coastguard Worker * 4*d9f75844SAndroid Build Coastguard Worker * Use of this source code is governed by a BSD-style license 5*d9f75844SAndroid Build Coastguard Worker * that can be found in the LICENSE file in the root of the source 6*d9f75844SAndroid Build Coastguard Worker * tree. An additional intellectual property rights grant can be found 7*d9f75844SAndroid Build Coastguard Worker * in the file PATENTS. All contributing project authors may 8*d9f75844SAndroid Build Coastguard Worker * be found in the AUTHORS file in the root of the source tree. 9*d9f75844SAndroid Build Coastguard Worker */ 10*d9f75844SAndroid Build Coastguard Worker 11*d9f75844SAndroid Build Coastguard Worker #ifndef RTC_BASE_NUMERICS_MOVING_AVERAGE_H_ 12*d9f75844SAndroid Build Coastguard Worker #define RTC_BASE_NUMERICS_MOVING_AVERAGE_H_ 13*d9f75844SAndroid Build Coastguard Worker 14*d9f75844SAndroid Build Coastguard Worker #include <stddef.h> 15*d9f75844SAndroid Build Coastguard Worker #include <stdint.h> 16*d9f75844SAndroid Build Coastguard Worker 17*d9f75844SAndroid Build Coastguard Worker #include <vector> 18*d9f75844SAndroid Build Coastguard Worker 19*d9f75844SAndroid Build Coastguard Worker #include "absl/types/optional.h" 20*d9f75844SAndroid Build Coastguard Worker 21*d9f75844SAndroid Build Coastguard Worker namespace rtc { 22*d9f75844SAndroid Build Coastguard Worker 23*d9f75844SAndroid Build Coastguard Worker // Calculates average over fixed size window. If there are less than window 24*d9f75844SAndroid Build Coastguard Worker // size elements, calculates average of all inserted so far elements. 25*d9f75844SAndroid Build Coastguard Worker // 26*d9f75844SAndroid Build Coastguard Worker class MovingAverage { 27*d9f75844SAndroid Build Coastguard Worker public: 28*d9f75844SAndroid Build Coastguard Worker // Maximum supported window size is 2^32 - 1. 29*d9f75844SAndroid Build Coastguard Worker explicit MovingAverage(size_t window_size); 30*d9f75844SAndroid Build Coastguard Worker ~MovingAverage(); 31*d9f75844SAndroid Build Coastguard Worker // MovingAverage is neither copyable nor movable. 32*d9f75844SAndroid Build Coastguard Worker MovingAverage(const MovingAverage&) = delete; 33*d9f75844SAndroid Build Coastguard Worker MovingAverage& operator=(const MovingAverage&) = delete; 34*d9f75844SAndroid Build Coastguard Worker 35*d9f75844SAndroid Build Coastguard Worker // Adds new sample. If the window is full, the oldest element is pushed out. 36*d9f75844SAndroid Build Coastguard Worker void AddSample(int sample); 37*d9f75844SAndroid Build Coastguard Worker 38*d9f75844SAndroid Build Coastguard Worker // Returns rounded down average of last `window_size` elements or all 39*d9f75844SAndroid Build Coastguard Worker // elements if there are not enough of them. Returns nullopt if there were 40*d9f75844SAndroid Build Coastguard Worker // no elements added. 41*d9f75844SAndroid Build Coastguard Worker absl::optional<int> GetAverageRoundedDown() const; 42*d9f75844SAndroid Build Coastguard Worker 43*d9f75844SAndroid Build Coastguard Worker // Same as above but rounded to the closest integer. 44*d9f75844SAndroid Build Coastguard Worker absl::optional<int> GetAverageRoundedToClosest() const; 45*d9f75844SAndroid Build Coastguard Worker 46*d9f75844SAndroid Build Coastguard Worker // Returns unrounded average over the window. 47*d9f75844SAndroid Build Coastguard Worker absl::optional<double> GetUnroundedAverage() const; 48*d9f75844SAndroid Build Coastguard Worker 49*d9f75844SAndroid Build Coastguard Worker // Resets to the initial state before any elements were added. 50*d9f75844SAndroid Build Coastguard Worker void Reset(); 51*d9f75844SAndroid Build Coastguard Worker 52*d9f75844SAndroid Build Coastguard Worker // Returns number of elements in the window. 53*d9f75844SAndroid Build Coastguard Worker size_t Size() const; 54*d9f75844SAndroid Build Coastguard Worker 55*d9f75844SAndroid Build Coastguard Worker private: 56*d9f75844SAndroid Build Coastguard Worker // Total number of samples added to the class since last reset. 57*d9f75844SAndroid Build Coastguard Worker size_t count_ = 0; 58*d9f75844SAndroid Build Coastguard Worker // Sum of the samples in the moving window. 59*d9f75844SAndroid Build Coastguard Worker int64_t sum_ = 0; 60*d9f75844SAndroid Build Coastguard Worker // Circular buffer for all the samples in the moving window. 61*d9f75844SAndroid Build Coastguard Worker // Size is always `window_size` 62*d9f75844SAndroid Build Coastguard Worker std::vector<int> history_; 63*d9f75844SAndroid Build Coastguard Worker }; 64*d9f75844SAndroid Build Coastguard Worker 65*d9f75844SAndroid Build Coastguard Worker } // namespace rtc 66*d9f75844SAndroid Build Coastguard Worker #endif // RTC_BASE_NUMERICS_MOVING_AVERAGE_H_ 67