component stats "compute mean and variance of a value";
// http://en.wikipedia.org/wiki/Algorithms_for_calculating_variance#Online_algorithm
pin in float in;
pin in bit enable "";
pin out float mean;
pin out float variance;
pin out u32 n;
variable hal_bit_t prev_enable = false;
variable hal_float_t m2 = 0.0;
variable hal_float_t delta = 0.0;
function _;
license "GPL";
;;
FUNCTION(_)
{
    if (enable ^ prev_enable) { // edge
	if (enable) {  // positive edge
	    mean = 0.0;
	    n = 0;
	    m2 = 0.0;
	}
    }
    if (enable) {
	n += 1;
	delta = in - mean;
	mean = mean + (hal_float_t)delta/n;
	m2 = m2 + delta * (in - mean);
	variance = m2/(hal_float_t)(n-1);
    }
    prev_enable = enable;

return 0;
}
