Filter Moving Average
This is the moving average filter implementation with a fixed size of user initialization.
The main buffer structure variable and the internal data buffer both should be declared on the application side, and the pointers to the variables are used in the module.
The average of the given elements is calculated by this filter, and even the average can be calculated as per the given size.
Usage
All API functions have the first argument as the moving average filter structure.
Internal data buffer size and pointer are initialized with module init function.
Other API functions to get result,sum and available data and to flush the elements in the filter
Return codes of API functions are defined in header file.
Function |
Description |
---|---|
rs_filter_moving_avg_init |
Function to initialize the filter moving average |
rs_filter_moving_avg_add_input |
Function to add the input and find the average based on window size |
rs_filter_moving_avg_get_average |
Function to get average value |
rs_filter_moving_avg_get_data |
Function to get data of averaged values |
rs_filter_moving_avg_flush |
Function to reset the elements of the filter structure |
Using these ‘rs’ calls to initialize the filter moving average of the window size and input is added, calculation and averages get retrieved from the structure, data is obtained from the structure, and all filter structure elements are flushed.
Feature Supported
The RAPIDSEA Filter Moving Average module provides two methods for smoothing data:
Sliding Window Moving Average - A traditional moving average computed over a fixed-size window.
Exponential Weighted Moving Average (EWMA) - A weighted moving average that gives more importance to recent values.
See also
- Macro’s are
RS_FILTER_MA_TYPE_SLIDING 1
RS_FILTER_MA_TYPE_EXPONENTIAL 2
Sliding Window Moving Average
The moving average is calculated over a fixed-size window defined by the user during initialization.
A circular buffer is used to store the last N values, where N is the window size.
The sum of the elements in the buffer is maintained, allowing efficient computation of the moving average.
When a new value is added, the oldest value is removed, and the new value is included in the sum to compute the updated average.
Window Size (N): The user must define the window size during initialization, which determines how many past values contribute to the average.
This method provides a stable, consistent smoothing effect but does not adapt dynamically to changing trends in data.
Exponential Weigting Method
The value of the forgetting factor determines the rate of change of the weighting factors. A forgetting factor of 0.9 gives more weight to the older data than does a forgetting factor of 0.1. To give more weight to the recent data, move the forgetting factor closer to 0. For detecting small shifts in rapidly varying data, a smaller value (below 0.5) is more suitable. A forgetting factor of 1.0 indicates infinite memory. All the previous samples are given an equal weight.
This method assigns exponentially decreasing weights to older values, making recent values more influential.
The formula used for updating the EMA is:
\[EMA\_t = \alpha \times X\_t + (1 - \alpha) \times EMA\_{t-1}\]where (alpha) (smoothing factor) is computed as:
\[\alpha = \frac{2}{N+1}\]where (N) is the effective window size.
Unlike the sliding window method, EWMA does not discard older data completely but rather reduces its influence over time.
Forgetting Factor (**(1 - alpha)****)**: The forgetting factor controls how much influence past values retain. A higher (alpha) gives more weight to recent values, while a lower (alpha) results in a smoother trend.
EWMA is particularly useful in applications where more recent data should have a stronger impact on the moving average calculation.
Error Code
Every API’s for the filter moving average module returns some success or failure values. Please refer below section,
Example Demo
Please refer below section,
Filter Moving Average Header Details
Documentation from the relevant header as follows:
Moving average filter Specific APIs.
Defines macros and functions specific to Filter moving average functionality
- Author
Embien RAPIDSEA Team
- Copyright
Embien Technologies India Pvt. Ltd.
Typedefs
-
typedef struct tag_rs_filter_moving_avg rs_filter_moving_avg_t
Structure contains filter moving average.
Functions
-
rs_ret_val_t rs_filter_moving_avg_init(rs_filter_moving_avg_t *ptr_filter_ma, uint8_t filter_type, float *ptr_data, uint32_t u32_window_size, float f_forgetting_factor)
Filter initialization.
This function initialize the filter
- Parameters:
ptr_filter_ma – [in] - pointer to filter
filter_type – [in] - Type of the filter method
ptr_data – [in] - pointer to data
u32_window_size – [in] - Size of the window
f_forgetting_factor – [in] - Exponential weighting factor
- Returns:
0 on success or error code on failure
-
rs_ret_val_t rs_filter_moving_avg_add_input(rs_filter_moving_avg_t *ptr_filter_ma, float f_input)
Add input to filter.
This function adds input to filter
- Parameters:
ptr_filter_ma – [in] - pointer to filter
f_input – [in] - input to filter
- Returns:
0 on success or error code on failure
-
float rs_filter_moving_avg_get_average(rs_filter_moving_avg_t *ptr_filter_ma)
Get average of the filter.
This function gets the average of the filter
- Parameters:
ptr_filter_ma – [in] - pointer to filter
- Returns:
Average value on success or error code on failure
-
rs_ret_val_t rs_filter_moving_avg_get_data(rs_filter_moving_avg_t *ptr_filter_ma, float *ptr_data, uint32_t u32_max_data_count)
Get data of the filter.
This function gets the data of the filter
- Parameters:
ptr_filter_ma – [in] - pointer to filter
ptr_data – [in] - pointer to data
u32_max_data_count – [in] - Maximum data count
- Returns:
0 on success or error code on failure
-
rs_ret_val_t rs_filter_moving_avg_flush(rs_filter_moving_avg_t *ptr_filter_ma)
Flush all the element in the filter.
This function flushes all the elements in the filter
- Parameters:
ptr_filter_ma – [in] - pointer to filter
- Returns:
0 on success or error code on failure
-
struct tag_rs_filter_moving_avg
- #include <rs_filter_moving_avg.h>
Structure contains filter moving average.