Gustaf Ullberg | 8406c43 | 2018-06-19 12:31:33 +0200 | [diff] [blame] | 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 | namespace webrtc { |
| 18 | namespace aec3 { |
| 19 | |
| 20 | MovingAverage::MovingAverage(size_t num_elem, size_t mem_len) |
| 21 | : num_elem_(num_elem), |
| 22 | mem_len_(mem_len - 1), |
| 23 | scaling_(1.0f / static_cast<float>(mem_len)), |
| 24 | memory_(num_elem * mem_len_, 0.f), |
| 25 | mem_index_(0) { |
| 26 | RTC_DCHECK(num_elem_ > 0); |
| 27 | RTC_DCHECK(mem_len > 0); |
| 28 | } |
| 29 | |
| 30 | MovingAverage::~MovingAverage() = default; |
| 31 | |
| 32 | void MovingAverage::Average(rtc::ArrayView<const float> input, |
| 33 | rtc::ArrayView<float> output) { |
| 34 | RTC_DCHECK(input.size() == num_elem_); |
| 35 | RTC_DCHECK(output.size() == num_elem_); |
| 36 | |
| 37 | // Sum all contributions. |
| 38 | std::copy(input.begin(), input.end(), output.begin()); |
| 39 | for (auto i = memory_.begin(); i < memory_.end(); i += num_elem_) { |
| 40 | std::transform(i, i + num_elem_, output.begin(), output.begin(), |
| 41 | std::plus<float>()); |
| 42 | } |
| 43 | |
| 44 | // Divide by mem_len_. |
| 45 | for (float& o : output) { |
| 46 | o *= scaling_; |
| 47 | } |
| 48 | |
| 49 | // Update memory. |
| 50 | if (mem_len_ > 0) { |
| 51 | std::copy(input.begin(), input.end(), |
| 52 | memory_.begin() + mem_index_ * num_elem_); |
| 53 | mem_index_ = (mem_index_ + 1) % mem_len_; |
| 54 | } |
| 55 | } |
| 56 | |
| 57 | } // namespace aec3 |
| 58 | } // namespace webrtc |