IoT-Labs/Coursework/buffer.h

78 lines
1.6 KiB
C
Raw Normal View History

2020-10-29 17:33:18 +00:00
#ifndef _BUFFER_GUARD
#define _BUFFER_GUARD
#include "util.h"
#include "math.h"
float*
getBuffer(int size)
{
return malloc(size * sizeof(float));
}
2020-10-29 17:33:18 +00:00
void
2020-11-16 16:20:44 +00:00
aggregateBuffer(float *bufferIn, int lengthIn, float *bufferOut, int lengthOut, int groupSize)
2020-10-29 17:33:18 +00:00
{
int requiredGroups = ceil((float)lengthIn/groupSize); // number of groups
2020-10-29 17:33:18 +00:00
int finalGroupSize = (lengthIn % groupSize) * groupSize;
if(requiredGroups > lengthOut) // error
2020-10-29 17:33:18 +00:00
{
putFloat((float)lengthIn/groupSize);
2020-11-16 16:20:44 +00:00
printf(" length out buffer required, %i provided\n", lengthOut);
2020-10-29 17:33:18 +00:00
return;
}
int g; // for group number
float *inputPtr = bufferIn; // cursor for full buffer
float *outputPtr = bufferOut; // cursor for output buffer
2020-10-29 17:33:18 +00:00
for(g = 0; g < requiredGroups; g++)
2020-11-16 16:20:44 +00:00
{
int length = groupSize; // length of this group's size
if(g == requiredGroups - 1 && finalGroupSize != 0) length = finalGroupSize; // shorten if necessary
2020-11-16 16:20:44 +00:00
*outputPtr = calculateMean(inputPtr, length); // SET
2020-11-16 16:20:44 +00:00
inputPtr += length; // increment both
2020-11-16 16:20:44 +00:00
outputPtr++;
2020-10-29 17:33:18 +00:00
}
}
void
2020-11-16 16:20:44 +00:00
clearBuffer(float *buffer, int length)
2020-10-29 17:33:18 +00:00
{
if(length > 0)
{
int i;
2020-11-16 16:20:44 +00:00
float *bufferPtr = buffer;
2020-10-29 17:33:18 +00:00
for(i = 0; i < length; i++)
{
2020-11-16 16:20:44 +00:00
*bufferPtr = 0.0;
bufferPtr++;
2020-10-29 17:33:18 +00:00
}
}
}
void
2020-11-16 16:20:44 +00:00
printBuffer(float *buffer, int length)
2020-10-29 17:33:18 +00:00
{
putchar('[');
if(length > 0)
{
int i;
2020-11-16 16:20:44 +00:00
float *ptr = buffer;
2020-10-29 17:33:18 +00:00
for(i = 0; i < length; i++)
{
if(i > 0) printf(", ");
2020-11-16 16:20:44 +00:00
putFloat(*ptr);
ptr++;
2020-10-29 17:33:18 +00:00
}
}
putchar(']');
}
#endif