1 /*
2  * Copyright (C) 2002-2003 Fhg Fokus
3  *
4  * This file is part of SEMS, a free SIP media server.
5  *
6  * SEMS is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version. This program is released under
10  * the GPL with the additional exemption that compiling, linking,
11  * and/or using OpenSSL is allowed.
12  *
13  * For a license to use the SEMS software under conditions
14  * other than those described here, or to purchase support for this
15  * software, please contact iptel.org by e-mail at the following addresses:
16  *    info@iptel.org
17  *
18  * SEMS is distributed in the hope that it will be useful,
19  * but WITHOUT ANY WARRANTY; without even the implied warranty of
20  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21  * GNU General Public License for more details.
22  *
23  * You should have received a copy of the GNU General Public License
24  * along with this program; if not, write to the Free Software
25  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
26  */
27 /** @file AmStats.h */
28 #ifndef _AmStats_h_
29 #define _AmStats_h_
30 
31 #include <sys/types.h>
32 #include <math.h>
33 #include <string.h>
34 /**
35  * \brief math mean implementation
36  *
37  * The mean of all previously stored values is calculated (no reset).
38  */
39 class MeanValue
40 {
41  protected:
42   double cum_val;
43   size_t n_val;
44 
45  public:
MeanValue()46   MeanValue()
47     : cum_val(0.0),
48     n_val(0)
49     {}
50 
~MeanValue()51   virtual ~MeanValue() { }
52 
push(double val)53   void push(double val){
54     cum_val += val;
55     n_val++;
56   }
57 
mean()58   double mean(){
59     if(!n_val) return 0.0;
60     return cum_val / float(n_val);
61   }
62 };
63 
64 /**
65  * \brief math stddev implementation
66  *
67  * The standard deviation of previously stored
68  * values is calculated.
69  */
70 class StddevValue
71 {
72  protected:
73   double cum_val;
74   double sq_cum_val;
75   size_t n_val;
76 
77  public:
StddevValue()78   StddevValue()
79     : cum_val(0.0),
80     sq_cum_val(0.0),
81     n_val(0)
82     {}
83 
push(double val)84   void push(double val){
85 
86     cum_val += val;
87     sq_cum_val += val*val;
88     n_val++;
89   }
90 
stddev()91   double stddev(){
92     if(!n_val) return 0.0;
93     return sqrt((n_val*sq_cum_val - cum_val*cum_val)/(n_val*(n_val-1)));
94   }
95 };
96 
97 /**
98  * \brief math mean implementation (n values)
99  *
100  * The mean of n previously stored values is calculated
101  */
102 class MeanArray: public MeanValue
103 {
104   double *buffer;
105   size_t  buf_size;
106 
107  public:
MeanArray(size_t size)108   MeanArray(size_t size)
109     : MeanValue(),
110     buf_size(size)
111     {
112       buffer = new double[size];
113       memset(buffer, 0, sizeof(buffer[0])*size);
114     }
115 
~MeanArray()116   ~MeanArray(){
117     delete [] buffer;
118   }
119 
push(double val)120   void push(double val){
121 
122     cum_val -= buffer[n_val % buf_size];
123     buffer[n_val % buf_size] = val;
124 
125     cum_val += val;
126     n_val++;
127   }
128 
mean()129   double mean(){
130     if(!n_val) return 0.0;
131     return cum_val / double(n_val > buf_size ? buf_size : n_val);
132   }
133 };
134 
135 #endif
136