IoT-Labs/Coursework/buffer.h

105 lines
2.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"
2020-11-17 01:45:25 +00:00
typedef struct Buffer {
float* items;
int length;
Stats stats;
} Buffer;
Buffer
getBuffer(int size) // retrieve new buffer with malloc-ed memory space
{
2020-11-17 01:45:25 +00:00
float* memSpace = (float*) malloc(size * sizeof(float));
Buffer buffer = {memSpace, size, };
return buffer;
}
2020-10-29 17:33:18 +00:00
void
freeBuffer(Buffer buffer) // little abstraction function to act as buffer destructor
{
free(buffer.items);
}
void
swapBufferMemory(Buffer *first, Buffer *second) // swap memspaces between buffers
{
float* firstItems = first->items; // swap input buffer and output buffer item pointers
first->items = second->items;
second->items = firstItems;
int firstLength = first->length; // swap lengths to iterate correctly
first->length = second->length;
second->length = firstLength;
}
void // perform aggregation into groupSize (4 in the spec)
2020-11-17 01:45:25 +00:00
aggregateBuffer(Buffer bufferIn, Buffer bufferOut, int groupSize)
2020-10-29 17:33:18 +00:00
{
2020-11-17 01:45:25 +00:00
int requiredGroups = ceil((float)bufferIn.length/groupSize); // number of groups
int finalGroupSize = bufferIn.length % groupSize; // work out length of final group if bufferIn not of length that divides nicely
2020-10-29 17:33:18 +00:00
if(requiredGroups > bufferOut.length) // error check
2020-10-29 17:33:18 +00:00
{
putFloat((float)bufferIn.length/groupSize); printf(" length out buffer required, %i provided\n", bufferOut.length);
2020-10-29 17:33:18 +00:00
return;
}
int g; // for group number
2020-11-17 01:45:25 +00:00
float *inputPtr = bufferIn.items; // cursor for full buffer
float *outputPtr = bufferOut.items; // 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 OUTPUT VALUE
2020-11-16 16:20:44 +00:00
inputPtr += length; // increment both cursors
2020-11-16 16:20:44 +00:00
outputPtr++;
2020-10-29 17:33:18 +00:00
}
}
void
2020-11-17 01:45:25 +00:00
clearBuffer(Buffer buffer)
2020-10-29 17:33:18 +00:00
{
2020-11-17 01:45:25 +00:00
int length = buffer.length;
2020-10-29 17:33:18 +00:00
if(length > 0)
{
int i;
2020-11-17 01:45:25 +00:00
float *bufferPtr = buffer.items;
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-17 01:45:25 +00:00
printBuffer(Buffer buffer)
2020-10-29 17:33:18 +00:00
{
putchar('[');
2020-11-17 01:45:25 +00:00
int length = buffer.length;
2020-10-29 17:33:18 +00:00
if(length > 0)
{
int i;
2020-11-17 01:45:25 +00:00
float *ptr = buffer.items;
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