histogram.h 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. /*
  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. #ifndef MODULES_AUDIO_CODING_NETEQ_HISTOGRAM_H_
  11. #define MODULES_AUDIO_CODING_NETEQ_HISTOGRAM_H_
  12. #include <string.h> // Provide access to size_t.
  13. #include <vector>
  14. #include "absl/types/optional.h"
  15. namespace webrtc {
  16. class Histogram {
  17. public:
  18. // Creates histogram with capacity |num_buckets| and |forget_factor| in Q15.
  19. Histogram(size_t num_buckets,
  20. int forget_factor,
  21. absl::optional<double> start_forget_weight = absl::nullopt);
  22. virtual ~Histogram();
  23. // Resets the histogram to the default start distribution.
  24. virtual void Reset();
  25. // Add entry in bucket |index|.
  26. virtual void Add(int index);
  27. // Calculates the quantile at |probability| (in Q30) of the histogram
  28. // distribution.
  29. virtual int Quantile(int probability);
  30. // Returns the number of buckets in the histogram.
  31. virtual int NumBuckets() const;
  32. // Returns the probability for each bucket in Q30.
  33. std::vector<int> buckets() const { return buckets_; }
  34. // Accessors only intended for testing purposes.
  35. int base_forget_factor_for_testing() const { return base_forget_factor_; }
  36. int forget_factor_for_testing() const { return forget_factor_; }
  37. absl::optional<double> start_forget_weight_for_testing() const {
  38. return start_forget_weight_;
  39. }
  40. private:
  41. std::vector<int> buckets_;
  42. int forget_factor_; // Q15
  43. const int base_forget_factor_;
  44. int add_count_;
  45. const absl::optional<double> start_forget_weight_;
  46. };
  47. } // namespace webrtc
  48. #endif // MODULES_AUDIO_CODING_NETEQ_HISTOGRAM_H_