1 /* 2 * Copyright (c) 2017 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 "modules/audio_processing/echo_detector/moving_max.h" 12 13 #include "rtc_base/checks.h" 14 15 namespace webrtc { 16 namespace { 17 18 // Parameter for controlling how fast the estimated maximum decays after the 19 // previous maximum is no longer valid. With a value of 0.99, the maximum will 20 // decay to 1% of its former value after 460 updates. 21 constexpr float kDecayFactor = 0.99f; 22 23 } // namespace 24 MovingMax(size_t window_size)25MovingMax::MovingMax(size_t window_size) : window_size_(window_size) { 26 RTC_DCHECK_GT(window_size, 0); 27 } 28 ~MovingMax()29MovingMax::~MovingMax() {} 30 Update(float value)31void MovingMax::Update(float value) { 32 if (counter_ >= window_size_ - 1) { 33 max_value_ *= kDecayFactor; 34 } else { 35 ++counter_; 36 } 37 if (value > max_value_) { 38 max_value_ = value; 39 counter_ = 0; 40 } 41 } 42 max() const43float MovingMax::max() const { 44 return max_value_; 45 } 46 Clear()47void MovingMax::Clear() { 48 max_value_ = 0.f; 49 counter_ = 0; 50 } 51 52 } // namespace webrtc 53