Blame mave.c

Packit 9c3e7e
/**
Packit 9c3e7e
 * @file mave.c
Packit 9c3e7e
 * @note Copyright (C) 2011 Richard Cochran <richardcochran@gmail.com>
Packit 9c3e7e
 *
Packit 9c3e7e
 * This program is free software; you can redistribute it and/or modify
Packit 9c3e7e
 * it under the terms of the GNU General Public License as published by
Packit 9c3e7e
 * the Free Software Foundation; either version 2 of the License, or
Packit 9c3e7e
 * (at your option) any later version.
Packit 9c3e7e
 *
Packit 9c3e7e
 * This program is distributed in the hope that it will be useful,
Packit 9c3e7e
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 9c3e7e
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
Packit 9c3e7e
 * GNU General Public License for more details.
Packit 9c3e7e
 *
Packit 9c3e7e
 * You should have received a copy of the GNU General Public License along
Packit 9c3e7e
 * with this program; if not, write to the Free Software Foundation, Inc.,
Packit 9c3e7e
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
Packit 9c3e7e
 */
Packit 9c3e7e
#include <stdlib.h>
Packit 9c3e7e
#include <string.h>
Packit 9c3e7e
Packit 9c3e7e
#include "mave.h"
Packit 9c3e7e
#include "filter_private.h"
Packit 9c3e7e
Packit 9c3e7e
struct mave {
Packit 9c3e7e
	struct filter filter;
Packit 9c3e7e
	int cnt;
Packit 9c3e7e
	int len;
Packit 9c3e7e
	int index;
Packit 9c3e7e
	tmv_t sum;
Packit 9c3e7e
	tmv_t *val;
Packit 9c3e7e
};
Packit 9c3e7e
Packit 9c3e7e
static void mave_destroy(struct filter *filter)
Packit 9c3e7e
{
Packit 9c3e7e
	struct mave *m = container_of(filter, struct mave, filter);
Packit 9c3e7e
	free(m->val);
Packit 9c3e7e
	free(m);
Packit 9c3e7e
}
Packit 9c3e7e
Packit 9c3e7e
static tmv_t mave_accumulate(struct filter *filter, tmv_t val)
Packit 9c3e7e
{
Packit 9c3e7e
	struct mave *m = container_of(filter, struct mave, filter);
Packit 9c3e7e
Packit 9c3e7e
	m->sum = tmv_sub(m->sum, m->val[m->index]);
Packit 9c3e7e
	m->val[m->index] = val;
Packit 9c3e7e
	m->index = (1 + m->index) % m->len;
Packit 9c3e7e
	m->sum = tmv_add(m->sum, val);
Packit 9c3e7e
	if (m->cnt < m->len) {
Packit 9c3e7e
		m->cnt++;
Packit 9c3e7e
	}
Packit 9c3e7e
	return tmv_div(m->sum, m->cnt);
Packit 9c3e7e
}
Packit 9c3e7e
Packit 9c3e7e
static void mave_reset(struct filter *filter)
Packit 9c3e7e
{
Packit 9c3e7e
	struct mave *m = container_of(filter, struct mave, filter);
Packit 9c3e7e
Packit 9c3e7e
	m->cnt = 0;
Packit 9c3e7e
	m->index = 0;
Packit 9c3e7e
	m->sum = tmv_zero();
Packit 9c3e7e
	memset(m->val, 0, m->len * sizeof(*m->val));
Packit 9c3e7e
}
Packit 9c3e7e
Packit 9c3e7e
struct filter *mave_create(int length)
Packit 9c3e7e
{
Packit 9c3e7e
	struct mave *m;
Packit 9c3e7e
	m = calloc(1, sizeof(*m));
Packit 9c3e7e
	if (!m) {
Packit 9c3e7e
		return NULL;
Packit 9c3e7e
	}
Packit 9c3e7e
	m->filter.destroy = mave_destroy;
Packit 9c3e7e
	m->filter.sample = mave_accumulate;
Packit 9c3e7e
	m->filter.reset = mave_reset;
Packit 9c3e7e
	m->val = calloc(1, length * sizeof(*m->val));
Packit 9c3e7e
	if (!m->val) {
Packit 9c3e7e
		free(m);
Packit 9c3e7e
		return NULL;
Packit 9c3e7e
	}
Packit 9c3e7e
	m->len = length;
Packit 9c3e7e
	return &m->filter;
Packit 9c3e7e
}