Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 /* | |
| 2 * 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
| |
| 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 buffer_(new float[window_length_]), | |
| 18 head_(0), | |
| 19 full_(false), | |
| 20 sum_(0.0f) { | |
| 21 } | |
| 22 | |
| 23 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.
| |
| 24 | |
| 25 // Add one sample to the sequence. | |
| 26 void MeanCalculator::AddSample(float sample) { | |
| 27 rtc::CritScope cs(&crit_); | |
| 28 if (full_) | |
| 29 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
| |
| 30 sum_ += sample; | |
| 31 buffer_[head_] = sample; | |
| 32 head_ = (head_ + 1) % window_length_; | |
| 33 if (!full_ && head_ == 0) | |
| 34 full_ = true; | |
| 35 } | |
| 36 | |
| 37 // Get the mean of the latest samples. Returns the mean if it is available, | |
| 38 // otherwise null, which happens when the added samples have not fully filled | |
| 39 // the window. | |
| 40 rtc::Optional<float> MeanCalculator::GetMean() { | |
| 41 rtc::CritScope cs(&crit_); | |
| 42 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.
| |
| 43 return rtc::Optional<float>(sum_ / window_length_); | |
| 44 } else { | |
| 45 return rtc::Optional<float>(); | |
| 46 } | |
| 47 } | |
| 48 | |
| 49 // Flush all samples added. | |
| 50 void MeanCalculator::Flush() { | |
| 51 rtc::CritScope cs(&crit_); | |
| 52 head_ = 0; | |
| 53 full_ = false; | |
| 54 sum_ = 0.0f; | |
| 55 } | |
| 56 | |
| 57 // Determines if the window is full. This is a quick way of checking if the | |
| 58 // mean is ready. | |
| 59 bool MeanCalculator::IsWindowFull() { | |
| 60 rtc::CritScope cs(&crit_); | |
| 61 return full_; | |
| 62 } | |
| 63 | |
| 64 } // namespace webrtc | |
| OLD | NEW |