Implemented getMetrics()

This commit is contained in:
2024-12-03 12:31:45 +01:00
parent c0bb5c46ef
commit 23951656c1
4 changed files with 146 additions and 4 deletions

View File

@@ -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;
}