mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Bruno Randolf <br1@einfach.org>
To: linux-kernel@vger.kernel.org
Subject: [PATCH] Add generic exponentially weighted moving average function
Date: Wed, 06 Oct 2010 18:32:25 +0900	[thread overview]
Message-ID: <20101006093225.8739.14012.stgit@tt-desk> (raw)

This adds a generic exponentially weighted moving average function. This
implementation makes use of a structure which keeps a scaled up internal
representation to reduce rounding errors.

The idea for this implementation comes from the rt2x00 driver (rt2x00link.c)
and i would like to use it in several places in the mac80211 and ath5k code.

Signed-off-by: Bruno Randolf <br1@einfach.org>

--
Is this the right place to add it? Who to CC:?
---
 include/linux/average.h |   32 ++++++++++++++++++++++++++++++++
 1 files changed, 32 insertions(+), 0 deletions(-)
 create mode 100644 include/linux/average.h

diff --git a/include/linux/average.h b/include/linux/average.h
new file mode 100644
index 0000000..2a00d3d
--- /dev/null
+++ b/include/linux/average.h
@@ -0,0 +1,32 @@
+#ifndef _LINUX_AVERAGE_H
+#define _LINUX_AVERAGE_H
+
+#define AVG_FACTOR	1000
+
+struct avg_val {
+	int value;
+	int internal;
+};
+
+/**
+ * moving_average -  Exponentially weighted moving average
+ * @avg: average structure
+ * @val: current value
+ * @samples: number of samples
+ *
+ * This implementation make use of a struct avg_val to prevent rounding
+ * errors.
+ */
+static inline struct avg_val
+moving_average(const struct avg_val avg, const int val, const int samples)
+{
+	struct avg_val ret;
+	ret.internal = avg.internal  ?
+		(((avg.internal * (samples - 1)) +
+			(val * AVG_FACTOR)) / samples) :
+		(val * AVG_FACTOR);
+	ret.value = ret.internal / AVG_FACTOR;
+	return ret;
+}
+
+#endif /* _LINUX_AVERAGE_H */


             reply	other threads:[~2010-10-06  9:39 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2010-10-06  9:32 Bruno Randolf [this message]
2010-10-13  0:33 ` Randy Dunlap
2010-10-13  2:10   ` Bruno Randolf
2010-10-13 16:33     ` Randy Dunlap
2010-10-15  3:40   ` Ben Pfaff
2010-10-15 14:41     ` Randy Dunlap
2010-10-13 14:01 ` kevin granade
2010-10-14  1:19   ` Bruno Randolf
2010-10-15 13:55     ` kevin granade
2010-10-18  3:27       ` Bruno Randolf

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20101006093225.8739.14012.stgit@tt-desk \
    --to=br1@einfach.org \
    --cc=linux-kernel@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

Powered by JetHome