IoT-Labs/Coursework/buffer.h

71 lines
1.4 KiB
C
Raw Normal View History

2020-10-29 17:33:18 +00:00
#ifndef _BUFFER_GUARD
#define _BUFFER_GUARD
#include "util.h"
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);
int finalGroupSize = (lengthIn % groupSize) * groupSize;
if(requiredGroups > lengthOut)
{
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
2020-11-16 16:20:44 +00:00
float *inputPtr = bufferIn;
float *outputPtr = bufferOut;
2020-10-29 17:33:18 +00:00
for(g = 0; g < requiredGroups; g++)
2020-11-16 16:20:44 +00:00
{
2020-10-29 17:33:18 +00:00
int length = groupSize;
if(g == requiredGroups - 1 && finalGroupSize != 0) length = finalGroupSize;
2020-11-16 16:20:44 +00:00
*outputPtr = calculateMean(inputPtr, length);
inputPtr += length;
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