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..cb3b28d567726c04cdc577c881c40127d621c83e |
--- /dev/null |
+++ b/webrtc/modules/audio_processing/utility/mean_calculator.cc |
@@ -0,0 +1,64 @@ |
+/* |
+ * Copyright 2015 The WebRTC Project Authors. All rights reserved. |
peah-webrtc
2016/03/14 09:12:53
The year should be 2016
minyue-webrtc
2016/03/14 14:51:42
Yes
|
+ * |
+ * 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), |
+ buffer_(new float[window_length_]), |
+ head_(0), |
+ full_(false), |
+ sum_(0.0f) { |
+} |
+ |
+MeanCalculator::~MeanCalculator() = default; |
peah-webrtc
2016/03/14 09:12:53
Is the destructor needed?
minyue-webrtc
2016/03/14 14:51:42
deleted
peah-webrtc
2016/03/15 09:18:40
No, it is just commented out.
|
+ |
+// Add one sample to the sequence. |
+void MeanCalculator::AddSample(float sample) { |
+ rtc::CritScope cs(&crit_); |
+ if (full_) |
+ sum_ -= buffer_[head_]; |
peah-webrtc
2016/03/14 09:12:53
This is not guaranteed to give the correct sum. Ac
minyue-webrtc
2016/03/14 14:51:42
I was also worried about the numerical accuracy. I
peah-webrtc
2016/03/15 09:18:40
I don't agree that as a mean calculator an error i
peah-webrtc
2016/03/15 09:18:40
The computation of a sliding window arithmetic mea
|
+ sum_ += sample; |
+ buffer_[head_] = sample; |
+ head_ = (head_ + 1) % window_length_; |
+ if (!full_ && head_ == 0) |
+ full_ = true; |
+} |
+ |
+// Get the mean of the latest samples. Returns the mean if it is available, |
+// otherwise null, which happens when the added samples have not fully filled |
+// the window. |
+rtc::Optional<float> MeanCalculator::GetMean() { |
+ rtc::CritScope cs(&crit_); |
+ if (IsWindowFull()) { |
peah-webrtc
2016/03/14 09:12:53
I think it is better to use full_ directly instead
minyue-webrtc
2016/03/14 14:51:42
I agree
peah-webrtc
2016/03/15 09:18:40
Acknowledged.
|
+ return rtc::Optional<float>(sum_ / window_length_); |
+ } else { |
+ return rtc::Optional<float>(); |
+ } |
+} |
+ |
+// Flush all samples added. |
+void MeanCalculator::Flush() { |
+ rtc::CritScope cs(&crit_); |
+ head_ = 0; |
+ full_ = false; |
+ sum_ = 0.0f; |
+} |
+ |
+// Determines if the window is full. This is a quick way of checking if the |
+// mean is ready. |
+bool MeanCalculator::IsWindowFull() { |
+ rtc::CritScope cs(&crit_); |
+ return full_; |
+} |
+ |
+} // namespace webrtc |