-
Notifications
You must be signed in to change notification settings - Fork 0
/
ringbufferAveraging.c
34 lines (30 loc) · 1002 Bytes
/
ringbufferAveraging.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
/*
* approximation.c
*
* Created: 07.11.2019 17:51:14
* Author: Kenan
*/
#include "ringbufferAveraging.h"
#include <stdlib.h>
void InitRingbufferAveraging(ringbufferAveraging_t * _ringbufferAveraging)
{
_ringbufferAveraging->bufferSize = (1 << bufferSizeShift);
_ringbufferAveraging->bufferHead = 0;
_ringbufferAveraging->values = (int*)calloc(_ringbufferAveraging->bufferSize,sizeof(int));
}
void addValue(ringbufferAveraging_t * _ringbufferAveraging, int newValue)
{
_ringbufferAveraging->values[_ringbufferAveraging->bufferHead] = newValue;
_ringbufferAveraging->bufferHead = (_ringbufferAveraging->bufferHead+1)%_ringbufferAveraging->bufferSize;
}
int getRingbufferAverage(ringbufferAveraging_t * _ringbufferAveraging)
{
int approximation = 0;
for(int i = 0; i < _ringbufferAveraging->bufferSize; i++)
{
approximation += _ringbufferAveraging->values[i];
}
//shifting instead of div to be faster!!
approximation = approximation >> bufferSizeShift;
return approximation;
}