Доработки фильтров:
- универсализированы проверки состояния инициализации фильтров - avg сделан скользящий и обычный - плюс еще что-то наверное
This commit is contained in:
@@ -11,11 +11,12 @@
|
||||
- Экспоненциальное скользящее среднее (float и int32_t)
|
||||
- Скользящее среднее арифметическое (float и int32_t)
|
||||
- Полиномиальную коррекцию (float и int32_t)
|
||||
- Табличный фильтр LUT (Look-Up Table) (float и int32_t)
|
||||
|
||||
Параметры для конфигурации:
|
||||
- @ref FILTERS_ENABLE - Включить библиотеку фильтров
|
||||
- @ref FILTER_MEDIAN_MAX_SIZE - Размер окна медианного фильтра (по умолчанию 5)
|
||||
- @ref FILTER_AVERAGE_MAX_SIZE - Размер окна усредняющего фильтра (по умолчанию 8)
|
||||
- @ref FILTER_MEDIAN_MAX_SIZE - Размер окна медианного фильтра (по умолчанию 5)
|
||||
- @ref FILTER_AVERAGE_MAX_SIZE - Размер окна усредняющего фильтра (по умолчанию 8)
|
||||
- @ref FILTER_POLY_MAX_ORDER - Максимальный порядок полинома (по умолчанию 4)
|
||||
|
||||
@par Пример использования:
|
||||
@@ -84,32 +85,65 @@ int32_t process_value_int(int32_t raw_adc_quant) {
|
||||
#ifdef FILTERS_ENABLE
|
||||
|
||||
#ifndef FILTER_AVERAGE_MAX_SIZE
|
||||
#define FILTER_AVERAGE_MAX_SIZE 100 ///< Размер окна медианного фильтра
|
||||
#define FILTER_AVERAGE_MAX_SIZE 100 ///< Размер окна усредняющего фильтра
|
||||
#endif
|
||||
|
||||
#ifndef FILTER_MEDIAN_MAX_SIZE
|
||||
#define FILTER_MEDIAN_MAX_SIZE 8 ///< Размер окна усредняющего фильтра
|
||||
#define FILTER_MEDIAN_MAX_SIZE 10 ///< Размер окна медианного фильтра
|
||||
#endif
|
||||
|
||||
#ifndef FILTER_POLY_MAX_ORDER
|
||||
#define FILTER_POLY_MAX_ORDER 4 ///< Максимальный порядок полинома
|
||||
#endif
|
||||
|
||||
#define FILTER_GET_STATE(_fltr_) (_fltr_)->state
|
||||
|
||||
// ==================== FLOAT ВЕРСИИ ====================
|
||||
/**
|
||||
* @brief Запуск фильтра
|
||||
* @param filter Указатель на структуру фильтра
|
||||
* @details Запускает фильтр только если он в состоянии готовности.
|
||||
* Если он не инициализирован или уже запущен - ничего не делается
|
||||
*/
|
||||
#define Filter_Start(_fltr_) \
|
||||
do{ if(Filter_isReady(_fltr_)) (_fltr_)->state = FILTER_ENABLE; }while(0);
|
||||
|
||||
/**
|
||||
* @brief Остановка работы фильтра
|
||||
* @param filter Указатель на структуру фильтра
|
||||
* @details Останавливет фильтр только если он запущен @ref Filter_Start.
|
||||
* Если он не инициализирован или уже остановлен - ничего не делается
|
||||
*/
|
||||
#define Filter_Stop(_fltr_) \
|
||||
do{ if(Filter_isStart(_fltr_)) (_fltr_)->state = FILTER_READY; }while(0);
|
||||
|
||||
#define Filter_GetState(_fltr_) (_fltr_)->state
|
||||
#define Filter_isInit(_fltr_) !(Filter_GetState(_fltr_) == FILTER_NOT_INIT)
|
||||
#define Filter_isReady(_fltr_) (Filter_GetState(_fltr_) == FILTER_READY)
|
||||
#define Filter_isStart(_fltr_) (Filter_GetState(_fltr_) == FILTER_ENABLE)
|
||||
|
||||
typedef enum
|
||||
{
|
||||
FILTER_DISABLE,
|
||||
FILTER_NOT_INIT,
|
||||
FILTER_READY,
|
||||
FILTER_ENABLE
|
||||
}FilterState_t;
|
||||
|
||||
typedef enum
|
||||
{
|
||||
FILTER_MODE_DEFAULT = 0,
|
||||
FILTER_MODE_MOVING,
|
||||
} FilterMode_t;
|
||||
|
||||
|
||||
// ==================== FLOAT ВЕРСИИ ====================
|
||||
|
||||
/**
|
||||
* @brief Структура медианного фильтра (float)
|
||||
*/
|
||||
typedef struct {
|
||||
float buffer[FILTER_MEDIAN_MAX_SIZE]; ///< Буфер значений
|
||||
uint8_t index; ///< Текущий индекс
|
||||
uint8_t size; ///< Фактический размер фильтра
|
||||
FilterState_t state; ///< Состояние фильтра
|
||||
float buffer[FILTER_MEDIAN_MAX_SIZE]; ///< Буфер значений
|
||||
uint8_t index; ///< Текущий индекс
|
||||
uint8_t size; ///< Фактический размер фильтра
|
||||
} FilterMedian_t;
|
||||
|
||||
/**
|
||||
@@ -127,19 +161,22 @@ typedef struct {
|
||||
*/
|
||||
typedef struct {
|
||||
FilterState_t state; ///< Состояние фильтра
|
||||
FilterMode_t mode; ///< Режим фильтра
|
||||
float buffer[FILTER_AVERAGE_MAX_SIZE]; ///< Буфер значений
|
||||
uint8_t size; ///< Фактический размер фильтра
|
||||
float sum; ///< Сумма значений
|
||||
uint8_t index; ///< Текущий индекс
|
||||
uint8_t count; ///< Количество элементов
|
||||
float lastValue; ///< Последнее измеренное значение
|
||||
} FilterAverage_t;
|
||||
|
||||
/**
|
||||
* @brief Структура полиномиальной коррекции (float)
|
||||
*/
|
||||
typedef struct {
|
||||
float coefficients[FILTER_POLY_MAX_ORDER + 1]; ///< Коэффициенты полинома
|
||||
uint8_t order; ///< Порядок полинома
|
||||
FilterState_t state; ///< Состояние фильтра
|
||||
float coefficients[FILTER_POLY_MAX_ORDER + 1]; ///< Коэффициенты полинома
|
||||
uint8_t order; ///< Порядок полинома
|
||||
} FilterPoly_t;
|
||||
|
||||
/**
|
||||
@@ -154,15 +191,15 @@ typedef struct {
|
||||
} FilterLUT_t;
|
||||
|
||||
// Float версии функций
|
||||
void FilterMedian_Init(FilterMedian_t* filter, uint8_t size);
|
||||
int FilterMedian_Init(FilterMedian_t* filter, uint8_t size);
|
||||
float FilterMedian_Process(FilterMedian_t* filter, float input);
|
||||
void FilterExp_Init(FilterExp_t* filter, float alpha);
|
||||
int FilterExp_Init(FilterExp_t* filter, float alpha);
|
||||
float FilterExp_Process(FilterExp_t* filter, float input);
|
||||
void FilterAverage_Init(FilterAverage_t* filter, uint8_t size);
|
||||
int FilterAverage_Init(FilterAverage_t* filter, uint8_t size, FilterMode_t mode);
|
||||
float FilterAverage_Process(FilterAverage_t* filter, float input);
|
||||
int FilterPoly_Init(FilterPoly_t* filter, float* coeffs, uint8_t order);
|
||||
float FilterPoly_Process(FilterPoly_t* filter, float input);
|
||||
void FilterLUT_Init(FilterLUT_t* filter, float* input_arr, float* output_arr, uint16_t size, uint8_t interpolation);
|
||||
int FilterLUT_Init(FilterLUT_t* filter, float* input_arr, float* output_arr, uint16_t size, uint8_t interpolation);
|
||||
float FilterLUT_Process(FilterLUT_t* filter, float input);
|
||||
// ==================== INT32_T ВЕРСИИ ====================
|
||||
|
||||
@@ -191,12 +228,14 @@ typedef struct {
|
||||
* @brief Структура фильтра скользящего среднего (int32_t)
|
||||
*/
|
||||
typedef struct {
|
||||
FilterState_t state; ///< Состояние фильтра
|
||||
FilterState_t state; ///< Состояние фильтра
|
||||
FilterMode_t mode; ///< Режим фильтра
|
||||
int32_t buffer[FILTER_AVERAGE_MAX_SIZE]; ///< Буфер значений
|
||||
uint8_t size; ///< Фактический размер фильтра
|
||||
int64_t sum; ///< Сумма значений
|
||||
uint8_t index; ///< Текущий индекс
|
||||
uint8_t count; ///< Количество элементов
|
||||
uint32_t lastValue; ///< Последнее измеренное значение
|
||||
} FilterAverageInt_t;
|
||||
|
||||
|
||||
@@ -223,51 +262,18 @@ typedef struct {
|
||||
} FilterLUTInt_t;
|
||||
|
||||
// Int32_t версии функций
|
||||
void FilterMedianInt_Init(FilterMedianInt_t* filter, uint8_t size);
|
||||
int FilterMedianInt_Init(FilterMedianInt_t* filter, uint8_t size);
|
||||
int32_t FilterMedianInt_Process(FilterMedianInt_t* filter, int32_t input);
|
||||
void FilterExpInt_Init(FilterExpInt_t* filter, int32_t alpha, int32_t scale);
|
||||
int FilterExpInt_Init(FilterExpInt_t* filter, int32_t alpha, int32_t scale);
|
||||
int32_t FilterExpInt_Process(FilterExpInt_t* filter, int32_t input);
|
||||
void FilterAverageInt_Init(FilterAverageInt_t* filter, uint8_t size);
|
||||
int FilterAverageInt_Init(FilterAverageInt_t* filter, uint8_t size, FilterMode_t mode);
|
||||
int32_t FilterAverageInt_Process(FilterAverageInt_t* filter, int32_t input);
|
||||
int FilterPolyInt_Init(FilterPolyInt_t* filter, int32_t* coeffs, uint8_t order, int32_t scale);
|
||||
int32_t FilterPolyInt_Process(FilterPolyInt_t* filter, int32_t input);
|
||||
void FilterLUTInt_Init(FilterLUTInt_t* filter, int32_t* input_arr, int32_t* output_arr, uint16_t size, uint8_t interpolation);
|
||||
int FilterLUTInt_Init(FilterLUTInt_t* filter, int32_t* input_arr, int32_t* output_arr, uint16_t size, uint8_t interpolation);
|
||||
int32_t FilterLUTInt_Process(FilterLUTInt_t* filter, int32_t input);
|
||||
|
||||
#else // FILTERS_ENABLE
|
||||
// Заглушки для float
|
||||
typedef struct { uint8_t dummy; } FilterMedian_t;
|
||||
typedef struct { uint8_t dummy; } FilterExp_t;
|
||||
typedef struct { uint8_t dummy; } FilterAverage_t;
|
||||
typedef struct { uint8_t dummy; } FilterPoly_t;
|
||||
|
||||
#define FilterMedian_Init(filter, size)
|
||||
#define FilterMedian_Process(filter, input) (input)
|
||||
#define FilterExp_Init(filter, alpha)
|
||||
#define FilterExp_Process(filter, input) (input)
|
||||
#define FilterAverage_Init(filter, size)
|
||||
#define FilterAverage_Process(filter, input) (input)
|
||||
#define FilterPoly_Init(filter, coeffs, order) (0)
|
||||
#define FilterPoly_Process(filter, input) (input)
|
||||
#define FilterLUT_Init(filter, coeffs, order) (0)
|
||||
#define FilterLUT_Process(filter, input) (input)
|
||||
|
||||
// Заглушки для int32_t
|
||||
typedef struct { uint8_t dummy; } FilterMedianInt_t;
|
||||
typedef struct { uint8_t dummy; } FilterExpInt_t;
|
||||
typedef struct { uint8_t dummy; } FilterAverageInt_t;
|
||||
typedef struct { uint8_t dummy; } FilterPolyInt_t;
|
||||
|
||||
#define FilterMedianInt_Init(filter, size)
|
||||
#define FilterMedianInt_Process(filter, input) (input)
|
||||
#define FilterExpInt_Init(filter, alpha, scale)
|
||||
#define FilterExpInt_Process(filter, input) (input)
|
||||
#define FilterAverageInt_Init(filter, size)
|
||||
#define FilterAverageInt_Process(filter, input) (input)
|
||||
#define FilterPolyInt_Init(filter, coeffs, order, scale) (0)
|
||||
#define FilterPolyInt_Process(filter, input) (input)
|
||||
#define FilterLUTInt_Init(filter, coeffs, order) (0)
|
||||
#define FilterLUTInt_Process(filter, input) (input)
|
||||
|
||||
#endif // FILTERS_ENABLE
|
||||
|
||||
|
||||
Reference in New Issue
Block a user