struct-ised

This commit is contained in:
aj 2020-11-17 01:45:25 +00:00
parent 111481b37d
commit da602ad67b
5 changed files with 1238 additions and 1219 deletions

View File

@ -4,28 +4,36 @@
#include "util.h" #include "util.h"
#include "math.h" #include "math.h"
float* typedef struct Buffer {
float* items;
int length;
Stats stats;
} Buffer;
Buffer
getBuffer(int size) getBuffer(int size)
{ {
return malloc(size * sizeof(float)); float* memSpace = (float*) malloc(size * sizeof(float));
Buffer buffer = {memSpace, size, };
return buffer;
} }
void void
aggregateBuffer(float *bufferIn, int lengthIn, float *bufferOut, int lengthOut, int groupSize) aggregateBuffer(Buffer bufferIn, Buffer bufferOut, int groupSize)
{ {
int requiredGroups = ceil((float)lengthIn/groupSize); // number of groups int requiredGroups = ceil((float)bufferIn.length/groupSize); // number of groups
int finalGroupSize = (lengthIn % groupSize) * groupSize; int finalGroupSize = (bufferIn.length % groupSize) * groupSize;
if(requiredGroups > lengthOut) // error if(requiredGroups > bufferOut.length) // error
{ {
putFloat((float)lengthIn/groupSize); putFloat((float)bufferIn.length/groupSize);
printf(" length out buffer required, %i provided\n", lengthOut); printf(" length out buffer required, %i provided\n", bufferOut.length);
return; return;
} }
int g; // for group number int g; // for group number
float *inputPtr = bufferIn; // cursor for full buffer float *inputPtr = bufferIn.items; // cursor for full buffer
float *outputPtr = bufferOut; // cursor for output buffer float *outputPtr = bufferOut.items; // cursor for output buffer
for(g = 0; g < requiredGroups; g++) for(g = 0; g < requiredGroups; g++)
{ {
int length = groupSize; // length of this group's size int length = groupSize; // length of this group's size
@ -39,12 +47,13 @@ aggregateBuffer(float *bufferIn, int lengthIn, float *bufferOut, int lengthOut,
} }
void void
clearBuffer(float *buffer, int length) clearBuffer(Buffer buffer)
{ {
int length = buffer.length;
if(length > 0) if(length > 0)
{ {
int i; int i;
float *bufferPtr = buffer; float *bufferPtr = buffer.items;
for(i = 0; i < length; i++) for(i = 0; i < length; i++)
{ {
*bufferPtr = 0.0; *bufferPtr = 0.0;
@ -54,14 +63,15 @@ clearBuffer(float *buffer, int length)
} }
void void
printBuffer(float *buffer, int length) printBuffer(Buffer buffer)
{ {
putchar('['); putchar('[');
int length = buffer.length;
if(length > 0) if(length > 0)
{ {
int i; int i;
float *ptr = buffer; float *ptr = buffer.items;
for(i = 0; i < length; i++) for(i = 0; i < length; i++)
{ {
if(i > 0) printf(", "); if(i > 0) printf(", ");

File diff suppressed because it is too large Load Diff

View File

@ -9,7 +9,7 @@
<simulation> <simulation>
<title>Aggregator Coursework</title> <title>Aggregator Coursework</title>
<speedlimit>1.0</speedlimit> <speedlimit>1.0</speedlimit>
<randomseed>123461</randomseed> <randomseed>123464</randomseed>
<motedelay_us>1000000</motedelay_us> <motedelay_us>1000000</motedelay_us>
<radiomedium> <radiomedium>
se.sics.cooja.radiomediums.UDGM se.sics.cooja.radiomediums.UDGM
@ -90,7 +90,7 @@
<zoomfactor>500.0</zoomfactor> <zoomfactor>500.0</zoomfactor>
</plugin_config> </plugin_config>
<width>1217</width> <width>1217</width>
<z>3</z> <z>4</z>
<height>183</height> <height>183</height>
<location_x>424</location_x> <location_x>424</location_x>
<location_y>852</location_y> <location_y>852</location_y>
@ -115,7 +115,7 @@
<scrollpos>0,0</scrollpos> <scrollpos>0,0</scrollpos>
</plugin_config> </plugin_config>
<width>1201</width> <width>1201</width>
<z>2</z> <z>3</z>
<height>706</height> <height>706</height>
<location_x>408</location_x> <location_x>408</location_x>
<location_y>15</location_y> <location_y>15</location_y>
@ -141,7 +141,7 @@
<scrollpos>0,0</scrollpos> <scrollpos>0,0</scrollpos>
</plugin_config> </plugin_config>
<width>317</width> <width>317</width>
<z>4</z> <z>2</z>
<height>206</height> <height>206</height>
<location_x>28</location_x> <location_x>28</location_x>
<location_y>445</location_y> <location_y>445</location_y>

View File

@ -1,8 +1,8 @@
#define READING_INTERVAL 1 //in Hz #define READING_INTERVAL 3 //in Hz
#define BUFFER_SIZE 12 // length of buffer to populate #define BUFFER_SIZE 9 // length of buffer to populate
#define SD_THRESHOLD 3 // whether to aggregate or flatten #define SD_THRESHOLD 300 // whether to aggregate or flatten
#define AGGREGATION_GROUP_SIZE 4 // group size to aggregate (4 in spec) #define AGGREGATION_GROUP_SIZE 3 // group size to aggregate (4 in spec)
#include "contiki.h" #include "contiki.h"
@ -13,8 +13,6 @@
#include "math.h" #include "math.h"
#include "buffer.h" #include "buffer.h"
// text to uncomment and make the sim reload new source
static process_event_t event_buffer_full; static process_event_t event_buffer_full;
/*---------------------------------------------------------------------------*/ /*---------------------------------------------------------------------------*/
@ -34,10 +32,10 @@ PROCESS_THREAD(sensing_process, ev, data)
SENSORS_ACTIVATE(light_sensor); SENSORS_ACTIVATE(light_sensor);
leds_off(LEDS_ALL); leds_off(LEDS_ALL);
static float* buffer; static Buffer buffer;
buffer = getBuffer(BUFFER_SIZE); buffer = getBuffer(BUFFER_SIZE);
clearBuffer(buffer, BUFFER_SIZE); clearBuffer(buffer);
printBuffer(buffer, BUFFER_SIZE);putchar('\n');putchar('\n'); printBuffer(buffer);putchar('\n');putchar('\n');
/*END INIT*/ /*END INIT*/
static int counter = 0; static int counter = 0;
@ -48,13 +46,13 @@ PROCESS_THREAD(sensing_process, ev, data)
float light_lx = getLight(); // GET float light_lx = getLight(); // GET
buffer[counter] = light_lx; // STORE buffer.items[counter] = light_lx; // STORE
printf("%2i/%i: ", counter + 1, BUFFER_SIZE);putFloat(light_lx);putchar('\n'); // DISPLAY VALUE printf("%2i/%i: ", counter + 1, buffer.length);putFloat(light_lx);putchar('\n'); // DISPLAY VALUE
//printBuffer(buffer, BUFFER_SIZE);putchar('\n'); // DISPLAY CURRENT BUFFER //printBuffer(buffer, BUFFER_SIZE);putchar('\n'); // DISPLAY CURRENT BUFFER
counter++; counter++;
if(counter == BUFFER_SIZE) // CHECK WHETHER FULL if(counter == buffer.length) // CHECK WHETHER FULL
{ {
process_post(&aggregator_process, event_buffer_full, &buffer); process_post(&aggregator_process, event_buffer_full, &buffer);
counter = 0; counter = 0;
@ -76,9 +74,11 @@ PROCESS_THREAD(aggregator_process, ev, data)
PROCESS_WAIT_EVENT_UNTIL(ev == event_buffer_full); PROCESS_WAIT_EVENT_UNTIL(ev == event_buffer_full);
leds_on(LEDS_RED); leds_on(LEDS_RED);
float *fullBuffer = *(float **)data; Buffer fullBuffer = *(Buffer *)data;
handleBufferRotation(fullBuffer, BUFFER_SIZE); /*********************/
free(fullBuffer); handleBufferRotation(fullBuffer);
free(fullBuffer.items);
/*********************/
} }
PROCESS_END(); PROCESS_END();
@ -86,36 +86,45 @@ PROCESS_THREAD(aggregator_process, ev, data)
/*---------------------------------------------------------------------------*/ /*---------------------------------------------------------------------------*/
// Buffer filled with readings, process and aggregate // Buffer filled with readings, process and aggregate
void void
handleBufferRotation(float *buffer, int length) handleBufferRotation(Buffer inBuffer)
{ {
printf("Buffer full, aggregating\n\n"); printf("Buffer full, aggregating\n\n");
Stats sd = calculateStdDev(buffer, length); // GET BUFFER STATISTICS Buffer outBuffer; // OUTPUT BUFFER HOLDER
// above pointer is assigned a buffer in either of the below cases
Stats sd = calculateStdDev(inBuffer.items, inBuffer.length); // GET BUFFER STATISTICS
if(sd.std > SD_THRESHOLD) if(sd.std > SD_THRESHOLD)
{// buffer length by 4 {// buffer length by 4
printf("Significant STD: ");putFloat(sd.std);printf(", compressing buffer\n"); printf("Significant STD: ");putFloat(sd.std);printf(", compressing buffer\n");
float *outBuffer = getBuffer(length); // CREATE OUTPUT BUFFER int outLength = ceil((float)inBuffer.length/AGGREGATION_GROUP_SIZE); // CALCULATE NUMBER OF OUTPUT ELEMENTS
clearBuffer(outBuffer, length); outBuffer = getBuffer(outLength); // CREATE OUTPUT BUFFER
int outLength = ceil((float)length/AGGREGATION_GROUP_SIZE); // CALCULATE NUMBER OF OUTPUT ELEMENTS aggregateBuffer(inBuffer, outBuffer, AGGREGATION_GROUP_SIZE);
aggregateBuffer(buffer, length, outBuffer, outLength, AGGREGATION_GROUP_SIZE);
handleFinalBuffer(outBuffer, outLength); // PASS FINAL BUFFER
free(outBuffer); // RELEASE
}else }else
{// buffer length to 1 {// buffer length to 1
printf("Insignificant STD: ");putFloat(sd.std);printf(", squashing buffer\n"); printf("Insignificant STD: ");putFloat(sd.std);printf(", squashing buffer\n");
handleFinalBuffer(&sd.mean, 1); // PASS FINAL BUFFER outBuffer = getBuffer(1); // CREATE OUTPUT BUFFER
outBuffer.items[0] = sd.mean;
} }
outBuffer.stats = sd; // final compressed buffer has pointer to stats for uncompressed data in case of further interest
/*********************/
handleFinalBuffer(outBuffer); // PASS FINAL BUFFER
free(outBuffer.items); // RELEASE ITEMS
/*********************/
} }
// Process final buffer following aggregation // Process final buffer following aggregation
void void
handleFinalBuffer(float *buffer, int length) handleFinalBuffer(Buffer buffer)
{ {
printf("Final buffer output: "); printf("Final buffer output: ");
printBuffer(buffer, length);putchar('\n');putchar('\n'); printBuffer(buffer);putchar('\n');
printf("Mean: ");putFloat(buffer.stats.mean);putchar('\n');
printf("Std Dev: ");putFloat(buffer.stats.std);putchar('\n');putchar('\n');
} }
/*---------------------------------------------------------------------------*/ /*---------------------------------------------------------------------------*/

Binary file not shown.