1 
2 /*
3  *  Copyright (c) 2018 The WebRTC project authors. All Rights Reserved.
4  *
5  *  Use of this source code is governed by a BSD-style license
6  *  that can be found in the LICENSE file in the root of the source
7  *  tree. An additional intellectual property rights grant can be found
8  *  in the file PATENTS.  All contributing project authors may
9  *  be found in the AUTHORS file in the root of the source tree.
10  */
11 
12 #include "modules/audio_processing/aec3/moving_average.h"
13 
14 #include <algorithm>
15 #include <functional>
16 
17 #include "rtc_base/checks.h"
18 
19 namespace webrtc {
20 namespace aec3 {
21 
MovingAverage(size_t num_elem,size_t mem_len)22 MovingAverage::MovingAverage(size_t num_elem, size_t mem_len)
23     : num_elem_(num_elem),
24       mem_len_(mem_len - 1),
25       scaling_(1.0f / static_cast<float>(mem_len)),
26       memory_(num_elem * mem_len_, 0.f),
27       mem_index_(0) {
28   RTC_DCHECK(num_elem_ > 0);
29   RTC_DCHECK(mem_len > 0);
30 }
31 
32 MovingAverage::~MovingAverage() = default;
33 
Average(rtc::ArrayView<const float> input,rtc::ArrayView<float> output)34 void MovingAverage::Average(rtc::ArrayView<const float> input,
35                             rtc::ArrayView<float> output) {
36   RTC_DCHECK(input.size() == num_elem_);
37   RTC_DCHECK(output.size() == num_elem_);
38 
39   // Sum all contributions.
40   std::copy(input.begin(), input.end(), output.begin());
41   for (auto i = memory_.begin(); i < memory_.end(); i += num_elem_) {
42     std::transform(i, i + num_elem_, output.begin(), output.begin(),
43                    std::plus<float>());
44   }
45 
46   // Divide by mem_len_.
47   for (float& o : output) {
48     o *= scaling_;
49   }
50 
51   // Update memory.
52   if (mem_len_ > 0) {
53     std::copy(input.begin(), input.end(),
54               memory_.begin() + mem_index_ * num_elem_);
55     mem_index_ = (mem_index_ + 1) % mem_len_;
56   }
57 }
58 
59 }  // namespace aec3
60 }  // namespace webrtc
61