Root/
Source at commit ec7cab4cbb721bff91ec924ec691efd8daf36579 created 12 years 8 months ago. By Maarten ter Huurne, MIPS: JZ4740: A320: Updated quickstart documentation. | |
---|---|
1 | /* |
2 | * lib/average.c |
3 | * |
4 | * This source code is licensed under the GNU General Public License, |
5 | * Version 2. See the file COPYING for more details. |
6 | */ |
7 | |
8 | #include <linux/module.h> |
9 | #include <linux/average.h> |
10 | #include <linux/bug.h> |
11 | #include <linux/log2.h> |
12 | |
13 | /** |
14 | * DOC: Exponentially Weighted Moving Average (EWMA) |
15 | * |
16 | * These are generic functions for calculating Exponentially Weighted Moving |
17 | * Averages (EWMA). We keep a structure with the EWMA parameters and a scaled |
18 | * up internal representation of the average value to prevent rounding errors. |
19 | * The factor for scaling up and the exponential weight (or decay rate) have to |
20 | * be specified thru the init fuction. The structure should not be accessed |
21 | * directly but only thru the helper functions. |
22 | */ |
23 | |
24 | /** |
25 | * ewma_init() - Initialize EWMA parameters |
26 | * @avg: Average structure |
27 | * @factor: Factor to use for the scaled up internal value. The maximum value |
28 | * of averages can be ULONG_MAX/(factor*weight). For performance reasons |
29 | * factor has to be a power of 2. |
30 | * @weight: Exponential weight, or decay rate. This defines how fast the |
31 | * influence of older values decreases. For performance reasons weight has |
32 | * to be a power of 2. |
33 | * |
34 | * Initialize the EWMA parameters for a given struct ewma @avg. |
35 | */ |
36 | void ewma_init(struct ewma *avg, unsigned long factor, unsigned long weight) |
37 | { |
38 | WARN_ON(!is_power_of_2(weight) || !is_power_of_2(factor)); |
39 | |
40 | avg->weight = ilog2(weight); |
41 | avg->factor = ilog2(factor); |
42 | avg->internal = 0; |
43 | } |
44 | EXPORT_SYMBOL(ewma_init); |
45 | |
46 | /** |
47 | * ewma_add() - Exponentially weighted moving average (EWMA) |
48 | * @avg: Average structure |
49 | * @val: Current value |
50 | * |
51 | * Add a sample to the average. |
52 | */ |
53 | struct ewma *ewma_add(struct ewma *avg, unsigned long val) |
54 | { |
55 | avg->internal = avg->internal ? |
56 | (((avg->internal << avg->weight) - avg->internal) + |
57 | (val << avg->factor)) >> avg->weight : |
58 | (val << avg->factor); |
59 | return avg; |
60 | } |
61 | EXPORT_SYMBOL(ewma_add); |
62 |
Branches:
ben-wpan
ben-wpan-stefan
javiroman/ks7010
jz-2.6.34
jz-2.6.34-rc5
jz-2.6.34-rc6
jz-2.6.34-rc7
jz-2.6.35
jz-2.6.36
jz-2.6.37
jz-2.6.38
jz-2.6.39
jz-3.0
jz-3.1
jz-3.11
jz-3.12
jz-3.13
jz-3.15
jz-3.16
jz-3.18-dt
jz-3.2
jz-3.3
jz-3.4
jz-3.5
jz-3.6
jz-3.6-rc2-pwm
jz-3.9
jz-3.9-clk
jz-3.9-rc8
jz47xx
jz47xx-2.6.38
master
Tags:
od-2011-09-04
od-2011-09-18
v2.6.34-rc5
v2.6.34-rc6
v2.6.34-rc7
v3.9