blob: 5b2f2b11ca1b416c013da84dbf217b516669a86b [file] [log] [blame]
Jakob Ivarsson1eb3d7e2019-02-21 15:42:31 +01001/*
2 * Copyright (c) 2019 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 MODULES_AUDIO_CODING_NETEQ_HISTOGRAM_H_
12#define MODULES_AUDIO_CODING_NETEQ_HISTOGRAM_H_
13
14#include <string.h> // Provide access to size_t.
15
16#include <vector>
17
Jakob Ivarssone9a2ee22019-05-22 16:54:09 +020018#include "absl/types/optional.h"
19
Jakob Ivarsson1eb3d7e2019-02-21 15:42:31 +010020namespace webrtc {
21
22class Histogram {
23 public:
Artem Titovd00ce742021-07-28 20:00:17 +020024 // Creates histogram with capacity `num_buckets` and `forget_factor` in Q15.
Jakob Ivarssone9a2ee22019-05-22 16:54:09 +020025 Histogram(size_t num_buckets,
26 int forget_factor,
27 absl::optional<double> start_forget_weight = absl::nullopt);
Jakob Ivarsson1eb3d7e2019-02-21 15:42:31 +010028
29 virtual ~Histogram();
30
31 // Resets the histogram to the default start distribution.
32 virtual void Reset();
33
Artem Titovd00ce742021-07-28 20:00:17 +020034 // Add entry in bucket `index`.
Jakob Ivarsson1eb3d7e2019-02-21 15:42:31 +010035 virtual void Add(int index);
36
Artem Titovd00ce742021-07-28 20:00:17 +020037 // Calculates the quantile at `probability` (in Q30) of the histogram
Jakob Ivarsson1eb3d7e2019-02-21 15:42:31 +010038 // distribution.
39 virtual int Quantile(int probability);
40
Jakob Ivarsson1eb3d7e2019-02-21 15:42:31 +010041 // Returns the number of buckets in the histogram.
42 virtual int NumBuckets() const;
43
44 // Returns the probability for each bucket in Q30.
45 std::vector<int> buckets() const { return buckets_; }
46
Jakob Ivarssone9a2ee22019-05-22 16:54:09 +020047 // Accessors only intended for testing purposes.
48 int base_forget_factor_for_testing() const { return base_forget_factor_; }
49 int forget_factor_for_testing() const { return forget_factor_; }
50 absl::optional<double> start_forget_weight_for_testing() const {
51 return start_forget_weight_;
52 }
53
Jakob Ivarsson1eb3d7e2019-02-21 15:42:31 +010054 private:
55 std::vector<int> buckets_;
56 int forget_factor_; // Q15
57 const int base_forget_factor_;
Jakob Ivarssone9a2ee22019-05-22 16:54:09 +020058 int add_count_;
59 const absl::optional<double> start_forget_weight_;
Jakob Ivarsson1eb3d7e2019-02-21 15:42:31 +010060};
61
62} // namespace webrtc
63
64#endif // MODULES_AUDIO_CODING_NETEQ_HISTOGRAM_H_