aggregation working
This commit is contained in:
parent
ceb45b8a2e
commit
120e9cb80d
@ -4,7 +4,7 @@
|
||||
#include "util.h"
|
||||
|
||||
void
|
||||
aggregateBuffer(float bufferIn[], int lengthIn, float bufferOut[], int lengthOut, int groupSize)
|
||||
aggregateBuffer(float *bufferIn, int lengthIn, float *bufferOut, int lengthOut, int groupSize)
|
||||
{
|
||||
int requiredGroups = ceil((float)lengthIn/groupSize);
|
||||
int finalGroupSize = (lengthIn % groupSize) * groupSize;
|
||||
@ -12,45 +12,55 @@ aggregateBuffer(float bufferIn[], int lengthIn, float bufferOut[], int lengthOut
|
||||
if(requiredGroups > lengthOut)
|
||||
{
|
||||
putFloat((float)lengthIn/groupSize);
|
||||
printf(" length out buffer required, %i provided", lengthOut);
|
||||
printf(" length out buffer required, %i provided\n", lengthOut);
|
||||
return;
|
||||
}
|
||||
|
||||
int g;// for group number
|
||||
float *inputPtr = bufferIn;
|
||||
float *outputPtr = bufferOut;
|
||||
for(g = 0; g < requiredGroups; g++)
|
||||
{
|
||||
int length = groupSize;
|
||||
if(g == requiredGroups - 1 && finalGroupSize != 0) length = finalGroupSize;
|
||||
bufferOut[g] = calculateMean(&bufferIn + (g*groupSize), length);
|
||||
|
||||
*outputPtr = calculateMean(inputPtr, length);
|
||||
|
||||
inputPtr += length;
|
||||
outputPtr++;
|
||||
}
|
||||
}
|
||||
|
||||
void
|
||||
clearBuffer(float buffer[], int length)
|
||||
clearBuffer(float *buffer, int length)
|
||||
{
|
||||
if(length > 0)
|
||||
{
|
||||
int i;
|
||||
float *bufferPtr = buffer;
|
||||
for(i = 0; i < length; i++)
|
||||
{
|
||||
buffer[i] = 0.0;
|
||||
*bufferPtr = 0.0;
|
||||
bufferPtr++;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void
|
||||
printBuffer(float buffer[], int length)
|
||||
printBuffer(float *buffer, int length)
|
||||
{
|
||||
putchar('[');
|
||||
|
||||
if(length > 0)
|
||||
{
|
||||
int i;
|
||||
float *ptr = buffer;
|
||||
for(i = 0; i < length; i++)
|
||||
{
|
||||
if(i > 0) printf(", ");
|
||||
|
||||
putFloat(buffer[i]);
|
||||
putFloat(*ptr);
|
||||
ptr++;
|
||||
}
|
||||
}
|
||||
|
||||
|
File diff suppressed because it is too large
Load Diff
@ -1,8 +1,8 @@
|
||||
#define READING_INTERVAL 3 //in Hz
|
||||
#define BUFFER_SIZE 5
|
||||
#define BUFFER_SIZE 12 // length of buffer to populate
|
||||
|
||||
#define SD_THRESHOLD 3
|
||||
#define AGGREGATION_GROUP_SIZE 4
|
||||
#define SD_THRESHOLD 0 // whether to aggregate or flatten
|
||||
#define AGGREGATION_GROUP_SIZE 4 // group size to aggregate (4 in spec)
|
||||
|
||||
#include "contiki.h"
|
||||
#include "dev/light-sensor.h"
|
||||
@ -13,7 +13,9 @@
|
||||
#include "math.h"
|
||||
#include "buffer.h"
|
||||
|
||||
// get float from light sensor including T function
|
||||
//text to uncomment and make the sim reload new source
|
||||
|
||||
// get float from light sensor including transfer function
|
||||
float
|
||||
getLight(void)
|
||||
{
|
||||
@ -27,7 +29,7 @@ getLight(void)
|
||||
|
||||
// Process final buffer following aggregation
|
||||
void
|
||||
handleFinalBuffer(float buffer[], int length)
|
||||
handleFinalBuffer(float *buffer, int length)
|
||||
{
|
||||
printf("Final buffer output: ");
|
||||
printBuffer(buffer, length);putchar('\n');putchar('\n');
|
||||
@ -35,30 +37,31 @@ handleFinalBuffer(float buffer[], int length)
|
||||
|
||||
// Buffer filled with readings, process and aggregate
|
||||
void
|
||||
handleBufferRotation(float buffer[], int length)
|
||||
handleBufferRotation(float *buffer, int length)
|
||||
{
|
||||
printf("Buffer full, aggregating\n\n");
|
||||
float aggregated[BUFFER_SIZE];
|
||||
|
||||
Stats sd = calculateStdDev(buffer, length);
|
||||
Stats sd = calculateStdDev(buffer, 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[BUFFER_SIZE];
|
||||
int outLength = ceil((float)length/AGGREGATION_GROUP_SIZE);
|
||||
aggregateBuffer(buffer, length, outBuffer, outLength, AGGREGATION_GROUP_SIZE);
|
||||
float outBuffer[BUFFER_SIZE]; // CREATE OUTPUT BUFFER
|
||||
clearBuffer(&outBuffer, BUFFER_SIZE);
|
||||
|
||||
handleFinalBuffer(outBuffer, outLength);
|
||||
int outLength = ceil((float)length/AGGREGATION_GROUP_SIZE); // CALCULATE NUMBER OF OUTPUT ELEMENTS
|
||||
aggregateBuffer(buffer, length, &outBuffer, outLength, AGGREGATION_GROUP_SIZE);
|
||||
|
||||
handleFinalBuffer(&outBuffer, outLength); // PASS FINAL BUFFER
|
||||
}else
|
||||
{// buffer length to 1
|
||||
printf("Insignificant STD: ");
|
||||
putFloat(sd.std);
|
||||
printf(", squashing buffer\n");
|
||||
|
||||
handleFinalBuffer(&sd.mean, 1);
|
||||
handleFinalBuffer(&sd.mean, 1); // PASS FINAL BUFFER
|
||||
}
|
||||
clearBuffer(buffer, length);
|
||||
}
|
||||
@ -78,8 +81,8 @@ PROCESS_THREAD(aggregator_process, ev, data)
|
||||
etimer_set(&timer, CLOCK_SECOND/READING_INTERVAL);
|
||||
SENSORS_ACTIVATE(light_sensor);
|
||||
|
||||
clearBuffer(buffer, BUFFER_SIZE);
|
||||
printBuffer(buffer, BUFFER_SIZE);putchar('\n');putchar('\n');
|
||||
clearBuffer(&buffer, BUFFER_SIZE);
|
||||
printBuffer(&buffer, BUFFER_SIZE);putchar('\n');putchar('\n');
|
||||
/*END INIT*/
|
||||
|
||||
static int counter = 0;
|
||||
@ -87,15 +90,15 @@ PROCESS_THREAD(aggregator_process, ev, data)
|
||||
{
|
||||
PROCESS_WAIT_EVENT_UNTIL(ev=PROCESS_EVENT_TIMER);
|
||||
|
||||
float light_lx = getLight();
|
||||
float light_lx = getLight(); // GET
|
||||
|
||||
buffer[counter] = light_lx;
|
||||
printBuffer(buffer, BUFFER_SIZE);putchar('\n');
|
||||
buffer[counter] = light_lx; // STORE
|
||||
printBuffer(&buffer, BUFFER_SIZE);putchar('\n'); // DISPLAY
|
||||
|
||||
counter++;
|
||||
if(counter == BUFFER_SIZE)
|
||||
if(counter == BUFFER_SIZE) // CHECK WHETHER FULL
|
||||
{
|
||||
handleBufferRotation(buffer, BUFFER_SIZE);
|
||||
handleBufferRotation(buffer, BUFFER_SIZE); // PASS OUT IF FULL
|
||||
counter = 0;
|
||||
}
|
||||
|
||||
|
Binary file not shown.
Loading…
Reference in New Issue
Block a user