moved to folders, updated etc
This commit is contained in:
@@ -0,0 +1,11 @@
|
||||
Averaging float buffer
|
||||
======================
|
||||
|
||||
*(You can adjust it to use doubles, if you prefer.)*
|
||||
|
||||
The `meanbuf_create()` function allocates a buffer.
|
||||
|
||||
You can then call `meanbuf_add()` to add a new value into the buffer (and remove the oldest).
|
||||
This function returns the current average value.
|
||||
|
||||
This buffer can be used for **signal smoothing** (such as from an analogue sensor).
|
||||
@@ -0,0 +1,64 @@
|
||||
#include <stdint.h>
|
||||
#include <malloc.h>
|
||||
|
||||
#include "meanbuf.h"
|
||||
|
||||
|
||||
struct meanbuf_struct {
|
||||
float * buf; // buffer (allocated at init)
|
||||
size_t cap; // capacity
|
||||
size_t nw; // next write index
|
||||
float mean; // updated on write
|
||||
};
|
||||
|
||||
|
||||
/** Init a buffer */
|
||||
MeanBuf *meanbuf_create(size_t size)
|
||||
{
|
||||
MeanBuf *mb = malloc(sizeof(MeanBuf));
|
||||
|
||||
if (size < 1) size = 1;
|
||||
|
||||
mb->buf = calloc(size, sizeof(float)); // calloc, so it starts with zeros.
|
||||
mb->cap = size;
|
||||
mb->nw = 0;
|
||||
mb->mean = 0;
|
||||
|
||||
// clean buffer
|
||||
for (uint16_t i = 0; i < size; i++) {
|
||||
mb->buf[i] = 0;
|
||||
}
|
||||
|
||||
return mb;
|
||||
}
|
||||
|
||||
|
||||
void meanbuf_destroy(MeanBuf *mb)
|
||||
{
|
||||
if (mb == NULL) return;
|
||||
|
||||
if (mb->buf != NULL) {
|
||||
free(mb->buf);
|
||||
}
|
||||
|
||||
free(mb);
|
||||
}
|
||||
|
||||
|
||||
/** Add a value to the buffer. Returns current mean. */
|
||||
float meanbuf_add(MeanBuf *mb, float f)
|
||||
{
|
||||
// add sample
|
||||
mb->buf[mb->nw++] = f;
|
||||
if (mb->nw == mb->cap) mb->nw = 0;
|
||||
|
||||
// calculate average
|
||||
float acc = 0;
|
||||
for (size_t i = 0; i < mb->cap; i++) {
|
||||
acc += mb->buf[i];
|
||||
}
|
||||
|
||||
acc /= mb->cap;
|
||||
|
||||
return mb->mean = acc;
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
/**
|
||||
* @file meanbuf.h
|
||||
* @author Ondřej Hruška, 2016
|
||||
*
|
||||
* Averaging float buffer. (You can adjust it to use doubles, if you prefer.)
|
||||
*
|
||||
* The meanbuf_create() function allocates a buffer.
|
||||
*
|
||||
* You can then call meanbuf_add() to add a new value into the buffer (and remove the oldest).
|
||||
* This function returns the current average value.
|
||||
*
|
||||
* This buffer can be used for signal smoothing (such as from an analogue sensor).
|
||||
*
|
||||
* MIT license
|
||||
*/
|
||||
|
||||
|
||||
#pragma once
|
||||
#include <stdlib.h>
|
||||
#include <stdint.h>
|
||||
|
||||
typedef struct meanbuf_struct MeanBuf;
|
||||
|
||||
/** Init a buffer */
|
||||
MeanBuf *meanbuf_create(size_t size);
|
||||
|
||||
/** Deinit a buffer (free buffer array) */
|
||||
void meanbuf_destroy(MeanBuf *mb);
|
||||
|
||||
/** Add a value to the buffer. Returns current mean. */
|
||||
float meanbuf_add(MeanBuf *mb, float f);
|
||||
Reference in New Issue
Block a user