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 #include "core/statistics.h" 18 19 #include <math.h> 20 21 namespace android { 22 namespace filterfw { 23 24 IncrementalGaussian::IncrementalGaussian() 25 : n_(0), 26 sum_x_(0.0f), 27 sum_x2_(0.0f), 28 mean_(0.0f), 29 var_(0.0f), 30 exp_denom_(0.0f), 31 pdf_denom_(0.0f) { 32 } 33 34 void IncrementalGaussian::Add(float value) { 35 ++n_; 36 sum_x_ += value; 37 sum_x2_ += value * value; 38 39 mean_ = sum_x_ / n_; 40 var_ = sum_x2_ / n_ - mean_ * mean_; 41 42 exp_denom_ = 2.0f * var_; 43 pdf_denom_ = sqrtf(M_PI * exp_denom_); 44 } 45 46 float IncrementalGaussian::Std() const { 47 return sqrtf(var_); 48 } 49 50 float IncrementalGaussian::Pdf(float value) const { 51 if (var_ == 0.0f) { return n_ > 0 ? value == mean_ : 0.0f; } 52 const float diff = value - mean_; 53 return expf(-diff * diff / exp_denom_) / pdf_denom_; 54 } 55 56 } // namespace filterfw 57 } // namespace android 58