<?php

function fn_hstddev_t($aql)
{
	$list = $aql->Consume(1);
	
	LiveValuesForPoints($list[0]->value);
	
	//Calculate Average
	foreach($list[0]->value as $p)
	{		
		$average = 0;
		$samples = 0;
		$total = 0;
		
		if (($p->history !== FALSE) && (count($p->history) > 0))
		{
			foreach($p->history as $hst)
			{
				$samples++;
				$total = $total + $hst[1];
			}
		}
		if ($samples > 0)
		{
			$average = $total / $samples;					
			
			//Calculate Variance
			$variance = array();
			foreach($p->history as $hst)
			{				
				$variance[] =$hst[1] - $average;
			}
			
			$sum = 0;
			foreach($variance as $v)
			{
				$sum += $v * $v;
			}
			
			$p->rawvalue = sqrt($sum / $samples);
			$p->value = $p->rawvalue;
			$p->history = FALSE;
		}		
	}
		
	$indx = round(count($lst)/2);
		
	array_push($aql->stack,$list[0]);
}

?>