|
Envorimental Monitoring
|
Sliding-window median filter for floating-point sensor values. More...
#include "stdint.h"Go to the source code of this file.
Data Structures | |
| struct | Filter_Handle_t |
| Internal state of one sliding-window median filter instance. More... | |
Macros | |
| #define | MAX_WINDOW_SIZE 20 |
| Maximum number of samples the filter window can hold. | |
Functions | |
| float | filter_sensor_value (Filter_Handle_t *p_filt, float raw_sensor_value, uint8_t window_size) |
| Applies a sliding-window median filter to a single raw sample. | |
| float | calculate_median (float *array, uint8_t array_size) |
| Computes the median of a pre-sorted float array. | |
| void | bubble_sort (float *array, uint8_t array_size) |
| Sorts a float array in ascending order using bubble sort. | |
Sliding-window median filter for floating-point sensor values.
The filter maintains a fixed-size window of the most recent raw readings. Each call to filter_sensor_value() appends the new sample to the window (evicting the oldest one when full), sorts a copy of the window, and returns the median. This effectively suppresses impulse noise while preserving step changes faster than a moving-average filter of the same length.
Utility functions bubble_sort() and calculate_median() are also exposed so that calculate_statistics() in main.c can reuse them on the ring buffer.
| void bubble_sort | ( | float * | array, |
| uint8_t | array_size ) |
Sorts a float array in ascending order using bubble sort.
Runs in O(n²) time; suitable for the small window sizes used here (≤ MAX_WINDOW_SIZE = 20 elements).
| [in,out] | array | Pointer to the array to sort in place. |
| [in] | array_size | Number of elements in array. |
| float calculate_median | ( | float * | array, |
| uint8_t | array_size ) |
Computes the median of a pre-sorted float array.
For even-length arrays the median is the average of the two central elements. For odd-length arrays it is the single central element.
| [in] | array | Pointer to a sorted array of floats. |
| [in] | array_size | Number of elements in array. |
| float filter_sensor_value | ( | Filter_Handle_t * | p_filt, |
| float | raw_sensor_value, | ||
| uint8_t | window_size ) |
Applies a sliding-window median filter to a single raw sample.
On each call the new sample is inserted into the window. If the window has not yet reached window_size the sample is appended; otherwise the oldest sample is discarded (left-shift) and the new one placed at the end. A copy of the window is then sorted and the median returned.
| [in,out] | p_filt | Pointer to the filter state structure. |
| [in] | raw_sensor_value | The latest raw measurement from the sensor. |
| [in] | window_size | Effective window length (must be ≤ MAX_WINDOW_SIZE). |