Skip to content

compute mmean

kgryte edited this page May 12, 2015 · 1 revision

Computes a moving arithmetic mean (sliding window average) over an array.

var data = [ 2, 4, 2, 7, 3 ];

var arr = compute.mmean( data, 2 );
// returns [ 3, 3, 4.5, 5 ]

For object arrays, provide an accessor function for accessing array values.

var data = [
	{'x':2},
	{'x':4},
	{'x':2},
	{'x':7},
	{'x':3}
];

function getValue( d ) {
	return d.x;
}

var values = compute.mmean( data, 2, {
	'accessor': getValue	
});
// returns [ 3, 3, 4.5, 5 ]

By default, a new array is returned. To compute the means in place, i.e., mutate the input array, set the copy option to false.

var data = [
	{'x':2},
	{'x':4},
	{'x':2},
	{'x':7},
	{'x':3}
];

function getValue( d ) {
	return d.x;
}

var values = compute.mmean( data, 2, {
	'accessor': getValue,
	'copy': false	
});
// returns [ 3, 3, 4.5, 5 ]

console.log( values === copy );
// returns true
Clone this wiki locally