Process consecutive values. Returns the central value of three consecutive values.

Revision:
0:d8d565a9d473
Child:
1:d34bcdec7177
diff -r 000000000000 -r d8d565a9d473 SpikeRemoveFilter.h
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/SpikeRemoveFilter.h	Mon Apr 20 15:28:40 2020 +0000
@@ -0,0 +1,65 @@
+/*
+ * Copyright (c) 2017 Koichi Shibata
+ */
+
+#ifndef SPIKE_REMOVE_FILTER_H
+#define SPIKE_REMOVE_FILTER_H
+
+#include "mbed.h"
+
+template <class T> class SpikeRemoveFilter
+{
+protected:
+
+    T x0; // latest data
+    T x1;
+    T x2; // oldest data
+
+public:
+
+    // Constructor
+    SpikeRemoveFilter( void );
+
+    // Destructor
+    virtual ~SpikeRemoveFilter( void );
+
+    T process( T x );
+
+};
+
+// Constructor
+template <class T> SpikeRemoveFilter<T>::SpikeRemoveFilter( void )
+{
+}
+
+// Destructor
+template <class T> SpikeRemoveFilter<T>::~SpikeRemoveFilter( void )
+{
+}
+
+template <class T> T SpikeRemoveFilter<T>::process( T x )
+{
+    x2 = x1;
+    x1 = x0;
+    x0 = x;
+    if ( x1 < x2 ) {
+        if ( x0 < x1 ) {
+            return x1;
+        } else if ( x2 < x0 ) {
+            return x2;
+        } else {
+            return x0;
+        }
+    } else { // x2 <= x1
+        if ( x0 < x2 ) {
+            return x2;
+        } else if ( x1 < x0 ) {
+            return x1;
+        } else {
+            return x0;
+        }
+    }
+}
+
+#endif
+