Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 /* | |
| 2 * Copyright 2016 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 "webrtc/modules/audio_processing/utility/mean_calculator.h" | |
| 12 | |
| 13 namespace webrtc { | |
| 14 | |
| 15 MeanCalculator::MeanCalculator(size_t window_length) | |
| 16 : window_length_(window_length), | |
| 17 count_(0), | |
| 18 sum_(0.0), | |
| 19 compensation_(0.0), | |
| 20 mean_(0.0) { | |
| 21 } | |
| 22 | |
| 23 void MeanCalculator::Reset() { | |
| 24 Clear(); | |
| 25 mean_ = 0.0; | |
| 26 } | |
| 27 | |
| 28 void MeanCalculator::AddSample(float sample) { | |
| 29 KahanSum(sample); | |
| 30 | |
| 31 // TODO(minyue): For a bug in AEC, the mean is calculated wrong. The following | |
| 32 // 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
| |
| 33 // AEC is updated. | |
| 34 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.
| |
| 35 mean_ = sum_ / window_length_; | |
| 36 Clear(); | |
| 37 } | |
| 38 } | |
| 39 | |
| 40 size_t MeanCalculator::GetNumberNewSamples() const { | |
| 41 return count_; | |
| 42 } | |
| 43 | |
| 44 float MeanCalculator::GetLatestMean() const { | |
| 45 return mean_; | |
| 46 } | |
| 47 | |
| 48 // Flush all samples added. | |
| 49 void MeanCalculator::Clear() { | |
| 50 count_ = 0; | |
| 51 sum_ = 0.0; | |
| 52 compensation_ = 0.0; | |
| 53 } | |
| 54 | |
| 55 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
| |
| 56 const float compensated_sample = sample - compensation_; | |
| 57 const float temp_sum = sum_ + compensated_sample; | |
| 58 compensation_ = (temp_sum - sum_) - compensated_sample; | |
| 59 sum_ = temp_sum; | |
| 60 } | |
| 61 | |
| 62 } // namespace webrtc | |
| OLD | NEW |