1
/* $Id: math.h 3553 2011-05-05 06:14:19Z nanang $ */
3
* Copyright (C) 2008-2011 Teluu Inc. (http://www.teluu.com)
4
* Copyright (C) 2003-2008 Benny Prijono <benny@prijono.org>
6
* This program 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.
11
* This program is distributed in the hope that it will be useful,
12
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
* GNU General Public License for more details.
16
* You should have received a copy of the GNU General Public License
17
* along with this program; if not, write to the Free Software
18
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
26
* @brief Mathematics and Statistics.
29
#include <pj/string.h>
30
#include <pj/compat/high_precision.h>
35
* @defgroup pj_math Mathematics and Statistics
39
* Provides common mathematics constants and operations, and also standard
40
* statistics calculation (min, max, mean, standard deviation). Statistics
41
* calculation is done in realtime (statistics state is updated on time each
46
* Mathematical constants
48
#define PJ_PI 3.14159265358979323846 /* pi */
49
#define PJ_1_PI 0.318309886183790671538 /* 1/pi */
54
#define PJ_ABS(x) ((x) > 0 ? (x) : -(x))
55
#define PJ_MAX(x, y) ((x) > (y)? (x) : (y))
56
#define PJ_MIN(x, y) ((x) < (y)? (x) : (y))
59
* This structure describes statistics state.
61
typedef struct pj_math_stat
63
int n; /* number of samples */
64
int max; /* maximum value */
65
int min; /* minimum value */
66
int last; /* last value */
70
#if PJ_HAS_FLOATING_POINT
71
float fmean_; /* mean(floating point) */
73
int mean_res_; /* mean residu */
75
pj_highprec_t m2_; /* variance * n */
79
* Calculate integer square root of an integer.
81
* @param i Integer to be calculated.
83
* @return Square root result.
85
PJ_INLINE(unsigned) pj_isqrt(unsigned i)
87
unsigned res = 1, prev;
89
/* Rough guess, calculate half bit of input */
96
/* Babilonian method */
99
res = (prev + i/prev) >> 1;
100
} while ((prev+res)>>1 != res);
106
* Initialize statistics state.
108
* @param stat Statistic state.
110
PJ_INLINE(void) pj_math_stat_init(pj_math_stat *stat)
112
pj_bzero(stat, sizeof(pj_math_stat));
116
* Update statistics state as a new sample comes.
118
* @param stat Statistic state.
119
* @param val The new sample data.
121
PJ_INLINE(void) pj_math_stat_update(pj_math_stat *stat, int val)
123
#if PJ_HAS_FLOATING_POINT
137
stat->min = stat->max = val;
140
#if PJ_HAS_FLOATING_POINT
141
delta = val - stat->fmean_;
142
stat->fmean_ += delta/stat->n;
144
/* Return mean value with 'rounding' */
145
stat->mean = (int) (stat->fmean_ + 0.5);
147
stat->m2_ += (int)(delta * (val-stat->fmean_));
149
delta = val - stat->mean;
150
stat->mean += delta/stat->n;
151
stat->mean_res_ += delta % stat->n;
152
if (stat->mean_res_ >= stat->n) {
154
stat->mean_res_ -= stat->n;
155
} else if (stat->mean_res_ <= -stat->n) {
157
stat->mean_res_ += stat->n;
160
stat->m2_ += delta * (val-stat->mean);
165
* Get the standard deviation of specified statistics state.
167
* @param stat Statistic state.
169
* @return The standard deviation.
171
PJ_INLINE(unsigned) pj_math_stat_get_stddev(const pj_math_stat *stat)
173
if (stat->n == 0) return 0;
174
return (pj_isqrt((unsigned)(stat->m2_/stat->n)));
178
* Set the standard deviation of statistics state. This is useful when
179
* the statistic state is operated in 'read-only' mode as a storage of
182
* @param stat Statistic state.
184
* @param dev The standard deviation.
186
PJ_INLINE(void) pj_math_stat_set_stddev(pj_math_stat *stat, unsigned dev)
190
stat->m2_ = dev*dev*stat->n;
197
#endif /* __PJ_MATH_H__ */