1 /* 2 * Copyright (C) 2011 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 #ifndef ANDROID_FILTERFW_CORE_STATISTICS_H 18 #define ANDROID_FILTERFW_CORE_STATISTICS_H 19 20 namespace android { 21 namespace filterfw { 22 23 // An incrementally-constructed Normal distribution. 24 class IncrementalGaussian { 25 public: 26 IncrementalGaussian(); 27 28 void Add(float value); 29 NumSamples()30 float NumSamples() const { return n_; } Mean()31 float Mean() const { return mean_; } Var()32 float Var() const { return var_; } 33 float Std() const; 34 float Pdf(float value) const; 35 36 private: 37 int n_; 38 float sum_x_; 39 float sum_x2_; 40 float mean_; 41 float var_; 42 float exp_denom_; 43 float pdf_denom_; 44 }; 45 46 // Discrete-time implementation of a simple RC low-pass filter: 47 // exponentially-weighted moving average. 48 class RCFilter { 49 public: RCFilter(float gain)50 explicit RCFilter(float gain) 51 : gain_(gain), n_(0), value_(0.0f) {} 52 Add(float measurement)53 void Add(float measurement) { 54 value_ = n_++ ? gain_ * measurement + (1.0f - gain_) * value_ : measurement; 55 } 56 Reset()57 void Reset() { n_ = 0; } 58 NumMeasurements()59 int NumMeasurements() const { return n_; } Output()60 float Output() const { return value_; } 61 62 private: 63 float gain_; 64 int n_; 65 float value_; 66 }; 67 68 } // namespace filterfw 69 } // namespace android 70 71 #endif // ANDROID_FILTERFW_CORE_STATISTICS_H 72