1 /*
2  *  Copyright (c) 2013 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 "modules/audio_processing/transient/moving_moments.h"
12 
13 #include <algorithm>
14 
15 #include "rtc_base/checks.h"
16 
17 namespace webrtc {
18 
MovingMoments(size_t length)19 MovingMoments::MovingMoments(size_t length)
20     : length_(length), queue_(), sum_(0.0), sum_of_squares_(0.0) {
21   RTC_DCHECK_GT(length, 0);
22   for (size_t i = 0; i < length; ++i) {
23     queue_.push(0.0);
24   }
25 }
26 
~MovingMoments()27 MovingMoments::~MovingMoments() {}
28 
CalculateMoments(const float * in,size_t in_length,float * first,float * second)29 void MovingMoments::CalculateMoments(const float* in,
30                                      size_t in_length,
31                                      float* first,
32                                      float* second) {
33   RTC_DCHECK(in);
34   RTC_DCHECK_GT(in_length, 0);
35   RTC_DCHECK(first);
36   RTC_DCHECK(second);
37 
38   for (size_t i = 0; i < in_length; ++i) {
39     const float old_value = queue_.front();
40     queue_.pop();
41     queue_.push(in[i]);
42 
43     sum_ += in[i] - old_value;
44     sum_of_squares_ += in[i] * in[i] - old_value * old_value;
45     first[i] = sum_ / length_;
46     second[i] = std::max(0.f, sum_of_squares_ / length_);
47   }
48 }
49 
50 }  // namespace webrtc
51