struct-ised
This commit is contained in:
parent
111481b37d
commit
da602ad67b
@ -4,28 +4,36 @@
|
||||
#include "util.h"
|
||||
#include "math.h"
|
||||
|
||||
float*
|
||||
typedef struct Buffer {
|
||||
float* items;
|
||||
int length;
|
||||
Stats stats;
|
||||
} Buffer;
|
||||
|
||||
Buffer
|
||||
getBuffer(int size)
|
||||
{
|
||||
return malloc(size * sizeof(float));
|
||||
float* memSpace = (float*) malloc(size * sizeof(float));
|
||||
Buffer buffer = {memSpace, size, };
|
||||
return buffer;
|
||||
}
|
||||
|
||||
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 finalGroupSize = (lengthIn % groupSize) * groupSize;
|
||||
int requiredGroups = ceil((float)bufferIn.length/groupSize); // number of groups
|
||||
int finalGroupSize = (bufferIn.length % groupSize) * groupSize;
|
||||
|
||||
if(requiredGroups > lengthOut) // error
|
||||
if(requiredGroups > bufferOut.length) // error
|
||||
{
|
||||
putFloat((float)lengthIn/groupSize);
|
||||
printf(" length out buffer required, %i provided\n", lengthOut);
|
||||
putFloat((float)bufferIn.length/groupSize);
|
||||
printf(" length out buffer required, %i provided\n", bufferOut.length);
|
||||
return;
|
||||
}
|
||||
|
||||
int g; // for group number
|
||||
float *inputPtr = bufferIn; // cursor for full buffer
|
||||
float *outputPtr = bufferOut; // cursor for output buffer
|
||||
float *inputPtr = bufferIn.items; // cursor for full buffer
|
||||
float *outputPtr = bufferOut.items; // cursor for output buffer
|
||||
for(g = 0; g < requiredGroups; g++)
|
||||
{
|
||||
int length = groupSize; // length of this group's size
|
||||
@ -39,12 +47,13 @@ aggregateBuffer(float *bufferIn, int lengthIn, float *bufferOut, int lengthOut,
|
||||
}
|
||||
|
||||
void
|
||||
clearBuffer(float *buffer, int length)
|
||||
clearBuffer(Buffer buffer)
|
||||
{
|
||||
int length = buffer.length;
|
||||
if(length > 0)
|
||||
{
|
||||
int i;
|
||||
float *bufferPtr = buffer;
|
||||
float *bufferPtr = buffer.items;
|
||||
for(i = 0; i < length; i++)
|
||||
{
|
||||
*bufferPtr = 0.0;
|
||||
@ -54,14 +63,15 @@ clearBuffer(float *buffer, int length)
|
||||
}
|
||||
|
||||
void
|
||||
printBuffer(float *buffer, int length)
|
||||
printBuffer(Buffer buffer)
|
||||
{
|
||||
putchar('[');
|
||||
|
||||
int length = buffer.length;
|
||||
if(length > 0)
|
||||
{
|
||||
int i;
|
||||
float *ptr = buffer;
|
||||
float *ptr = buffer.items;
|
||||
for(i = 0; i < length; i++)
|
||||
{
|
||||
if(i > 0) printf(", ");
|
||||
|
File diff suppressed because it is too large
Load Diff
@ -9,7 +9,7 @@
|
||||
<simulation>
|
||||
<title>Aggregator Coursework</title>
|
||||
<speedlimit>1.0</speedlimit>
|
||||
<randomseed>123461</randomseed>
|
||||
<randomseed>123464</randomseed>
|
||||
<motedelay_us>1000000</motedelay_us>
|
||||
<radiomedium>
|
||||
se.sics.cooja.radiomediums.UDGM
|
||||
@ -90,7 +90,7 @@
|
||||
<zoomfactor>500.0</zoomfactor>
|
||||
</plugin_config>
|
||||
<width>1217</width>
|
||||
<z>3</z>
|
||||
<z>4</z>
|
||||
<height>183</height>
|
||||
<location_x>424</location_x>
|
||||
<location_y>852</location_y>
|
||||
@ -115,7 +115,7 @@
|
||||
<scrollpos>0,0</scrollpos>
|
||||
</plugin_config>
|
||||
<width>1201</width>
|
||||
<z>2</z>
|
||||
<z>3</z>
|
||||
<height>706</height>
|
||||
<location_x>408</location_x>
|
||||
<location_y>15</location_y>
|
||||
@ -141,7 +141,7 @@
|
||||
<scrollpos>0,0</scrollpos>
|
||||
</plugin_config>
|
||||
<width>317</width>
|
||||
<z>4</z>
|
||||
<z>2</z>
|
||||
<height>206</height>
|
||||
<location_x>28</location_x>
|
||||
<location_y>445</location_y>
|
||||
|
@ -1,8 +1,8 @@
|
||||
#define READING_INTERVAL 1 //in Hz
|
||||
#define BUFFER_SIZE 12 // length of buffer to populate
|
||||
#define READING_INTERVAL 3 //in Hz
|
||||
#define BUFFER_SIZE 9 // length of buffer to populate
|
||||
|
||||
#define SD_THRESHOLD 3 // whether to aggregate or flatten
|
||||
#define AGGREGATION_GROUP_SIZE 4 // group size to aggregate (4 in spec)
|
||||
#define SD_THRESHOLD 300 // whether to aggregate or flatten
|
||||
#define AGGREGATION_GROUP_SIZE 3 // group size to aggregate (4 in spec)
|
||||
|
||||
#include "contiki.h"
|
||||
|
||||
@ -13,8 +13,6 @@
|
||||
#include "math.h"
|
||||
#include "buffer.h"
|
||||
|
||||
// text to uncomment and make the sim reload new source
|
||||
|
||||
static process_event_t event_buffer_full;
|
||||
|
||||
/*---------------------------------------------------------------------------*/
|
||||
@ -34,10 +32,10 @@ PROCESS_THREAD(sensing_process, ev, data)
|
||||
SENSORS_ACTIVATE(light_sensor);
|
||||
leds_off(LEDS_ALL);
|
||||
|
||||
static float* buffer;
|
||||
static Buffer buffer;
|
||||
buffer = getBuffer(BUFFER_SIZE);
|
||||
clearBuffer(buffer, BUFFER_SIZE);
|
||||
printBuffer(buffer, BUFFER_SIZE);putchar('\n');putchar('\n');
|
||||
clearBuffer(buffer);
|
||||
printBuffer(buffer);putchar('\n');putchar('\n');
|
||||
/*END INIT*/
|
||||
|
||||
static int counter = 0;
|
||||
@ -48,13 +46,13 @@ PROCESS_THREAD(sensing_process, ev, data)
|
||||
|
||||
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
|
||||
|
||||
counter++;
|
||||
if(counter == BUFFER_SIZE) // CHECK WHETHER FULL
|
||||
if(counter == buffer.length) // CHECK WHETHER FULL
|
||||
{
|
||||
process_post(&aggregator_process, event_buffer_full, &buffer);
|
||||
counter = 0;
|
||||
@ -76,9 +74,11 @@ PROCESS_THREAD(aggregator_process, ev, data)
|
||||
PROCESS_WAIT_EVENT_UNTIL(ev == event_buffer_full);
|
||||
leds_on(LEDS_RED);
|
||||
|
||||
float *fullBuffer = *(float **)data;
|
||||
handleBufferRotation(fullBuffer, BUFFER_SIZE);
|
||||
free(fullBuffer);
|
||||
Buffer fullBuffer = *(Buffer *)data;
|
||||
/*********************/
|
||||
handleBufferRotation(fullBuffer);
|
||||
free(fullBuffer.items);
|
||||
/*********************/
|
||||
}
|
||||
|
||||
PROCESS_END();
|
||||
@ -86,36 +86,45 @@ PROCESS_THREAD(aggregator_process, ev, data)
|
||||
/*---------------------------------------------------------------------------*/
|
||||
// Buffer filled with readings, process and aggregate
|
||||
void
|
||||
handleBufferRotation(float *buffer, int length)
|
||||
handleBufferRotation(Buffer inBuffer)
|
||||
{
|
||||
printf("Buffer full, aggregating\n\n");
|
||||
|
||||
Buffer outBuffer; // OUTPUT BUFFER HOLDER
|
||||
// above pointer is assigned a buffer in either of the below cases
|
||||
|
||||
Stats sd = calculateStdDev(buffer, length); // GET BUFFER STATISTICS
|
||||
Stats sd = calculateStdDev(inBuffer.items, inBuffer.length); // GET BUFFER STATISTICS
|
||||
if(sd.std > SD_THRESHOLD)
|
||||
{// buffer length by 4
|
||||
printf("Significant STD: ");putFloat(sd.std);printf(", compressing buffer\n");
|
||||
|
||||
float *outBuffer = getBuffer(length); // CREATE OUTPUT BUFFER
|
||||
clearBuffer(outBuffer, length);
|
||||
|
||||
int outLength = ceil((float)length/AGGREGATION_GROUP_SIZE); // CALCULATE NUMBER OF OUTPUT ELEMENTS
|
||||
aggregateBuffer(buffer, length, outBuffer, outLength, AGGREGATION_GROUP_SIZE);
|
||||
int outLength = ceil((float)inBuffer.length/AGGREGATION_GROUP_SIZE); // CALCULATE NUMBER OF OUTPUT ELEMENTS
|
||||
outBuffer = getBuffer(outLength); // CREATE OUTPUT BUFFER
|
||||
|
||||
aggregateBuffer(inBuffer, outBuffer, AGGREGATION_GROUP_SIZE);
|
||||
|
||||
handleFinalBuffer(outBuffer, outLength); // PASS FINAL BUFFER
|
||||
free(outBuffer); // RELEASE
|
||||
}else
|
||||
{// buffer length to 1
|
||||
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
|
||||
void
|
||||
handleFinalBuffer(float *buffer, int length)
|
||||
handleFinalBuffer(Buffer buffer)
|
||||
{
|
||||
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.
Loading…
Reference in New Issue
Block a user