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