Временная оптимизация фильтра. Но вообще надо переделать их
This commit is contained in:
parent
99652a9ad5
commit
9bff9ad44d
@ -91,7 +91,11 @@ int32_t process_value_int(int32_t raw_adc_quant) {
|
|||||||
|
|
||||||
|
|
||||||
#ifndef FILTER_AVERAGE_MAX_SIZE
|
#ifndef FILTER_AVERAGE_MAX_SIZE
|
||||||
|
#ifndef FILTERS_DISABLE_MOVING_AVERAGE
|
||||||
#define FILTER_AVERAGE_MAX_SIZE 100 ///< Размер окна усредняющего фильтра
|
#define FILTER_AVERAGE_MAX_SIZE 100 ///< Размер окна усредняющего фильтра
|
||||||
|
#else
|
||||||
|
#define FILTER_AVERAGE_MAX_SIZE 100000 ///< Размер окна усредняющего фильтра без буфера
|
||||||
|
#endif
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#ifndef FILTER_MEDIAN_MAX_SIZE
|
#ifndef FILTER_MEDIAN_MAX_SIZE
|
||||||
@ -125,7 +129,7 @@ int32_t process_value_int(int32_t raw_adc_quant) {
|
|||||||
* @param filter Указатель на структуру фильтра
|
* @param filter Указатель на структуру фильтра
|
||||||
* @details Запускает функцию инициализации, если указатель инициализирован
|
* @details Запускает функцию инициализации, если указатель инициализирован
|
||||||
*/
|
*/
|
||||||
#define Filter_Reset(_fltr_, _input_) \
|
#define Filter_ReInit(_fltr_, _input_) \
|
||||||
((_fltr_)->reset != NULL) ? (_fltr_)->reset(_fltr_, _input_): -1
|
((_fltr_)->reset != NULL) ? (_fltr_)->reset(_fltr_, _input_): -1
|
||||||
|
|
||||||
|
|
||||||
@ -155,11 +159,14 @@ do{ if(Filter_isReady(_fltr_)) (_fltr_)->state = FILTER_ENABLE; }while(0)
|
|||||||
#define Filter_Stop(_fltr_) \
|
#define Filter_Stop(_fltr_) \
|
||||||
do{ if(Filter_isEnable(_fltr_)) (_fltr_)->state = FILTER_READY; }while(0)
|
do{ if(Filter_isEnable(_fltr_)) (_fltr_)->state = FILTER_READY; }while(0)
|
||||||
|
|
||||||
|
#define Filter_isDataReady(_fltr_) ((_fltr_)->dataProcessing == 0)
|
||||||
|
|
||||||
#define Filter_GetState(_fltr_) (_fltr_)->state
|
#define Filter_GetState(_fltr_) (_fltr_)->state
|
||||||
#define Filter_isInit(_fltr_) !(Filter_GetState(_fltr_) == FILTER_NOT_INIT)
|
#define Filter_isInit(_fltr_) !(Filter_GetState(_fltr_) == FILTER_NOT_INIT)
|
||||||
#define Filter_isReady(_fltr_) (Filter_GetState(_fltr_) == FILTER_READY)
|
#define Filter_isReady(_fltr_) (Filter_GetState(_fltr_) == FILTER_READY)
|
||||||
#define Filter_isEnable(_fltr_) (Filter_GetState(_fltr_) == FILTER_ENABLE)
|
#define Filter_isEnable(_fltr_) (Filter_GetState(_fltr_) == FILTER_ENABLE)
|
||||||
|
|
||||||
|
|
||||||
typedef enum
|
typedef enum
|
||||||
{
|
{
|
||||||
FILTER_NOT_INIT,
|
FILTER_NOT_INIT,
|
||||||
@ -184,6 +191,7 @@ typedef struct _FilterMedian_t{
|
|||||||
float buffer[FILTER_MEDIAN_MAX_SIZE]; ///< Буфер значений
|
float buffer[FILTER_MEDIAN_MAX_SIZE]; ///< Буфер значений
|
||||||
uint8_t index; ///< Текущий индекс
|
uint8_t index; ///< Текущий индекс
|
||||||
uint8_t size; ///< Фактический размер фильтра
|
uint8_t size; ///< Фактический размер фильтра
|
||||||
|
uint8_t dataProcessing; ///< Флаг - данные в обработке
|
||||||
|
|
||||||
int (*reset)(struct _FilterMedian_t *filter, uint8_t size);
|
int (*reset)(struct _FilterMedian_t *filter, uint8_t size);
|
||||||
float (*process)(struct _FilterMedian_t *filter, float input);
|
float (*process)(struct _FilterMedian_t *filter, float input);
|
||||||
@ -197,6 +205,7 @@ typedef struct _FilterExp_t {
|
|||||||
float alpha; ///< Коэффициент сглаживания (0..1)
|
float alpha; ///< Коэффициент сглаживания (0..1)
|
||||||
float value; ///< Текущее значение
|
float value; ///< Текущее значение
|
||||||
uint8_t initialized; ///< Флаг инициализации
|
uint8_t initialized; ///< Флаг инициализации
|
||||||
|
uint8_t dataProcessing; ///< Флаг - данные в обработке
|
||||||
|
|
||||||
int (*reset)(struct _FilterExp_t *filter, float alpha);
|
int (*reset)(struct _FilterExp_t *filter, float alpha);
|
||||||
float (*process)(struct _FilterExp_t *filter, float input);
|
float (*process)(struct _FilterExp_t *filter, float input);
|
||||||
@ -208,14 +217,17 @@ typedef struct _FilterExp_t {
|
|||||||
typedef struct _FilterAverage_t{
|
typedef struct _FilterAverage_t{
|
||||||
FilterState_t state; ///< Состояние фильтра
|
FilterState_t state; ///< Состояние фильтра
|
||||||
FilterMode_t mode; ///< Режим фильтра
|
FilterMode_t mode; ///< Режим фильтра
|
||||||
|
#ifndef FILTERS_DISABLE_MOVING_AVERAGE
|
||||||
float buffer[FILTER_AVERAGE_MAX_SIZE]; ///< Буфер значений
|
float buffer[FILTER_AVERAGE_MAX_SIZE]; ///< Буфер значений
|
||||||
uint8_t size; ///< Фактический размер фильтра
|
#endif
|
||||||
|
uint32_t size; ///< Фактический размер фильтра
|
||||||
float sum; ///< Сумма значений
|
float sum; ///< Сумма значений
|
||||||
uint8_t index; ///< Текущий индекс
|
uint32_t index; ///< Текущий индекс
|
||||||
uint8_t count; ///< Количество элементов
|
uint32_t count; ///< Количество элементов
|
||||||
float lastValue; ///< Последнее измеренное значение
|
float lastValue; ///< Последнее измеренное значение
|
||||||
|
uint8_t dataProcessing; ///< Флаг - данные в обработке
|
||||||
|
|
||||||
int (*reset)(struct _FilterAverage_t *filter, uint8_t size, FilterMode_t mode);
|
int (*reset)(struct _FilterAverage_t *filter, uint32_t size, FilterMode_t mode);
|
||||||
float (*process)(struct _FilterAverage_t *filter, float input);
|
float (*process)(struct _FilterAverage_t *filter, float input);
|
||||||
} FilterAverage_t;
|
} FilterAverage_t;
|
||||||
|
|
||||||
@ -226,6 +238,7 @@ typedef struct _FilterPoly_t{
|
|||||||
FilterState_t state; ///< Состояние фильтра
|
FilterState_t state; ///< Состояние фильтра
|
||||||
float coefficients[FILTER_POLY_MAX_ORDER + 1]; ///< Коэффициенты полинома
|
float coefficients[FILTER_POLY_MAX_ORDER + 1]; ///< Коэффициенты полинома
|
||||||
uint8_t order; ///< Порядок полинома
|
uint8_t order; ///< Порядок полинома
|
||||||
|
uint8_t dataProcessing; ///< Флаг - данные в обработке
|
||||||
|
|
||||||
int (*reset)(struct _FilterPoly_t *filter, float* coeffs, uint8_t order);
|
int (*reset)(struct _FilterPoly_t *filter, float* coeffs, uint8_t order);
|
||||||
float (*process)(struct _FilterPoly_t *filter, float input);
|
float (*process)(struct _FilterPoly_t *filter, float input);
|
||||||
@ -240,6 +253,7 @@ typedef struct _FilterLUT_t{
|
|||||||
float* output_values; // Массив выходных значений
|
float* output_values; // Массив выходных значений
|
||||||
uint16_t size; // Размер таблицы
|
uint16_t size; // Размер таблицы
|
||||||
uint8_t interpolation; // Флаг интерполяции (0 - отключена, 1 - линейная)
|
uint8_t interpolation; // Флаг интерполяции (0 - отключена, 1 - линейная)
|
||||||
|
uint8_t dataProcessing; ///< Флаг - данные в обработке
|
||||||
|
|
||||||
int (*reset)(struct _FilterLUT_t *filter, float* input_arr, float* output_arr, uint16_t size, uint8_t interpolation);
|
int (*reset)(struct _FilterLUT_t *filter, float* input_arr, float* output_arr, uint16_t size, uint8_t interpolation);
|
||||||
float (*process)(struct _FilterLUT_t *filter, float input);
|
float (*process)(struct _FilterLUT_t *filter, float input);
|
||||||
@ -251,7 +265,7 @@ int FilterMedian_Init(FilterMedian_t* filter, uint8_t size);
|
|||||||
float FilterMedian_Process(FilterMedian_t* filter, float input);
|
float FilterMedian_Process(FilterMedian_t* filter, float input);
|
||||||
int FilterExp_Init(FilterExp_t* filter, float alpha);
|
int FilterExp_Init(FilterExp_t* filter, float alpha);
|
||||||
float FilterExp_Process(FilterExp_t* filter, float input);
|
float FilterExp_Process(FilterExp_t* filter, float input);
|
||||||
int FilterAverage_Init(FilterAverage_t* filter, uint8_t size, FilterMode_t mode);
|
int FilterAverage_Init(FilterAverage_t* filter, uint32_t size, FilterMode_t mode);
|
||||||
float FilterAverage_Process(FilterAverage_t* filter, float input);
|
float FilterAverage_Process(FilterAverage_t* filter, float input);
|
||||||
int FilterPoly_Init(FilterPoly_t* filter, float* coeffs, uint8_t order);
|
int FilterPoly_Init(FilterPoly_t* filter, float* coeffs, uint8_t order);
|
||||||
float FilterPoly_Process(FilterPoly_t* filter, float input);
|
float FilterPoly_Process(FilterPoly_t* filter, float input);
|
||||||
@ -267,6 +281,7 @@ typedef struct _FilterMedianInt_t{
|
|||||||
int32_t buffer[FILTER_MEDIAN_MAX_SIZE]; ///< Буфер значений
|
int32_t buffer[FILTER_MEDIAN_MAX_SIZE]; ///< Буфер значений
|
||||||
uint8_t index; ///< Текущий индекс
|
uint8_t index; ///< Текущий индекс
|
||||||
uint8_t size; ///< Фактический размер фильтра
|
uint8_t size; ///< Фактический размер фильтра
|
||||||
|
uint8_t dataProcessing; ///< Флаг - данные в обработке
|
||||||
|
|
||||||
int (*reset)(struct _FilterMedianInt_t *filter, uint8_t size);
|
int (*reset)(struct _FilterMedianInt_t *filter, uint8_t size);
|
||||||
int32_t (*process)(struct _FilterMedianInt_t *filter, int32_t input);
|
int32_t (*process)(struct _FilterMedianInt_t *filter, int32_t input);
|
||||||
@ -281,6 +296,7 @@ typedef struct _FilterExpInt_t{
|
|||||||
int32_t value; ///< Текущее значение
|
int32_t value; ///< Текущее значение
|
||||||
uint8_t initialized; ///< Флаг инициализации
|
uint8_t initialized; ///< Флаг инициализации
|
||||||
int32_t scale; ///< Масштаб коэффициента (например 100 для 0.01)
|
int32_t scale; ///< Масштаб коэффициента (например 100 для 0.01)
|
||||||
|
uint8_t dataProcessing; ///< Флаг - данные в обработке
|
||||||
|
|
||||||
int (*reset)(struct _FilterExpInt_t *filter, int32_t alpha, int32_t scale);
|
int (*reset)(struct _FilterExpInt_t *filter, int32_t alpha, int32_t scale);
|
||||||
int32_t (*process)(struct _FilterExpInt_t *filter, int32_t input);
|
int32_t (*process)(struct _FilterExpInt_t *filter, int32_t input);
|
||||||
@ -292,14 +308,17 @@ typedef struct _FilterExpInt_t{
|
|||||||
typedef struct _FilterAverageInt_t{
|
typedef struct _FilterAverageInt_t{
|
||||||
FilterState_t state; ///< Состояние фильтра
|
FilterState_t state; ///< Состояние фильтра
|
||||||
FilterMode_t mode; ///< Режим фильтра
|
FilterMode_t mode; ///< Режим фильтра
|
||||||
|
#ifndef FILTERS_DISABLE_MOVING_AVERAGE
|
||||||
int32_t buffer[FILTER_AVERAGE_MAX_SIZE]; ///< Буфер значений
|
int32_t buffer[FILTER_AVERAGE_MAX_SIZE]; ///< Буфер значений
|
||||||
uint8_t size; ///< Фактический размер фильтра
|
#endif
|
||||||
|
uint32_t size; ///< Фактический размер фильтра
|
||||||
int64_t sum; ///< Сумма значений
|
int64_t sum; ///< Сумма значений
|
||||||
uint8_t index; ///< Текущий индекс
|
uint32_t index; ///< Текущий индекс
|
||||||
uint8_t count; ///< Количество элементов
|
uint32_t count; ///< Количество элементов
|
||||||
uint32_t lastValue; ///< Последнее измеренное значение
|
uint32_t lastValue; ///< Последнее измеренное значение
|
||||||
|
uint8_t dataProcessing; ///< Флаг - данные в обработке
|
||||||
|
|
||||||
int (*reset)(struct _FilterAverageInt_t *filter, uint8_t size, FilterMode_t mode);
|
int (*reset)(struct _FilterAverageInt_t *filter, uint32_t size, FilterMode_t mode);
|
||||||
int32_t (*process)(struct _FilterAverageInt_t *filter, int32_t input);
|
int32_t (*process)(struct _FilterAverageInt_t *filter, int32_t input);
|
||||||
} FilterAverageInt_t;
|
} FilterAverageInt_t;
|
||||||
|
|
||||||
@ -313,6 +332,7 @@ typedef struct _FilterPolyInt_t{
|
|||||||
int32_t coefficients[FILTER_POLY_MAX_ORDER + 1]; ///< Коэффициенты полинома
|
int32_t coefficients[FILTER_POLY_MAX_ORDER + 1]; ///< Коэффициенты полинома
|
||||||
uint8_t order; ///< Порядок полинома
|
uint8_t order; ///< Порядок полинома
|
||||||
int32_t scale; ///< Масштаб коэффициентов
|
int32_t scale; ///< Масштаб коэффициентов
|
||||||
|
uint8_t dataProcessing; ///< Флаг - данные в обработке
|
||||||
|
|
||||||
int (*reset)(struct _FilterPolyInt_t *filter, int32_t* coeffs, uint8_t order, int32_t scale);
|
int (*reset)(struct _FilterPolyInt_t *filter, int32_t* coeffs, uint8_t order, int32_t scale);
|
||||||
int32_t (*process)(struct _FilterPolyInt_t *filter, int32_t input);
|
int32_t (*process)(struct _FilterPolyInt_t *filter, int32_t input);
|
||||||
@ -327,6 +347,7 @@ typedef struct _FilterLUTInt_t{
|
|||||||
int32_t* output_values; ///< Массив выходных значений
|
int32_t* output_values; ///< Массив выходных значений
|
||||||
uint16_t size; ///< Размер таблицы
|
uint16_t size; ///< Размер таблицы
|
||||||
uint8_t interpolation; ///< Флаг интерполяции
|
uint8_t interpolation; ///< Флаг интерполяции
|
||||||
|
uint8_t dataProcessing; ///< Флаг - данные в обработке
|
||||||
|
|
||||||
int (*reset)(struct _FilterLUTInt_t *filter, int32_t* input_arr, int32_t* output_arr, uint16_t size, uint8_t interpolation);
|
int (*reset)(struct _FilterLUTInt_t *filter, int32_t* input_arr, int32_t* output_arr, uint16_t size, uint8_t interpolation);
|
||||||
int32_t (*process)(struct _FilterLUTInt_t *filter, int32_t input);
|
int32_t (*process)(struct _FilterLUTInt_t *filter, int32_t input);
|
||||||
@ -337,7 +358,7 @@ int FilterMedianInt_Init(FilterMedianInt_t* filter, uint8_t size);
|
|||||||
int32_t FilterMedianInt_Process(FilterMedianInt_t* filter, int32_t input);
|
int32_t FilterMedianInt_Process(FilterMedianInt_t* filter, int32_t input);
|
||||||
int 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);
|
int32_t FilterExpInt_Process(FilterExpInt_t* filter, int32_t input);
|
||||||
int FilterAverageInt_Init(FilterAverageInt_t* filter, uint8_t size, FilterMode_t mode);
|
int FilterAverageInt_Init(FilterAverageInt_t* filter, uint32_t size, FilterMode_t mode);
|
||||||
int32_t FilterAverageInt_Process(FilterAverageInt_t* filter, int32_t input);
|
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);
|
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);
|
int32_t FilterPolyInt_Process(FilterPolyInt_t* filter, int32_t input);
|
||||||
|
|||||||
@ -111,11 +111,13 @@ float FilterExp_Process(FilterExp_t* filter, float input) {
|
|||||||
* @param filter Указатель на структуру фильтра
|
* @param filter Указатель на структуру фильтра
|
||||||
* @return 0 - успех, -1 - ошибка
|
* @return 0 - успех, -1 - ошибка
|
||||||
*/
|
*/
|
||||||
int FilterAverage_Init(FilterAverage_t* filter, uint8_t size, FilterMode_t mode) {
|
int FilterAverage_Init(FilterAverage_t* filter, uint32_t size, FilterMode_t mode) {
|
||||||
check_init_filter(filter);
|
check_init_filter(filter);
|
||||||
if (size == 0 || size > FILTER_AVERAGE_MAX_SIZE) return -1;
|
if (size == 0 || size > FILTER_AVERAGE_MAX_SIZE) return -1;
|
||||||
|
|
||||||
|
#ifndef FILTERS_DISABLE_MOVING_AVERAGE
|
||||||
memset(filter->buffer, 0, sizeof(filter->buffer));
|
memset(filter->buffer, 0, sizeof(filter->buffer));
|
||||||
|
#endif
|
||||||
filter->size = size;
|
filter->size = size;
|
||||||
filter->sum = 0.0f;
|
filter->sum = 0.0f;
|
||||||
filter->index = 0;
|
filter->index = 0;
|
||||||
@ -140,9 +142,10 @@ float FilterAverage_Process(FilterAverage_t* filter, float input) {
|
|||||||
// Общая логика для обоих режимов
|
// Общая логика для обоих режимов
|
||||||
filter->sum += input;
|
filter->sum += input;
|
||||||
filter->count++;
|
filter->count++;
|
||||||
|
filter->dataProcessing = 1;
|
||||||
// Логика скользящего среднего
|
// Логика скользящего среднего
|
||||||
if (filter->mode == FILTER_MODE_MOVING) {
|
if (filter->mode == FILTER_MODE_MOVING) {
|
||||||
|
#ifndef FILTERS_DISABLE_MOVING_AVERAGE
|
||||||
if (filter->count > filter->size) {
|
if (filter->count > filter->size) {
|
||||||
filter->sum -= filter->buffer[filter->index];
|
filter->sum -= filter->buffer[filter->index];
|
||||||
filter->count = filter->size; // Поддерживаем фиксированный размер окна
|
filter->count = filter->size; // Поддерживаем фиксированный размер окна
|
||||||
@ -150,6 +153,8 @@ float FilterAverage_Process(FilterAverage_t* filter, float input) {
|
|||||||
filter->buffer[filter->index] = input;
|
filter->buffer[filter->index] = input;
|
||||||
filter->index = (filter->index + 1) % filter->size;
|
filter->index = (filter->index + 1) % filter->size;
|
||||||
filter->lastValue = filter->sum / filter->count;
|
filter->lastValue = filter->sum / filter->count;
|
||||||
|
filter->dataProcessing = 0;
|
||||||
|
#endif
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
@ -158,6 +163,7 @@ float FilterAverage_Process(FilterAverage_t* filter, float input) {
|
|||||||
filter->lastValue = filter->sum / filter->count;
|
filter->lastValue = filter->sum / filter->count;
|
||||||
filter->count = 0;
|
filter->count = 0;
|
||||||
filter->sum = 0;
|
filter->sum = 0;
|
||||||
|
filter->dataProcessing = 0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -380,11 +386,12 @@ int32_t FilterExpInt_Process(FilterExpInt_t* filter, int32_t input) {
|
|||||||
* @param filter Указатель на структуру фильтра
|
* @param filter Указатель на структуру фильтра
|
||||||
* @return 0 - успех, -1 - ошибка
|
* @return 0 - успех, -1 - ошибка
|
||||||
*/
|
*/
|
||||||
int FilterAverageInt_Init(FilterAverageInt_t* filter, uint8_t size, FilterMode_t mode) {
|
int FilterAverageInt_Init(FilterAverageInt_t* filter, uint32_t size, FilterMode_t mode) {
|
||||||
check_init_filter(filter);
|
check_init_filter(filter);
|
||||||
if (size == 0 || size > FILTER_AVERAGE_MAX_SIZE) return - 1;
|
if (size == 0 || size > FILTER_AVERAGE_MAX_SIZE) return - 1;
|
||||||
|
#ifndef FILTERS_DISABLE_MOVING_AVERAGE
|
||||||
memset(filter->buffer, 0, sizeof(filter->buffer));
|
memset(filter->buffer, 0, sizeof(filter->buffer));
|
||||||
|
#endif
|
||||||
filter->size = size;
|
filter->size = size;
|
||||||
filter->sum = 0;
|
filter->sum = 0;
|
||||||
filter->index = 0;
|
filter->index = 0;
|
||||||
@ -412,6 +419,7 @@ int32_t FilterAverageInt_Process(FilterAverageInt_t* filter, int32_t input) {
|
|||||||
|
|
||||||
// Логика скользящего среднего
|
// Логика скользящего среднего
|
||||||
if (filter->mode == FILTER_MODE_MOVING) {
|
if (filter->mode == FILTER_MODE_MOVING) {
|
||||||
|
#ifndef FILTERS_DISABLE_MOVING_AVERAGE
|
||||||
if (filter->count > filter->size) {
|
if (filter->count > filter->size) {
|
||||||
filter->sum -= filter->buffer[filter->index];
|
filter->sum -= filter->buffer[filter->index];
|
||||||
filter->count = filter->size; // Поддерживаем фиксированный размер окна
|
filter->count = filter->size; // Поддерживаем фиксированный размер окна
|
||||||
@ -419,6 +427,7 @@ int32_t FilterAverageInt_Process(FilterAverageInt_t* filter, int32_t input) {
|
|||||||
filter->buffer[filter->index] = input;
|
filter->buffer[filter->index] = input;
|
||||||
filter->index = (filter->index + 1) % filter->size;
|
filter->index = (filter->index + 1) % filter->size;
|
||||||
filter->lastValue = filter->sum / filter->count;
|
filter->lastValue = filter->sum / filter->count;
|
||||||
|
#endif
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user