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