Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Andowinger add median #896

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions function/smooth/17-smooth.html
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
<option value="max">Return the maximum value seen</option>
<option value="min">Return the minimum value seen</option>
<option value="mean">Return the mean value</option>
<option value="median">Return the median value</option>
<option value="sd">Return the standard deviation</option>
<option value="low">Perform low pass filter</option>
<option value="high">Perform high pass filter</option>
Expand Down
11 changes: 11 additions & 0 deletions function/smooth/17-smooth.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,17 @@ 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].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;
v[top].tot2 = v[top].tot2 + (n*n) - (v[top].pop * v[top].pop);
Expand Down
25 changes: 25 additions & 0 deletions test/function/smooth/17-smooth_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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"} ];
Expand Down