tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

moving_average.cc (1749B)


      1 /*
      2 *  Copyright (c) 2018 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/aec3/moving_average.h"
     12 
     13 #include <algorithm>
     14 #include <cstddef>
     15 #include <functional>
     16 
     17 #include "api/array_view.h"
     18 #include "rtc_base/checks.h"
     19 
     20 namespace webrtc {
     21 namespace aec3 {
     22 
     23 MovingAverage::MovingAverage(size_t num_elem, size_t mem_len)
     24    : num_elem_(num_elem),
     25      mem_len_(mem_len - 1),
     26      scaling_(1.0f / static_cast<float>(mem_len)),
     27      memory_(num_elem * mem_len_, 0.f),
     28      mem_index_(0) {
     29  RTC_DCHECK(num_elem_ > 0);
     30  RTC_DCHECK(mem_len > 0);
     31 }
     32 
     33 MovingAverage::~MovingAverage() = default;
     34 
     35 void MovingAverage::Average(ArrayView<const float> input,
     36                            ArrayView<float> output) {
     37  RTC_DCHECK(input.size() == num_elem_);
     38  RTC_DCHECK(output.size() == num_elem_);
     39 
     40  // Sum all contributions.
     41  std::copy(input.begin(), input.end(), output.begin());
     42  for (auto i = memory_.begin(); i < memory_.end(); i += num_elem_) {
     43    std::transform(i, i + num_elem_, output.begin(), output.begin(),
     44                   std::plus<float>());
     45  }
     46 
     47  // Divide by mem_len_.
     48  for (float& o : output) {
     49    o *= scaling_;
     50  }
     51 
     52  // Update memory.
     53  if (mem_len_ > 0) {
     54    std::copy(input.begin(), input.end(),
     55              memory_.begin() + mem_index_ * num_elem_);
     56    mem_index_ = (mem_index_ + 1) % mem_len_;
     57  }
     58 }
     59 
     60 }  // namespace aec3
     61 }  // namespace webrtc