From 4508c3f67bed70fb2669c2e29f944dbf22707a7d Mon Sep 17 00:00:00 2001 From: andowinger <45272122+andowinger@users.noreply.github.com> Date: Sun, 27 Mar 2022 22:41:34 +0200 Subject: [PATCH 1/4] Update 17-smooth.html --- function/smooth/17-smooth.html | 1 + 1 file changed, 1 insertion(+) diff --git a/function/smooth/17-smooth.html b/function/smooth/17-smooth.html index 81643a1b..1083ec1a 100644 --- a/function/smooth/17-smooth.html +++ b/function/smooth/17-smooth.html @@ -10,6 +10,7 @@ + From 262976efc44bbe1810b3c50534f52db9931468fb Mon Sep 17 00:00:00 2001 From: andowinger <45272122+andowinger@users.noreply.github.com> Date: Sun, 27 Mar 2022 22:56:10 +0200 Subject: [PATCH 2/4] Update 17-smooth.js --- function/smooth/17-smooth.js | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/function/smooth/17-smooth.js b/function/smooth/17-smooth.js index c34c04aa..67594606 100644 --- a/function/smooth/17-smooth.js +++ b/function/smooth/17-smooth.js @@ -54,6 +54,16 @@ module.exports = function(RED) { v[top].tot = v[top].tot + n - v[top].pop; value = v[top].tot / v[top].a.length; } + if (node.action === "median") { + var sortedForMedian = v[top].a.slice().sort((a, b) => a - b); + var medianIndex = Math.floor(v[top].count / 2); + if (v[top].count % 2 === 0) { + value = (v[top].sortedForMedian[v[top].medianIndex - 1] + v[top].sortedForMedian[v[top].medianIndex]) / 2; + } + else { + value = v[top].sortedForMedian[v[top].medianIndex] + } + if (node.action === "sd") { v[top].tot = v[top].tot + n - v[top].pop; v[top].tot2 = v[top].tot2 + (n*n) - (v[top].pop * v[top].pop); From 7567d9f532f0abd59e2fecfcbcf496f1c309fdf4 Mon Sep 17 00:00:00 2001 From: andowinger <45272122+andowinger@users.noreply.github.com> Date: Sun, 27 Mar 2022 23:49:26 +0200 Subject: [PATCH 3/4] Update 17-smooth.js --- function/smooth/17-smooth.js | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/function/smooth/17-smooth.js b/function/smooth/17-smooth.js index 67594606..9545051b 100644 --- a/function/smooth/17-smooth.js +++ b/function/smooth/17-smooth.js @@ -55,14 +55,15 @@ module.exports = function(RED) { value = v[top].tot / v[top].a.length; } if (node.action === "median") { - var sortedForMedian = v[top].a.slice().sort((a, b) => a - b); - var medianIndex = Math.floor(v[top].count / 2); - if (v[top].count % 2 === 0) { - value = (v[top].sortedForMedian[v[top].medianIndex - 1] + v[top].sortedForMedian[v[top].medianIndex]) / 2; - } - else { - value = v[top].sortedForMedian[v[top].medianIndex] + var sortedForMedian = v[top].a.slice().sort((a, b) => a - b); + var medianIndex = Math.floor(v[top].iter / 2); + if (v[top].iter % 2 === 0) { + value = (sortedForMedian[medianIndex - 1] + sortedForMedian[medianIndex]) / 2; } + else { + value = sortedForMedian[medianIndex] + } + } if (node.action === "sd") { v[top].tot = v[top].tot + n - v[top].pop; From 870e9cdd45c23bcea8d8ed458ce137d4d841484b Mon Sep 17 00:00:00 2001 From: andowinger <45272122+andowinger@users.noreply.github.com> Date: Sun, 10 Apr 2022 22:48:01 +0200 Subject: [PATCH 4/4] Add files via upload --- test/function/smooth/17-smooth_spec.js | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/test/function/smooth/17-smooth_spec.js b/test/function/smooth/17-smooth_spec.js index ac7c7fc7..3e92ecc6 100644 --- a/test/function/smooth/17-smooth_spec.js +++ b/test/function/smooth/17-smooth_spec.js @@ -48,6 +48,31 @@ describe('smooth node', function() { }); }); + + + it('should average over a number of inputs using median', function(done) { + var flow = [{"id":"n1", "type":"smooth", action:"median", count:"5", round:"true", wires:[["n2"]] }, + {id:"n2", type:"helper"} ]; + helper.load(testNode, flow, function() { + var n1 = helper.getNode("n1"); + var n2 = helper.getNode("n2"); + var c = 0; + n2.on("input", function(msg) { + c += 1; + if (c === 4) { msg.should.have.a.property("payload", 55); } + if (c === 5) { msg.should.have.a.property("payload", 100); } + if (c === 6) { msg.should.have.a.property("payload", 550); done(); } + }); + n1.emit("input", {payload:1}); + n1.emit("input", {payload:10}); + n1.emit("input", {payload:100}); + n1.emit("input", {payload:1000}); + n1.emit("input", {payload:10000}); + n1.emit("input", {payload:100000}); + }); + }); + + it('should average over a number of inputs - another property - foo', function(done) { var flow = [{"id":"n1", "type":"smooth", action:"mean", count:"5", round:"true", property:"foo", wires:[["n2"]] }, {id:"n2", type:"helper"} ];