blob: 22f3e4d4eb7ad2abed4318f08694ea8db40cc00c [file] [log] [blame]
Alexandre Lision8af73cb2013-12-10 14:11:20 -05001/* $Id: math.h 3553 2011-05-05 06:14:19Z nanang $ */
Tristan Matthews0a329cc2013-07-17 13:20:14 -04002/*
3 * Copyright (C) 2008-2011 Teluu Inc. (http://www.teluu.com)
4 * Copyright (C) 2003-2008 Benny Prijono <benny@prijono.org>
5 *
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.
10 *
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.
15 *
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
19 */
20
21#ifndef __PJ_MATH_H__
22#define __PJ_MATH_H__
23
24/**
25 * @file math.h
26 * @brief Mathematics and Statistics.
27 */
28
29#include <pj/string.h>
30#include <pj/compat/high_precision.h>
31
32PJ_BEGIN_DECL
33
34/**
35 * @defgroup pj_math Mathematics and Statistics
36 * @ingroup PJ_MISC
37 * @{
38 *
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
42 * new sample comes).
43 */
44
45/**
46 * Mathematical constants
47 */
48#define PJ_PI 3.14159265358979323846 /* pi */
49#define PJ_1_PI 0.318309886183790671538 /* 1/pi */
50
51/**
52 * Mathematical macro
53 */
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))
57
58/**
59 * This structure describes statistics state.
60 */
61typedef struct pj_math_stat
62{
63 int n; /* number of samples */
64 int max; /* maximum value */
65 int min; /* minimum value */
66 int last; /* last value */
67 int mean; /* mean */
68
69 /* Private members */
70#if PJ_HAS_FLOATING_POINT
71 float fmean_; /* mean(floating point) */
72#else
73 int mean_res_; /* mean residu */
74#endif
75 pj_highprec_t m2_; /* variance * n */
76} pj_math_stat;
77
78/**
79 * Calculate integer square root of an integer.
80 *
81 * @param i Integer to be calculated.
82 *
83 * @return Square root result.
84 */
85PJ_INLINE(unsigned) pj_isqrt(unsigned i)
86{
87 unsigned res = 1, prev;
88
89 /* Rough guess, calculate half bit of input */
90 prev = i >> 2;
91 while (prev) {
92 prev >>= 2;
93 res <<= 1;
94 }
95
96 /* Babilonian method */
97 do {
98 prev = res;
99 res = (prev + i/prev) >> 1;
100 } while ((prev+res)>>1 != res);
101
102 return res;
103}
104
105/**
106 * Initialize statistics state.
107 *
108 * @param stat Statistic state.
109 */
110PJ_INLINE(void) pj_math_stat_init(pj_math_stat *stat)
111{
112 pj_bzero(stat, sizeof(pj_math_stat));
113}
114
115/**
116 * Update statistics state as a new sample comes.
117 *
118 * @param stat Statistic state.
119 * @param val The new sample data.
120 */
121PJ_INLINE(void) pj_math_stat_update(pj_math_stat *stat, int val)
122{
123#if PJ_HAS_FLOATING_POINT
124 float delta;
125#else
126 int delta;
127#endif
128
129 stat->last = val;
130
131 if (stat->n++) {
132 if (stat->min > val)
133 stat->min = val;
134 if (stat->max < val)
135 stat->max = val;
136 } else {
137 stat->min = stat->max = val;
138 }
139
140#if PJ_HAS_FLOATING_POINT
141 delta = val - stat->fmean_;
142 stat->fmean_ += delta/stat->n;
143
144 /* Return mean value with 'rounding' */
145 stat->mean = (int) (stat->fmean_ + 0.5);
146
147 stat->m2_ += (int)(delta * (val-stat->fmean_));
148#else
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) {
153 ++stat->mean;
154 stat->mean_res_ -= stat->n;
155 } else if (stat->mean_res_ <= -stat->n) {
156 --stat->mean;
157 stat->mean_res_ += stat->n;
158 }
159
160 stat->m2_ += delta * (val-stat->mean);
161#endif
162}
163
164/**
165 * Get the standard deviation of specified statistics state.
166 *
167 * @param stat Statistic state.
168 *
169 * @return The standard deviation.
170 */
171PJ_INLINE(unsigned) pj_math_stat_get_stddev(const pj_math_stat *stat)
172{
173 if (stat->n == 0) return 0;
174 return (pj_isqrt((unsigned)(stat->m2_/stat->n)));
175}
176
177/**
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
180 * statistical data.
181 *
182 * @param stat Statistic state.
183 *
184 * @param dev The standard deviation.
185 */
186PJ_INLINE(void) pj_math_stat_set_stddev(pj_math_stat *stat, unsigned dev)
187{
188 if (stat->n == 0)
189 stat->n = 1;
190 stat->m2_ = dev*dev*stat->n;
191}
192
193/** @} */
194
195PJ_END_DECL
196
197#endif /* __PJ_MATH_H__ */