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

Committer:
Chi7Shi
Date:
Wed May 27 03:36:40 2020 +0000
Revision:
1:d34bcdec7177
Parent:
0:d8d565a9d473
Add function size() that returns the size of buffer.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Chi7Shi 0:d8d565a9d473 1 /*
Chi7Shi 0:d8d565a9d473 2 * Copyright (c) 2017 Koichi Shibata
Chi7Shi 0:d8d565a9d473 3 */
Chi7Shi 0:d8d565a9d473 4
Chi7Shi 0:d8d565a9d473 5 #ifndef SPIKE_REMOVE_FILTER_H
Chi7Shi 0:d8d565a9d473 6 #define SPIKE_REMOVE_FILTER_H
Chi7Shi 0:d8d565a9d473 7
Chi7Shi 0:d8d565a9d473 8 #include "mbed.h"
Chi7Shi 0:d8d565a9d473 9
Chi7Shi 0:d8d565a9d473 10 template <class T> class SpikeRemoveFilter
Chi7Shi 0:d8d565a9d473 11 {
Chi7Shi 0:d8d565a9d473 12 protected:
Chi7Shi 0:d8d565a9d473 13
Chi7Shi 0:d8d565a9d473 14 T x0; // latest data
Chi7Shi 0:d8d565a9d473 15 T x1;
Chi7Shi 0:d8d565a9d473 16 T x2; // oldest data
Chi7Shi 0:d8d565a9d473 17
Chi7Shi 0:d8d565a9d473 18 public:
Chi7Shi 0:d8d565a9d473 19
Chi7Shi 0:d8d565a9d473 20 // Constructor
Chi7Shi 0:d8d565a9d473 21 SpikeRemoveFilter( void );
Chi7Shi 0:d8d565a9d473 22
Chi7Shi 0:d8d565a9d473 23 // Destructor
Chi7Shi 0:d8d565a9d473 24 virtual ~SpikeRemoveFilter( void );
Chi7Shi 0:d8d565a9d473 25
Chi7Shi 0:d8d565a9d473 26 T process( T x );
Chi7Shi 1:d34bcdec7177 27
Chi7Shi 1:d34bcdec7177 28 static int size( void ) { return 3; }
Chi7Shi 0:d8d565a9d473 29
Chi7Shi 0:d8d565a9d473 30 };
Chi7Shi 0:d8d565a9d473 31
Chi7Shi 0:d8d565a9d473 32 // Constructor
Chi7Shi 0:d8d565a9d473 33 template <class T> SpikeRemoveFilter<T>::SpikeRemoveFilter( void )
Chi7Shi 0:d8d565a9d473 34 {
Chi7Shi 0:d8d565a9d473 35 }
Chi7Shi 0:d8d565a9d473 36
Chi7Shi 0:d8d565a9d473 37 // Destructor
Chi7Shi 0:d8d565a9d473 38 template <class T> SpikeRemoveFilter<T>::~SpikeRemoveFilter( void )
Chi7Shi 0:d8d565a9d473 39 {
Chi7Shi 0:d8d565a9d473 40 }
Chi7Shi 0:d8d565a9d473 41
Chi7Shi 0:d8d565a9d473 42 template <class T> T SpikeRemoveFilter<T>::process( T x )
Chi7Shi 0:d8d565a9d473 43 {
Chi7Shi 0:d8d565a9d473 44 x2 = x1;
Chi7Shi 0:d8d565a9d473 45 x1 = x0;
Chi7Shi 0:d8d565a9d473 46 x0 = x;
Chi7Shi 0:d8d565a9d473 47 if ( x1 < x2 ) {
Chi7Shi 0:d8d565a9d473 48 if ( x0 < x1 ) {
Chi7Shi 0:d8d565a9d473 49 return x1;
Chi7Shi 0:d8d565a9d473 50 } else if ( x2 < x0 ) {
Chi7Shi 0:d8d565a9d473 51 return x2;
Chi7Shi 0:d8d565a9d473 52 } else {
Chi7Shi 0:d8d565a9d473 53 return x0;
Chi7Shi 0:d8d565a9d473 54 }
Chi7Shi 0:d8d565a9d473 55 } else { // x2 <= x1
Chi7Shi 0:d8d565a9d473 56 if ( x0 < x2 ) {
Chi7Shi 0:d8d565a9d473 57 return x2;
Chi7Shi 0:d8d565a9d473 58 } else if ( x1 < x0 ) {
Chi7Shi 0:d8d565a9d473 59 return x1;
Chi7Shi 0:d8d565a9d473 60 } else {
Chi7Shi 0:d8d565a9d473 61 return x0;
Chi7Shi 0:d8d565a9d473 62 }
Chi7Shi 0:d8d565a9d473 63 }
Chi7Shi 0:d8d565a9d473 64 }
Chi7Shi 0:d8d565a9d473 65
Chi7Shi 0:d8d565a9d473 66 #endif
Chi7Shi 0:d8d565a9d473 67