Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(15)

Side by Side Diff: webrtc/modules/congestion_controller/theil_sen_estimator.cc

Issue 2512693002: Implement Theil-Sen's method for fitting a line to noisy data (used in bandwidth estimation). (Closed)
Patch Set: Review comments Created 4 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 /*
2 * Copyright (c) 2016 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 #include "webrtc/modules/congestion_controller/theil_sen_estimator.h"
12
13 #include <algorithm>
14 #include <vector>
15
16 #include "webrtc/base/logging.h"
17 #include "webrtc/modules/remote_bitrate_estimator/include/bwe_defines.h"
18 #include "webrtc/modules/remote_bitrate_estimator/test/bwe_test_logging.h"
19
20 namespace webrtc {
21
22 constexpr unsigned int kDeltaCounterMax = 1000;
23
24 TheilSenEstimator::TheilSenEstimator(size_t window_size, double threshold_gain)
25 : window_size_(window_size),
26 threshold_gain_(threshold_gain),
27 num_of_deltas_(0),
28 accumulated_delay_(0),
29 delay_hist_(),
30 median_filter_(0.5),
31 trendline_(0) {}
32
33 TheilSenEstimator::~TheilSenEstimator() {}
34
35 void TheilSenEstimator::Update(double recv_delta_ms,
36 double send_delta_ms,
37 double now_ms) {
38 const double delta_ms = recv_delta_ms - send_delta_ms;
39 ++num_of_deltas_;
40 if (num_of_deltas_ > kDeltaCounterMax) {
41 num_of_deltas_ = kDeltaCounterMax;
42 }
43
44 accumulated_delay_ += delta_ms;
45 BWE_TEST_LOGGING_PLOT(1, "accumulated_delay_ms", now_ms, accumulated_delay_);
46
47 // If the window is full, remove the |window_size_| - 1 slopes that belong to
48 // the oldest point.
49 if (delay_hist_.size() == window_size_) {
50 for (double slope : delay_hist_.front().slopes) {
51 if (!median_filter_.Erase(slope)) {
52 LOG(LS_ERROR) << "Failed to erase previously inserted slope. This is "
53 "a bug, likely related to rounding.";
brandtr 2016/12/05 12:34:45 Remove or update "likely related to rounding."
54 RTC_NOTREACHED();
55 }
56 }
57 delay_hist_.pop_front();
58 }
59 // Add |window_size_| - 1 new slopes.
60 for (auto& old_delay : delay_hist_) {
61 if (now_ms - old_delay.time != 0) {
62 double slope =
63 (accumulated_delay_ - old_delay.delay) / (now_ms - old_delay.time);
64 median_filter_.Insert(slope);
65 old_delay.slopes.push_back(slope);
66 }
67 }
68 delay_hist_.emplace_back(now_ms, accumulated_delay_, window_size_ - 1);
69 // Recompute the median slope.
70 if (delay_hist_.size() == window_size_)
71 trendline_ = median_filter_.GetPercentileValue();
72
73 BWE_TEST_LOGGING_PLOT(1, "trendline_slope", now_ms, trendline_);
74 }
75
76 } // namespace webrtc
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698