Chromium Code Reviews| Index: webrtc/modules/audio_processing/utility/mean_calculator.cc |
| diff --git a/webrtc/modules/audio_processing/utility/mean_calculator.cc b/webrtc/modules/audio_processing/utility/mean_calculator.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..ddc12df7e93d3a303a91e2a68040a7d9b5a0ff84 |
| --- /dev/null |
| +++ b/webrtc/modules/audio_processing/utility/mean_calculator.cc |
| @@ -0,0 +1,62 @@ |
| +/* |
| + * Copyright 2016 The WebRTC Project Authors. All rights reserved. |
| + * |
| + * Use of this source code is governed by a BSD-style license |
| + * that can be found in the LICENSE file in the root of the source |
| + * tree. An additional intellectual property rights grant can be found |
| + * in the file PATENTS. All contributing project authors may |
| + * be found in the AUTHORS file in the root of the source tree. |
| + */ |
| + |
| +#include "webrtc/modules/audio_processing/utility/mean_calculator.h" |
| + |
| +namespace webrtc { |
| + |
| +MeanCalculator::MeanCalculator(size_t window_length) |
| + : window_length_(window_length), |
| + count_(0), |
| + sum_(0.0), |
| + compensation_(0.0), |
| + mean_(0.0) { |
| +} |
| + |
| +void MeanCalculator::Reset() { |
| + Clear(); |
| + mean_ = 0.0; |
| +} |
| + |
| +void MeanCalculator::AddSample(float sample) { |
| + KahanSum(sample); |
| + |
| + // TODO(minyue): For a bug in AEC, the mean is calculated wrong. The following |
| + // line should become "++count_ == window_length_" when bit exactness test in |
|
peah-webrtc
2016/03/18 09:18:34
What do you mean by this todo? As far as I can see
minyue-webrtc
2016/03/21 15:27:51
Sorry. I forgot to remove this when I came up with
|
| + // AEC is updated. |
| + if (++count_ == window_length_) { |
|
peah-webrtc
2016/03/18 09:18:34
Please move the increement outside of the if-state
minyue-webrtc
2016/03/21 15:27:51
Done.
|
| + mean_ = sum_ / window_length_; |
| + Clear(); |
| + } |
| +} |
| + |
| +size_t MeanCalculator::GetNumberNewSamples() const { |
| + return count_; |
| +} |
| + |
| +float MeanCalculator::GetLatestMean() const { |
| + return mean_; |
| +} |
| + |
| +// Flush all samples added. |
| +void MeanCalculator::Clear() { |
| + count_ = 0; |
| + sum_ = 0.0; |
| + compensation_ = 0.0; |
| +} |
| + |
| +void MeanCalculator::KahanSum(float sample) { |
|
peah-webrtc
2016/03/18 09:18:34
I don't think we need to compute the sum using Kah
minyue-webrtc
2016/03/21 15:27:51
KahanSum is better in accuracy in any long-window
|
| + const float compensated_sample = sample - compensation_; |
| + const float temp_sum = sum_ + compensated_sample; |
| + compensation_ = (temp_sum - sum_) - compensated_sample; |
| + sum_ = temp_sum; |
| +} |
| + |
| +} // namespace webrtc |