From 5d4a2558766a2e1398e9471c67b2034f172639c4 Mon Sep 17 00:00:00 2001 From: Dave Conway-Jones Date: Sun, 11 Oct 2015 17:47:08 +0100 Subject: [PATCH] Add Standard Deviation calculation to smooth node --- function/smooth/17-smooth.html | 3 ++- function/smooth/17-smooth.js | 9 +++++++++ function/smooth/README.md | 28 +++++++++++++++++++++------- function/smooth/package.json | 4 ++-- 4 files changed, 34 insertions(+), 10 deletions(-) diff --git a/function/smooth/17-smooth.html b/function/smooth/17-smooth.html index 66b2af90..e5e6ab83 100644 --- a/function/smooth/17-smooth.html +++ b/function/smooth/17-smooth.html @@ -20,7 +20,8 @@ diff --git a/function/smooth/17-smooth.js b/function/smooth/17-smooth.js index f17ce8f2..1905d7a6 100644 --- a/function/smooth/17-smooth.js +++ b/function/smooth/17-smooth.js @@ -25,6 +25,7 @@ module.exports = function(RED) { var node = this; var a = []; var tot = 0; + var tot2 = 0; var pop = 0; var old = null; @@ -51,6 +52,14 @@ module.exports = function(RED) { tot = tot + n - pop; msg.payload = tot / a.length; } + if (node.action === "sd") { + tot = tot + n - pop; + tot2 = tot2 + (n*n) - (pop * pop); + if (a.length > 1) { + msg.payload = Math.sqrt((a.length * tot2 - tot * tot)/(a.length * (a.length - 1))); + } + else { msg.payload = 0; } + } } if (node.round) { msg.payload = Math.round(msg.payload); } node.send(msg); diff --git a/function/smooth/README.md b/function/smooth/README.md index a4ebaa09..e93714c5 100644 --- a/function/smooth/README.md +++ b/function/smooth/README.md @@ -1,12 +1,21 @@ node-red-node-smooth ==================== -A Node-RED node that provides several simple smoothing algorithms for incoming data values. +A Node-RED node that provides +several simple smoothing algorithms for incoming data values. These include + + - Minimum + - Maximum + - Mean + - Standard Deviation + - High Pass Smoothing + - Loww Pass Smoothing Install ------- -Run the following command in the root directory of your Node-RED install +Run the following command in the root directory of your Node-RED install. This +is normally `~/.node-red` npm install node-red-node-smooth @@ -14,11 +23,16 @@ Run the following command in the root directory of your Node-RED install Usage ----- -A simple node to provide various functions across several previous values, including max, min, mean, high and low pass filters. +A simple node to provide various functions across several previous values, +including max, min, mean, standard deviation, high and low pass filters. -Max, Min and Mean work over a specified number of previous values. +Max, Min, Mean and Standard Deviation work over a rolling window, based on a +specified number of previous values. -The High and Low pass filters use a smoothing factor. The higher the number the more the smoothing. E.g. a value of 10 is similar to an α of 0.1. It is analogous to an RC time constant - but there is no time component to this as the code is based on events arriving. +The High and Low pass filters use a smoothing factor. The higher the number +the more the smoothing. E.g. a value of 10 is similar to an α of 0.1. +It is analogous to an RC time constant - but there is no time component to +this as the code is based on events arriving. - -**Note:** This only operates on **numbers**. Anything else will try to be made into a number and rejected if that fails. +**Note:** This node only operates on **numbers**. Anything else will try to be +made into a number and rejected if that fails. diff --git a/function/smooth/package.json b/function/smooth/package.json index 59b2c96e..84e5eb4d 100644 --- a/function/smooth/package.json +++ b/function/smooth/package.json @@ -1,6 +1,6 @@ { "name" : "node-red-node-smooth", - "version" : "0.0.4", + "version" : "0.0.5", "description" : "A Node-RED node that provides several simple smoothing algorithms for incoming data values.", "dependencies" : { }, @@ -9,7 +9,7 @@ "url":"https://github.com/node-red/node-red-nodes/tree/master/analysis/smooth" }, "license": "Apache-2.0", - "keywords": [ "node-red", "smooth", "average" ], + "keywords": [ "node-red", "smooth", "average", "standard deviation" ], "node-red" : { "nodes" : { "smooth": "17-smooth.js"