Добаботка фильтров:
- в целом улучшена логика хендлов - добавлена фильтрация по таблице - добавлен флаг включенности (в либе пока не используется)
This commit is contained in:
@@ -14,8 +14,8 @@
|
||||
|
||||
Параметры для конфигурации:
|
||||
- @ref FILTERS_ENABLE - Включить библиотеку фильтров
|
||||
- @ref FILTER_MEDIAN_SIZE - Размер окна медианного фильтра (по умолчанию 5)
|
||||
- @ref FILTER_AVERAGE_SIZE - Размер окна усредняющего фильтра (по умолчанию 8)
|
||||
- @ref FILTER_MEDIAN_MAX_SIZE - Размер окна медианного фильтра (по умолчанию 5)
|
||||
- @ref FILTER_AVERAGE_MAX_SIZE - Размер окна усредняющего фильтра (по умолчанию 8)
|
||||
- @ref FILTER_POLY_MAX_ORDER - Максимальный порядок полинома (по умолчанию 4)
|
||||
|
||||
@par Пример использования:
|
||||
@@ -76,53 +76,62 @@ int32_t process_value_int(int32_t raw_adc_quant) {
|
||||
*****************************************************************************/
|
||||
#ifndef __FILTERS_H_
|
||||
#define __FILTERS_H_
|
||||
|
||||
#include "mylibs_defs.h"
|
||||
#include <stdint.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#ifdef FILTERS_ENABLE
|
||||
|
||||
#ifndef FILTER_MEDIAN_SIZE
|
||||
#define FILTER_MEDIAN_SIZE 5 ///< Размер окна медианного фильтра
|
||||
#ifndef FILTER_AVERAGE_MAX_SIZE
|
||||
#define FILTER_AVERAGE_MAX_SIZE 100 ///< Размер окна медианного фильтра
|
||||
#endif
|
||||
|
||||
#ifndef FILTER_AVERAGE_SIZE
|
||||
#define FILTER_AVERAGE_SIZE 8 ///< Размер окна усредняющего фильтра
|
||||
#ifndef FILTER_MEDIAN_MAX_SIZE
|
||||
#define FILTER_MEDIAN_MAX_SIZE 8 ///< Размер окна усредняющего фильтра
|
||||
#endif
|
||||
|
||||
#ifndef FILTER_POLY_MAX_ORDER
|
||||
#define FILTER_POLY_MAX_ORDER 4 ///< Максимальный порядок полинома
|
||||
#define FILTER_POLY_MAX_ORDER 4 ///< Максимальный порядок полинома
|
||||
#endif
|
||||
|
||||
// ==================== FLOAT ВЕРСИИ ====================
|
||||
#define FILTER_GET_STATE(_fltr_) (_fltr_)->state
|
||||
|
||||
// ==================== FLOAT ВЕРСИИ ====================
|
||||
typedef enum
|
||||
{
|
||||
FILTER_DISABLE,
|
||||
FILTER_ENABLE
|
||||
}FilterState_t;
|
||||
/**
|
||||
* @brief Структура медианного фильтра (float)
|
||||
*/
|
||||
typedef struct {
|
||||
float buffer[FILTER_MEDIAN_SIZE]; ///< Буфер значений
|
||||
float buffer[FILTER_MEDIAN_MAX_SIZE]; ///< Буфер значений
|
||||
uint8_t index; ///< Текущий индекс
|
||||
uint8_t size; ///< Размер буфера
|
||||
uint8_t size; ///< Фактический размер фильтра
|
||||
} FilterMedian_t;
|
||||
|
||||
/**
|
||||
* @brief Структура экспоненциального фильтра (float)
|
||||
*/
|
||||
typedef struct {
|
||||
float alpha; ///< Коэффициент сглаживания (0..1)
|
||||
float value; ///< Текущее значение
|
||||
uint8_t initialized; ///< Флаг инициализации
|
||||
FilterState_t state; ///< Состояние фильтра
|
||||
float alpha; ///< Коэффициент сглаживания (0..1)
|
||||
float value; ///< Текущее значение
|
||||
uint8_t initialized; ///< Флаг инициализации
|
||||
} FilterExp_t;
|
||||
|
||||
/**
|
||||
* @brief Структура фильтра скользящего среднего (float)
|
||||
*/
|
||||
typedef struct {
|
||||
float buffer[FILTER_AVERAGE_SIZE]; ///< Буфер значений
|
||||
float sum; ///< Сумма значений
|
||||
uint8_t index; ///< Текущий индекс
|
||||
uint8_t count; ///< Количество элементов
|
||||
FilterState_t state; ///< Состояние фильтра
|
||||
float buffer[FILTER_AVERAGE_MAX_SIZE]; ///< Буфер значений
|
||||
uint8_t size; ///< Фактический размер фильтра
|
||||
float sum; ///< Сумма значений
|
||||
uint8_t index; ///< Текущий индекс
|
||||
uint8_t count; ///< Количество элементов
|
||||
} FilterAverage_t;
|
||||
|
||||
/**
|
||||
@@ -133,65 +142,97 @@ typedef struct {
|
||||
uint8_t order; ///< Порядок полинома
|
||||
} FilterPoly_t;
|
||||
|
||||
/**
|
||||
* @brief Структура табличного фильтра (float)
|
||||
*/
|
||||
typedef struct {
|
||||
FilterState_t state; ///< Состояние фильтра
|
||||
float* input_values; // Массив входных значений
|
||||
float* output_values; // Массив выходных значений
|
||||
uint16_t size; // Размер таблицы
|
||||
uint8_t interpolation; // Флаг интерполяции (0 - отключена, 1 - линейная)
|
||||
} FilterLUT_t;
|
||||
|
||||
// Float версии функций
|
||||
void FilterMedian_Init(FilterMedian_t* filter);
|
||||
void FilterMedian_Init(FilterMedian_t* filter, uint8_t size);
|
||||
float FilterMedian_Process(FilterMedian_t* filter, float input);
|
||||
void FilterExp_Init(FilterExp_t* filter, float alpha);
|
||||
float FilterExp_Process(FilterExp_t* filter, float input);
|
||||
void FilterAverage_Init(FilterAverage_t* filter);
|
||||
void FilterAverage_Init(FilterAverage_t* filter, uint8_t size);
|
||||
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);
|
||||
float FilterLUT_Process(FilterLUT_t* filter, float input);
|
||||
// ==================== INT32_T ВЕРСИИ ====================
|
||||
|
||||
/**
|
||||
* @brief Структура медианного фильтра (int32_t)
|
||||
*/
|
||||
typedef struct {
|
||||
int32_t buffer[FILTER_MEDIAN_SIZE]; ///< Буфер значений
|
||||
uint8_t index; ///< Текущий индекс
|
||||
uint8_t size; ///< Размер буфера
|
||||
FilterState_t state; ///< Состояние фильтра
|
||||
int32_t buffer[FILTER_MEDIAN_MAX_SIZE]; ///< Буфер значений
|
||||
uint8_t index; ///< Текущий индекс
|
||||
uint8_t size; ///< Фактический размер фильтра
|
||||
} FilterMedianInt_t;
|
||||
|
||||
/**
|
||||
* @brief Структура экспоненциального фильтра (int32_t)
|
||||
*/
|
||||
typedef struct {
|
||||
int32_t alpha; ///< Коэффициент сглаживания (в масштабе scale)
|
||||
int32_t value; ///< Текущее значение
|
||||
uint8_t initialized; ///< Флаг инициализации
|
||||
int32_t scale; ///< Масштаб коэффициента (например 100 для 0.01)
|
||||
FilterState_t state; ///< Состояние фильтра
|
||||
int32_t alpha; ///< Коэффициент сглаживания (в масштабе scale)
|
||||
int32_t value; ///< Текущее значение
|
||||
uint8_t initialized; ///< Флаг инициализации
|
||||
int32_t scale; ///< Масштаб коэффициента (например 100 для 0.01)
|
||||
} FilterExpInt_t;
|
||||
|
||||
/**
|
||||
* @brief Структура фильтра скользящего среднего (int32_t)
|
||||
*/
|
||||
typedef struct {
|
||||
int32_t buffer[FILTER_AVERAGE_SIZE]; ///< Буфер значений
|
||||
int64_t sum; ///< Сумма значений
|
||||
uint8_t index; ///< Текущий индекс
|
||||
uint8_t count; ///< Количество элементов
|
||||
FilterState_t state; ///< Состояние фильтра
|
||||
int32_t buffer[FILTER_AVERAGE_MAX_SIZE]; ///< Буфер значений
|
||||
uint8_t size; ///< Фактический размер фильтра
|
||||
int64_t sum; ///< Сумма значений
|
||||
uint8_t index; ///< Текущий индекс
|
||||
uint8_t count; ///< Количество элементов
|
||||
} FilterAverageInt_t;
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* @brief Структура полиномиальной коррекции (int32_t)
|
||||
*/
|
||||
typedef struct {
|
||||
int32_t coefficients[FILTER_POLY_MAX_ORDER + 1]; ///< Коэффициенты полинома
|
||||
uint8_t order; ///< Порядок полинома
|
||||
int32_t scale; ///< Масштаб коэффициентов
|
||||
FilterState_t state; ///< Состояние фильтра
|
||||
int32_t coefficients[FILTER_POLY_MAX_ORDER + 1]; ///< Коэффициенты полинома
|
||||
uint8_t order; ///< Порядок полинома
|
||||
int32_t scale; ///< Масштаб коэффициентов
|
||||
} FilterPolyInt_t;
|
||||
|
||||
/**
|
||||
* @brief Структура табличного фильтра (int32_t)
|
||||
*/
|
||||
typedef struct {
|
||||
FilterState_t state; ///< Состояние фильтра
|
||||
int32_t* input_values; // Массив входных значений
|
||||
int32_t* output_values; // Массив выходных значений
|
||||
uint16_t size; // Размер таблицы
|
||||
uint8_t interpolation; // Флаг интерполяции
|
||||
} FilterLUTInt_t;
|
||||
|
||||
// Int32_t версии функций
|
||||
void FilterMedianInt_Init(FilterMedianInt_t* filter);
|
||||
void 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);
|
||||
int32_t FilterExpInt_Process(FilterExpInt_t* filter, int32_t input);
|
||||
void FilterAverageInt_Init(FilterAverageInt_t* filter);
|
||||
void FilterAverageInt_Init(FilterAverageInt_t* filter, uint8_t size);
|
||||
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);
|
||||
int32_t FilterLUTInt_Process(FilterLUTInt_t* filter, int32_t input);
|
||||
|
||||
#else // FILTERS_ENABLE
|
||||
// Заглушки для float
|
||||
@@ -200,14 +241,16 @@ 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)
|
||||
#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)
|
||||
#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;
|
||||
@@ -215,14 +258,16 @@ 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)
|
||||
#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)
|
||||
#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