/*
 * @Author: mypx
 * @Date: 2025-08-15 14:40:15
 * @LastEditTime: 2025-09-19 11:31:11
 * @LastEditors: mypx mypx_coder@163.com
 * @Description: Exponential Moving Average (EMA) filter implementation
 */
#ifndef __ET_EMA_FILTER_H__
#define __ET_EMA_FILTER_H__
#include <stdint.h>
#ifdef __cplusplus
extern "C"
{
#endif

/**
 * @brief EMA filter state structure for real-time processing
 * 
 * This structure maintains the state needed for EMA filtering:
 * - last_output: previous filtered output value
 * - alpha: smoothing factor (0 < alpha < 1)
 * - is_initialized: flag to indicate if filter is initialized
 */
typedef struct
{
    float   last_output;    // previous output value for EMA calculation
    float   alpha;          // smoothing factor, controls filter responsiveness
    uint8_t is_initialized; // initialization flag for first sample handling
} et_ema_filter_t;

/**
 * @brief EMA filter configuration for array processing
 * 
 * This structure contains input/output arrays and configuration
 * for batch processing of data arrays
 */
typedef struct
{
    const float *input;     // pointer to input data array
    float       *output;    // pointer to output data array
    uint32_t     data_size; // number of data points to process
    float        alpha;     // smoothing factor
} et_ema_array_config_t;

/**
 * @brief Initialize EMA filter for real-time processing
 * 
 * @param ema pointer to EMA filter structure
 * @param alpha smoothing factor (0 < alpha < 1)
 * @return 0 on success, -1 on error
 */
int et_ema_filter_init(et_ema_filter_t *ema, float alpha);

/**
 * @brief Apply EMA filter to single input value (real-time)
 * 
 * @param ema pointer to EMA filter structure
 * @param new_input new input value to filter
 * @return filtered output value
 */
float et_ema_filter_process(et_ema_filter_t *ema, float new_input);

/**
 * @brief Reset EMA filter state
 * 
 * @param ema pointer to EMA filter structure
 * @return 0 on success, -1 on error
 */
int et_ema_filter_reset(et_ema_filter_t *ema);

/**
 * @brief Apply EMA filter to array of input data
 * 
 * @param config pointer to array configuration structure
 * @return 0 on success, -1 on error
 */
int et_ema_array_filter_process(const et_ema_array_config_t *config);

#ifdef __cplusplus
}
#endif

#endif // __ET_EXP_MOVING_AVERAGE_FILTER_H__
