MovingAverage template

Dependents:   trms_helloworld

Fork of MoyenneMobile by Alexandre Proulx

Files at this revision

API Documentation at this revision

Comitter:
Alegrowin
Date:
Sun Apr 14 01:02:39 2013 +0000
Child:
1:b310d132db09
Commit message:
Initial Commit; ; This library purpose is to provide a tool to filter data from a source that is not precise enough,; ; Read more here:http://en.wikipedia.org/wiki/Moving_average

Changed in this revision

MovingAverage.cpp Show annotated file Show diff for this revision Revisions of this file
MovingAverage.h Show annotated file Show diff for this revision Revisions of this file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/MovingAverage.cpp	Sun Apr 14 01:02:39 2013 +0000
@@ -0,0 +1,51 @@
+/*****************************************************/
+/*           _                              _        */
+/*     /\   | |                            (_)       */
+/*    /  \  | | ___  __ _ _ __ _____      ___ _ __   */
+/*   / /\ \ | |/ _ \/ _` | '__/ _ \ \ /\ / / | '_ \  */
+/*  / ____ \| |  __/ (_| | | | (_) \ V  V /| | | | | */
+/* /_/    \_\_|\___|\__, |_|  \___/ \_/\_/ |_|_| |_| */
+/*                   __/ |                           */
+/*                  |___/                            */
+/*                          Created on April 2013    */
+/*****************************************************/
+
+
+/*
+
+for(int i = 0; i < FILTRE_RANGE; i++) {
+        FiltreMobile[i] = cmd[0];
+    }
+    
+    Temp = Temp - FiltreMobile[Temp_next]/FILTRE_RANGE + cmd[0]/FILTRE_RANGE;
+    FiltreMobile[Temp_next++] = cmd[0];
+
+    Temp_next = Temp_next&0x07;
+}*/
+#include "MovingAverage.h"
+
+template<class T>
+MovingAverage<T>::MovingAverage(unsigned char maxLength, T defaultValue){
+    MaxLength = maxLength;
+    
+    AverageFilter = new T[MaxLength];
+    
+    for(int i = 0; i<MaxLength;i++)
+    {
+        AverageFilter[i] = defaultValue;
+    }
+    Average = defaultValue;
+}
+
+template<class T>
+T MovingAverage<T>::GetAverage(){
+    return Average;
+}
+template<class T>
+void MovingAverage<T>::Insert(T value){
+    
+    Average = Average - AverageFilter[NextElement]/MaxLength + value/MaxLength;
+    FiltreMobile[NextElement++] = value;
+
+    NextElement = NextElement&MaxLength;
+}
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/MovingAverage.h	Sun Apr 14 01:02:39 2013 +0000
@@ -0,0 +1,19 @@
+#ifndef MOVING_AVERAGE_H
+#define MOVING_AVERAGE_H
+
+template <class T>
+class MovingAverage
+{
+private:
+    T AverageFilter[];
+    T Average;
+    
+    unsigned char NextElement;
+    unsigned char MaxLength;
+public:
+    MovingAverage(unsigned char maxLength, T defaultValue);
+    T GetAverage();
+    void Insert(T value);
+};
+
+#endif
\ No newline at end of file