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:
Warning
doxygenfile: Cannot find file “rs_filter_moving_avg.h