From 85f26ba6ce85355454f115989f3e1dcd05c41688 Mon Sep 17 00:00:00 2001 From: Rafael Bachmann Date: Wed, 4 Jan 2017 22:18:50 +0100 Subject: [PATCH 1/5] fix minor typos --- extras/Filters/MAF.h | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/extras/Filters/MAF.h b/extras/Filters/MAF.h index 2204110..8f76dc8 100644 --- a/extras/Filters/MAF.h +++ b/extras/Filters/MAF.h @@ -1,6 +1,6 @@ /* Moving Average Filter implementation Using ring buffer to store/swap between the received data. - SmoothFactor (parsed as int to the constructor) can never be higher then + SmoothFactor (passed as int to the constructor) can never be higher than MAF_ARRAYSIZE constant. */ @@ -43,4 +43,5 @@ class MAF { uint8_t smoothFactor; double data[MAF_ARRAYSIZE]; uint8_t head; -}; \ No newline at end of file +}; + From cb9192af0785178c2be66a6a71eb57622d6678c2 Mon Sep 17 00:00:00 2001 From: Rafael Bachmann Date: Wed, 4 Jan 2017 22:19:45 +0100 Subject: [PATCH 2/5] actually check if Size variable is larger than MAF_ARRAYSIZE --- extras/Filters/MAF.h | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/extras/Filters/MAF.h b/extras/Filters/MAF.h index 8f76dc8..4259b3c 100644 --- a/extras/Filters/MAF.h +++ b/extras/Filters/MAF.h @@ -11,8 +11,8 @@ class MAF { // Constructor MAF(uint8_t Size) { // Save array size - smoothFactor = Size; - + smoothFactor = Size > MAF_ARRAYSIZE ? MAF_ARRAYSIZE : Size; + // Bear in mind that data[N] array is defined in private // but is not initialized. // For some reason the implementation works, but in case you encounter @@ -44,4 +44,3 @@ class MAF { double data[MAF_ARRAYSIZE]; uint8_t head; }; - From 099f17dcea9aac4cef9dd5e94aa2156cfc53e19d Mon Sep 17 00:00:00 2001 From: Rafael Bachmann Date: Wed, 4 Jan 2017 22:21:55 +0100 Subject: [PATCH 3/5] rename smoothFactor to buffer_size to give it a more meaningful name --- extras/Filters/MAF.h | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/extras/Filters/MAF.h b/extras/Filters/MAF.h index 4259b3c..2a565c7 100644 --- a/extras/Filters/MAF.h +++ b/extras/Filters/MAF.h @@ -11,7 +11,7 @@ class MAF { // Constructor MAF(uint8_t Size) { // Save array size - smoothFactor = Size > MAF_ARRAYSIZE ? MAF_ARRAYSIZE : Size; + buffer_size = Size > MAF_ARRAYSIZE ? MAF_ARRAYSIZE : Size; // Bear in mind that data[N] array is defined in private // but is not initialized. @@ -28,19 +28,19 @@ class MAF { head++; // If we reached end of the array, return to beginning - if (head == smoothFactor) head = 0; + if (head == buffer_size) head = 0; double sum; - for (uint8_t i = 0; i < smoothFactor; i++) { + for (uint8_t i = 0; i < buffer_size; i++) { sum += data[i]; } - sum /= smoothFactor; + sum /= buffer_size; return sum; }; private: - uint8_t smoothFactor; + uint8_t buffer_size; double data[MAF_ARRAYSIZE]; uint8_t head; }; From c113b13e8a0cc50dca641afc36234b216727f1bf Mon Sep 17 00:00:00 2001 From: Rafael Bachmann Date: Wed, 4 Jan 2017 22:35:31 +0100 Subject: [PATCH 4/5] add optional zeroing initialization for data array (in the comment) --- extras/Filters/MAF.h | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/extras/Filters/MAF.h b/extras/Filters/MAF.h index 2a565c7..b58a8ce 100644 --- a/extras/Filters/MAF.h +++ b/extras/Filters/MAF.h @@ -10,14 +10,18 @@ class MAF { public: // Constructor MAF(uint8_t Size) { - // Save array size + // Check and save array size buffer_size = Size > MAF_ARRAYSIZE ? MAF_ARRAYSIZE : Size; - // Bear in mind that data[N] array is defined in private - // but is not initialized. - // For some reason the implementation works, but in case you encounter - // "Weird behaviour", this is the place to look. + /* Bear in mind that data[N] array is defined in private + but is not initialized. + For some reason the implementation works, but in case you encounter + "Weird behaviour", this is the place to look. + for (uint8_t i = 0; i < buffer_size; i++) { + data[i] = 0; + }*/ + // Initialize head head = 0; }; From 33555f0a5093eabdb4d22244cc45b002462f1323 Mon Sep 17 00:00:00 2001 From: Rafael Bachmann Date: Thu, 5 Jan 2017 00:08:21 +0100 Subject: [PATCH 5/5] use new algorithm that is a bit more complicated but faster for larger windows --- extras/Filters/MAF.h | 37 +++++++++++++++++-------------------- 1 file changed, 17 insertions(+), 20 deletions(-) diff --git a/extras/Filters/MAF.h b/extras/Filters/MAF.h index b58a8ce..617c9e6 100644 --- a/extras/Filters/MAF.h +++ b/extras/Filters/MAF.h @@ -1,6 +1,6 @@ /* Moving Average Filter implementation Using ring buffer to store/swap between the received data. - SmoothFactor (passed as int to the constructor) can never be higher than + buffer_size (passed as int to the constructor) can never be higher than MAF_ARRAYSIZE constant. */ @@ -18,33 +18,30 @@ class MAF { For some reason the implementation works, but in case you encounter "Weird behaviour", this is the place to look. + // Initialize data for (uint8_t i = 0; i < buffer_size; i++) { - data[i] = 0; + data[i] = 0.0; }*/ + } - // Initialize head - head = 0; - }; - - double update(double value) { - // Store new value inside the array + double update(double value) { + // Subtract oldest value from sum + sum -= data[head]; + // Add newest value to sum + sum += value; + // Store new value inside the array (overwrite oldest) data[head] = value; + head++; - // If we reached end of the array, return to beginning if (head == buffer_size) head = 0; - - double sum; - for (uint8_t i = 0; i < buffer_size; i++) { - sum += data[i]; - } - - sum /= buffer_size; - return sum; - }; - + + return sum / buffer_size; + } + private: uint8_t buffer_size; double data[MAF_ARRAYSIZE]; - uint8_t head; + uint8_t head = 0; + double sum = 0.0; };