Fixed logic of adding values
- Changed logic of addReading() - Fixed methods according to this edit - Added docs
This commit is contained in:
@@ -11,20 +11,56 @@ static int sensorsNumber;
|
||||
static int slidingWindowSize;
|
||||
|
||||
|
||||
/**
|
||||
* @brief Sets the number of sensors.
|
||||
*
|
||||
* This function sets the global variable `sensorsNumber` to the specified value.
|
||||
*
|
||||
* @param number The number of sensors to set.
|
||||
*/
|
||||
static void setSensorsNumber(int number) {
|
||||
sensorsNumber = number;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Sets the size of the sliding window.
|
||||
*
|
||||
* This function sets the size of the sliding window used for data acquisition.
|
||||
*
|
||||
* @param size The desired size of the sliding window.
|
||||
*/
|
||||
static void setSlidingWindowSize(int size) {
|
||||
slidingWindowSize = size;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Initializes the sensor readings array.
|
||||
*
|
||||
* This function allocates memory for a 2D array to store sensor readings.
|
||||
* Each sensor will have a sliding window of readings.
|
||||
*
|
||||
* @param numSensors The number of sensors.
|
||||
* @param windowSize The size of the sliding window for each sensor.
|
||||
*
|
||||
* The function performs the following steps:
|
||||
* 1. Allocates memory for the array of sensor readings.
|
||||
* 2. Allocates memory for each sensor's sliding window of readings.
|
||||
* 3. Sets the number of sensors and the sliding window size using private setter functions.
|
||||
*
|
||||
* If memory allocation fails at any point, the function prints an error message and exits the program.
|
||||
*/
|
||||
void initializeReadings(int numSensors, int windowSize) {
|
||||
|
||||
// Allocate memory for the array of sensor readings
|
||||
readings = (float **)malloc(numSensors * sizeof(float *));
|
||||
|
||||
// Check if memory allocation was successful
|
||||
if (readings == NULL) {
|
||||
perror("Failed to allocate memory for readings");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
// Allocate memory for each sensor's sliding window of readings
|
||||
for (int i = 0; i < numSensors; i++) {
|
||||
readings[i] = (float *)calloc(windowSize, sizeof(float));
|
||||
if (readings[i] == NULL) {
|
||||
@@ -32,11 +68,21 @@ void initializeReadings(int numSensors, int windowSize) {
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
}
|
||||
// Chiamate private ai setter
|
||||
|
||||
// Calling the private setter functions
|
||||
setSensorsNumber(numSensors);
|
||||
setSlidingWindowSize(windowSize);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Frees the memory allocated for sensor readings.
|
||||
*
|
||||
* This function iterates through the array of sensor readings and frees the memory
|
||||
* allocated for each individual reading. After freeing all individual readings, it
|
||||
* checks if the main readings array is not NULL and frees it as well.
|
||||
*
|
||||
* @return true if the main readings array was successfully freed, false otherwise.
|
||||
*/
|
||||
bool freeReadings() {
|
||||
for (int i = 0; i < sensorsNumber; i++) {
|
||||
free(readings[i]);
|
||||
@@ -44,26 +90,49 @@ bool freeReadings() {
|
||||
|
||||
if(readings != NULL){
|
||||
free(readings);
|
||||
readings = NULL;
|
||||
return true;
|
||||
}
|
||||
else{
|
||||
return false;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// Functions
|
||||
|
||||
/**
|
||||
* @brief Get the number of sensors.
|
||||
*
|
||||
* This function returns the total number of sensors currently available.
|
||||
*
|
||||
* @return int The number of sensors.
|
||||
*/
|
||||
// Get the number of sensors
|
||||
int getSensorsNumber() {
|
||||
return sensorsNumber;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Get the sliding window size.
|
||||
*
|
||||
* This function returns the current size of the sliding window used in data acquisition.
|
||||
*
|
||||
* @return The size of the sliding window.
|
||||
*/
|
||||
// Get the sliding window size
|
||||
int getSlidingWindowSize() {
|
||||
return slidingWindowSize;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Checks if the sliding window for a given sensor is full.
|
||||
*
|
||||
* This function iterates through the readings of a specified sensor and
|
||||
* determines if all entries in the sliding window are non-zero, indicating
|
||||
* that the window is full.
|
||||
*
|
||||
* @param sensorIndex The index of the sensor to check.
|
||||
* @return true if the sliding window is full (all entries are non-zero),
|
||||
* false otherwise.
|
||||
*/
|
||||
// Control on the fullness of the sliding window
|
||||
bool isFull(int sensorIndex) {
|
||||
for (int i = 0; i < slidingWindowSize; i++) {
|
||||
@@ -74,6 +143,16 @@ bool isFull(int sensorIndex) {
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Retrieves the last reading from the specified sensor.
|
||||
*
|
||||
* This function returns the most recent reading from the sensor identified by
|
||||
* the given index. If the sensor's data buffer is full, it returns the last
|
||||
* value in the buffer. Otherwise, it returns the first value.
|
||||
*
|
||||
* @param sensorIndex The index of the sensor to retrieve the reading from.
|
||||
* @return The last reading from the specified sensor.
|
||||
*/
|
||||
float getLastReading(int sensorIndex) {
|
||||
if (isFull(sensorIndex)) {
|
||||
return readings[sensorIndex][slidingWindowSize - 1];
|
||||
@@ -82,8 +161,21 @@ float getLastReading(int sensorIndex) {
|
||||
}
|
||||
}
|
||||
|
||||
// Add a reading to the readings array
|
||||
void addReading(float value, int sensorIndex) {
|
||||
static int lastSensorIndex = -1;
|
||||
|
||||
/**
|
||||
* @brief Adds a new sensor reading to the data acquisition system.
|
||||
*
|
||||
* This function updates the readings for the sensors in a circular buffer manner.
|
||||
* If the buffer for a sensor is full, it shifts the readings to make space for the new value.
|
||||
* If the buffer is not full, it adds the new value to the first available position.
|
||||
*
|
||||
* @param value The new sensor reading to be added.
|
||||
*/
|
||||
void addReading(float value) {
|
||||
lastSensorIndex = (lastSensorIndex + 1) % sensorsNumber;
|
||||
int sensorIndex = lastSensorIndex;
|
||||
|
||||
if (isFull(sensorIndex)) {
|
||||
for (int i = 0; i < slidingWindowSize - 1; i++) {
|
||||
readings[sensorIndex][i] = readings[sensorIndex][i + 1];
|
||||
@@ -93,15 +185,22 @@ void addReading(float value, int sensorIndex) {
|
||||
for (int i = 0; i < slidingWindowSize; i++) {
|
||||
if (readings[sensorIndex][i] == 0) {
|
||||
readings[sensorIndex][i] = value;
|
||||
// Update the position index
|
||||
|
||||
break;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Calculates the average reading for a specified sensor.
|
||||
*
|
||||
* This function computes the average of the readings stored in a sliding window
|
||||
* for the sensor specified by the sensorIndex parameter. If the sliding window
|
||||
* is not full, the function prints a message and returns 0.
|
||||
*
|
||||
* @param sensorIndex The index of the sensor for which the average reading is to be calculated.
|
||||
* @return The average reading of the specified sensor if the sliding window is full; otherwise, returns 0.
|
||||
*/
|
||||
float getAverageOnSensor(int sensorIndex) {
|
||||
if(isFull(sensorIndex) == false){
|
||||
printf("The sliding window is not full\n");
|
||||
@@ -116,6 +215,14 @@ float getAverageOnSensor(int sensorIndex) {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Calculates the average reading from all sensors.
|
||||
*
|
||||
* This function iterates through all available sensors, retrieves the last reading
|
||||
* from each sensor, and calculates the average of these readings.
|
||||
*
|
||||
* @return The average reading from all sensors as a float.
|
||||
*/
|
||||
float getAverageOnAllSensors() {
|
||||
float sum = 0;
|
||||
for (int i = 0; i < sensorsNumber; i++) {
|
||||
@@ -124,6 +231,18 @@ float getAverageOnAllSensors() {
|
||||
return sum / sensorsNumber;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Calculates the overall average of sensor readings.
|
||||
*
|
||||
* This function iterates through all sensors and their respective sliding windows
|
||||
* to calculate the overall average of the readings. It first checks if the sliding
|
||||
* window for each sensor is full. If any sliding window is not full, it prints a
|
||||
* message indicating which sensor's sliding window is not full and returns 0.
|
||||
* Otherwise, it sums up all the readings from all sensors and calculates the average.
|
||||
*
|
||||
* @return The overall average of the sensor readings if all sliding windows are full,
|
||||
* otherwise returns 0.
|
||||
*/
|
||||
float getOverallAverage() {
|
||||
float sum = 0;
|
||||
int totalReadings = 0;
|
||||
@@ -140,6 +259,18 @@ float getOverallAverage() {
|
||||
return sum / totalReadings;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Calculates the standard deviation of sensor readings.
|
||||
*
|
||||
* This function computes the standard deviation of the readings from a specified sensor.
|
||||
* It first checks if the sliding window for the sensor is full. If not, it prints a message
|
||||
* and returns 0. If the sliding window is full, it calculates the average of the readings,
|
||||
* then computes the sum of the squared differences between each reading and the average.
|
||||
* Finally, it returns the square root of the average of these squared differences.
|
||||
*
|
||||
* @param sensorIndex The index of the sensor for which the standard deviation is to be calculated.
|
||||
* @return The standard deviation of the sensor readings, or 0 if the sliding window is not full.
|
||||
*/
|
||||
float getStandardDeviationOnSensor(int sensorIndex) {
|
||||
if(isFull(sensorIndex) == false){
|
||||
printf("The sliding window is not full\n");
|
||||
@@ -155,6 +286,16 @@ float getStandardDeviationOnSensor(int sensorIndex) {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Calculates the standard deviation of the readings from all sensors.
|
||||
*
|
||||
* This function computes the standard deviation of the sensor readings by first
|
||||
* calculating the average of all sensor readings, then summing the squared
|
||||
* differences between each reading and the average, and finally taking the
|
||||
* square root of the average of these squared differences.
|
||||
*
|
||||
* @return The standard deviation of the sensor readings.
|
||||
*/
|
||||
float getStandardDeviationOnAllSensors() {
|
||||
float sum = 0;
|
||||
float average = getAverageOnAllSensors();
|
||||
@@ -166,12 +307,27 @@ float getStandardDeviationOnAllSensors() {
|
||||
return sqrt(sum / sensorsNumber);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Detects anomalies in sensor readings based on a given average and standard deviation.
|
||||
*
|
||||
* This function calculates the upper and lower thresholds for anomaly detection using a 97% confidence interval.
|
||||
* It then iterates through the sensor readings within a sliding window and counts the number of outliers that fall
|
||||
* outside the calculated thresholds.
|
||||
*
|
||||
* @param average The average value of the sensor readings.
|
||||
* @param standardDeviation The standard deviation of the sensor readings.
|
||||
*
|
||||
* @note The function uses a confidence interval multiplier of 2.17 to determine the thresholds.
|
||||
* @note The variable `outlierCount` is used to store the number of detected outliers.
|
||||
* @note The variables `slidingWindowSize` and `sensorsNumber` are assumed to be defined globally.
|
||||
* @note The 2D array `readings` is assumed to contain the sensor data.
|
||||
*/
|
||||
void anomalyDetect(float average, float standardDeviation) {
|
||||
float upperThreshold = average + 2.17 * standardDeviation; // 97% confidence interval
|
||||
float lowerThreshold = average - 2.17 * standardDeviation; // Lower bound for anomaly detection
|
||||
outlierCount = 0;
|
||||
for (int i = 0; i < sensorsNumber; i++) {
|
||||
for (int j = 0; j < slidingWindowSize; j++) {
|
||||
for (int j = 0; j < slidingWindowSize; j++) {
|
||||
for (int i = 0; i < sensorsNumber; i++) {
|
||||
if (readings[i][j] > upperThreshold || readings[i][j] < lowerThreshold) {
|
||||
outlierCount++;
|
||||
}
|
||||
@@ -180,6 +336,17 @@ void anomalyDetect(float average, float standardDeviation) {
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Calculates the overall standard deviation of sensor readings.
|
||||
*
|
||||
* This function computes the overall standard deviation of the readings from multiple sensors.
|
||||
* It first checks if the sliding window for each sensor is full. If any sensor's sliding window
|
||||
* is not full, it prints a message and returns 0. Otherwise, it calculates the sum of the squared
|
||||
* differences between each reading and the overall average, and then computes the standard deviation.
|
||||
* The function also calls `anomalyDetect` with the calculated average and standard deviation.
|
||||
*
|
||||
* @return The overall standard deviation of the sensor readings. Returns 0 if any sensor's sliding window is not full.
|
||||
*/
|
||||
float getOverallStandardDeviation() {
|
||||
float sum = 0;
|
||||
int totalReadings = 0;
|
||||
@@ -202,6 +369,14 @@ float getOverallStandardDeviation() {
|
||||
return totalStandardDeviation;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Retrieves the current count of outliers.
|
||||
*
|
||||
* This function returns the number of outliers detected and stored in the
|
||||
* variable `outlierCount`.
|
||||
*
|
||||
* @return The number of outliers.
|
||||
*/
|
||||
int getOutlierCount() {
|
||||
return outlierCount;
|
||||
}
|
||||
Reference in New Issue
Block a user