Envorimental Monitoring
Loading...
Searching...
No Matches
filter.c File Reference

Sliding-window median filter implementation. More...

#include "filter.h"

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.
void bubble_sort (float *array, uint8_t array_size)
 Sorts a float array in ascending order using bubble sort.
float calculate_median (float *array, uint8_t array_size)
 Computes the median of a pre-sorted float array.

Detailed Description

Sliding-window median filter implementation.

Author
Şule Nur Demirdaş
Date
April 2026

Function Documentation

◆ bubble_sort()

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).

Parameters
[in,out]arrayPointer to the array to sort in place.
[in]array_sizeNumber of elements in array.

◆ calculate_median()

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.

Parameters
[in]arrayPointer to a sorted array of floats.
[in]array_sizeNumber of elements in array.
Returns
Median value.

◆ filter_sensor_value()

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.

Parameters
[in,out]p_filtPointer to the filter state structure.
[in]raw_sensor_valueThe latest raw measurement from the sensor.
[in]window_sizeEffective window length (must be ≤ MAX_WINDOW_SIZE).
Returns
Filtered (median) value of the current window contents.