view stat.c @ 183:791ca26fee6a

Rewrite data writing to file Fmt() uses by default truncation to conver double arguments to their text representation. Rounding must be used. Rewrite using standard C functions to get rid of this problem (and probably make it more efficient). Extend to handle arbitrary number of channels and to report errors on opening the data files.
author Daniele Nicolodi <daniele.nicolodi@obspm.fr>
date Fri, 21 Feb 2014 18:42:30 +0100
parents be87c8e78266
children
line wrap: on
line source

#include <ansi_c.h>
#include "stat.h"

void stat_zero(struct stat *s)
{
	s->samples = 0;
	s->mean = 0.0;
	s->slope = 0.0;
	s->previous = 0.0;
}


void stat_accumulate(struct stat *s, double value)
{
	s->samples += 1;
	if (s->samples > 1)
		s->slope = (s->slope * (s->samples - 2) + 6 * (value - s->mean) / s->samples) / (s->samples + 1);
	s->mean = ((s->samples - 1) * s->mean + value) / s->samples;
	s->previous = value;
}


void rollmean_zero(struct rollmean *s)
{
	s->nobs = 0;
	s->mean = 0.0;
	s->acc = 0.0;
	memset(s->prev, 0, sizeof(s->prev));
}


void rollmean_accumulate(struct rollmean *s, double value)
{
	s->acc = s->acc + value - s->prev[s->nobs % _ROLLMEAN_WLEN];
	s->prev[s->nobs % _ROLLMEAN_WLEN] = value;
	s->nobs++;
	if (s->nobs < _ROLLMEAN_WLEN)
		s->mean = s->acc / s->nobs;
	else
		s->mean = s->acc / _ROLLMEAN_WLEN;
}