Implemented getMetrics()
This commit is contained in:
parent
c0bb5c46ef
commit
23951656c1
44
buildResult/dataAcquisition.h
Normal file
44
buildResult/dataAcquisition.h
Normal file
@ -0,0 +1,44 @@
|
||||
#ifndef DATA_ACQUISITION_H
|
||||
#define DATA_ACQUISITION_H
|
||||
|
||||
#include <stdbool.h>
|
||||
#include <math.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
typedef struct {
|
||||
float mean;
|
||||
float standardDeviation;
|
||||
int possibleFaultySensor;
|
||||
} Metrics;
|
||||
|
||||
typedef struct {
|
||||
int sensorsNumber;
|
||||
int slidingWindowSize;
|
||||
} Matrix;
|
||||
|
||||
void initializeReadings(int numSensors, float deltaTime);
|
||||
bool freeReadings();
|
||||
|
||||
int getSensorsNumber();
|
||||
int getSlidingWindowSize();
|
||||
bool isFull(int sensorIndex);
|
||||
|
||||
void addReading(float value);
|
||||
|
||||
float getAverageOnSensor(int sensorIndex);
|
||||
float getAverageOnAllSensors();
|
||||
float getOverallAverage();
|
||||
|
||||
float getStandardDeviationOnSensor(int sensorIndex);
|
||||
float getStandardDeviationOnAllSensors();
|
||||
float getOverallStandardDeviation();
|
||||
|
||||
float getLastReading(int sensorIndex);
|
||||
|
||||
bool anomalyDetect(float average, float standardDeviation);
|
||||
|
||||
int getOutlierCount();
|
||||
|
||||
Metrics getMetrics(float **readings, int sensorNumber, int slidingWindow);
|
||||
|
||||
#endif // DATA_ACQUISITION_H
|
||||
@ -5,7 +5,18 @@
|
||||
#include <math.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
void initializeReadings();
|
||||
typedef struct {
|
||||
float mean;
|
||||
float standardDeviation;
|
||||
int possibleFaultySensor;
|
||||
} Metrics;
|
||||
|
||||
typedef struct {
|
||||
int sensorsNumber;
|
||||
int slidingWindowSize;
|
||||
} Matrix;
|
||||
|
||||
void initializeReadings(int numSensors, float deltaTime);
|
||||
bool freeReadings();
|
||||
|
||||
int getSensorsNumber();
|
||||
@ -28,4 +39,6 @@ bool anomalyDetect(float average, float standardDeviation);
|
||||
|
||||
int getOutlierCount();
|
||||
|
||||
Metrics getMetrics(float **readings, int sensorNumber, int slidingWindow);
|
||||
|
||||
#endif // DATA_ACQUISITION_H
|
||||
@ -10,6 +10,12 @@ static float **readings;
|
||||
static int sensorsNumber;
|
||||
static int slidingWindowSize;
|
||||
|
||||
typedef struct {
|
||||
float mean;
|
||||
float standardDeviation;
|
||||
int possibleFaultySensor;
|
||||
} Metrics;
|
||||
|
||||
|
||||
/**
|
||||
* @brief Sets the number of sensors.
|
||||
@ -49,7 +55,9 @@ static void setSlidingWindowSize(int size) {
|
||||
*
|
||||
* If memory allocation fails at any point, the function prints an error message and exits the program.
|
||||
*/
|
||||
void initializeReadings(int numSensors, int windowSize) {
|
||||
void initializeReadings(int numSensors, float deltaTime) {
|
||||
|
||||
int windowSize = (int)(1000 / deltaTime); // Standard case of 1 second of acquisition
|
||||
|
||||
// Allocate memory for the array of sensor readings
|
||||
readings = (float **)malloc(numSensors * sizeof(float *));
|
||||
@ -379,4 +387,56 @@ float getOverallStandardDeviation() {
|
||||
*/
|
||||
int getOutlierCount() {
|
||||
return outlierCount;
|
||||
}
|
||||
|
||||
Metrics getMetrics(float **readings, int sensorNumber, int slidingWindow){
|
||||
Metrics metrics;
|
||||
|
||||
int average = 0;
|
||||
int standardDeviation = 0;
|
||||
int outlierCount = 0;
|
||||
int* faultySensors = (int*)malloc(sensorNumber * sizeof(int));
|
||||
|
||||
for(int i = 0; i < sensorNumber; i++){
|
||||
faultySensors[i] = 0;
|
||||
}
|
||||
|
||||
float sum = 0;
|
||||
|
||||
for(int i = 0; i < sensorNumber; i++){
|
||||
for(int j = 0; j < slidingWindow; j++){
|
||||
sum += readings[i][j];
|
||||
}
|
||||
}
|
||||
|
||||
average = sum / (sensorNumber * slidingWindow);
|
||||
|
||||
for(int i = 0; i < sensorNumber; i++){
|
||||
for(int j = 0; j < slidingWindow; j++){
|
||||
standardDeviation += pow(readings[i][j] - average, 2);
|
||||
}
|
||||
}
|
||||
|
||||
standardDeviation = sqrt(standardDeviation / (sensorNumber * slidingWindow));
|
||||
for(int i = 0; i < sensorNumber; i++){
|
||||
for(int j = 0; j < slidingWindow; j++){
|
||||
if(readings[i][j] > average + 2.17 * standardDeviation || readings[i][j] < average - 2.17 * standardDeviation){
|
||||
outlierCount++;
|
||||
faultySensors[i]++;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int max = 0;
|
||||
for(int i = 0; i < sensorNumber; i++){
|
||||
if(faultySensors[i] > max){
|
||||
max = faultySensors[i];
|
||||
}
|
||||
}
|
||||
|
||||
metrics.mean = average;
|
||||
metrics.standardDeviation = standardDeviation;
|
||||
metrics.possibleFaultySensor = max;
|
||||
|
||||
return metrics;
|
||||
}
|
||||
@ -31,7 +31,7 @@
|
||||
*/
|
||||
void test_initializeReadings_uniform()
|
||||
{
|
||||
initializeReadings(NUMBER_OF_SENSORS, SLIDING_WINDOW_SIZE);
|
||||
initializeReadings(NUMBER_OF_SENSORS, 100);
|
||||
assert(getSensorsNumber() == NUMBER_OF_SENSORS);
|
||||
assert(getSlidingWindowSize() == SLIDING_WINDOW_SIZE);
|
||||
}
|
||||
@ -237,7 +237,7 @@ void test_freeReadings()
|
||||
*/
|
||||
void test_initializeReadings_normal()
|
||||
{
|
||||
initializeReadings(NUMBER_OF_SENSORS, SLIDING_WINDOW_SIZE);
|
||||
initializeReadings(NUMBER_OF_SENSORS, 100);
|
||||
assert(getSensorsNumber() == NUMBER_OF_SENSORS);
|
||||
assert(getSlidingWindowSize() == SLIDING_WINDOW_SIZE);
|
||||
}
|
||||
@ -424,6 +424,30 @@ void test_anomalyDetect_normal()
|
||||
assert(fabs(NUMBER_OF_SENSORS * SLIDING_WINDOW_SIZE * 0.05 + 1 >= getOutlierCount())); // Assuming 5% of the data is outliers and adding one more
|
||||
}
|
||||
|
||||
void test_getMetrics()
|
||||
{
|
||||
|
||||
// initialize a matrix of readings
|
||||
float **readings = (float **)malloc(NUMBER_OF_SENSORS * sizeof(float *));
|
||||
for (int i = 0; i < NUMBER_OF_SENSORS; i++)
|
||||
{
|
||||
readings[i] = (float *)malloc(SLIDING_WINDOW_SIZE * sizeof(float));
|
||||
for (int j = 0; j < SLIDING_WINDOW_SIZE; j++)
|
||||
{
|
||||
readings[i][j] = j + 1;
|
||||
}
|
||||
}
|
||||
|
||||
// get metrics
|
||||
Metrics metrics = getMetrics(readings, NUMBER_OF_SENSORS, SLIDING_WINDOW_SIZE);
|
||||
|
||||
printf("Metrics:\n");
|
||||
printf("Mean: %f\n", metrics.mean);
|
||||
printf("Standard Deviation: %f\n", metrics.standardDeviation);
|
||||
printf("Possible Faulty Sensor: %d\n", metrics.possibleFaultySensor);
|
||||
|
||||
}
|
||||
|
||||
int main() {
|
||||
int tests_run = 0;
|
||||
int tests_passed = 0;
|
||||
@ -458,6 +482,7 @@ int main() {
|
||||
RUN_TEST(test_overallStandardDeviation_normal);
|
||||
RUN_TEST(test_anomalyDetect_normal);
|
||||
RUN_TEST(test_freeReadings);
|
||||
RUN_TEST(test_getMetrics);
|
||||
|
||||
printf("\n=== Results ===\n");
|
||||
printf("Tests run: %d\n", tests_run);
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user