Compare commits
12 Commits
606058ef55
...
develop
| Author | SHA1 | Date | |
|---|---|---|---|
| 583856a2ff | |||
|
|
1b57406d57 | ||
|
|
457ec2a729 | ||
|
|
dfbae9be39 | ||
|
|
a6b27da4ce | ||
|
|
1f7384c5ed | ||
|
|
c5a01c56ac | ||
|
|
0fd9c78c32 | ||
|
|
30fdbc35dd | ||
|
|
513f56fe7d | ||
|
|
9bff9ad44d | ||
|
|
99652a9ad5 |
@@ -182,6 +182,61 @@ static inline uint32_t BenchTime_End(uint8_t channel, uint32_t ticks) {
|
||||
return elapsed_ticks;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @brief Измерение периода на указанном канале
|
||||
* @param channel Номер канала (0..BENCH_TIME_MAX_CHANNELS-1)
|
||||
* @param ticks Источник тиков (например: HAL_GetTick(), TIM2->CNT, DWT->CYCCNT)
|
||||
* @param tick_period Период тиков для переполнения
|
||||
* @return Измеренное время в тиках, 0 - в случае первого запуска или ошибки
|
||||
*/
|
||||
static inline uint32_t BenchTime_Period(uint8_t channel, uint32_t ticks, uint32_t tick_period) {
|
||||
if (channel >= BENCH_TIME_MAX_CHANNELS) return 0;
|
||||
if (!hbt.channels[channel].is_running)
|
||||
{
|
||||
hbt.channels[channel].start_tick = ticks;
|
||||
hbt.channels[channel].tick_period = tick_period;
|
||||
hbt.channels[channel].is_running = 1;
|
||||
return 0;
|
||||
};
|
||||
|
||||
uint32_t end_tick = ticks;
|
||||
uint32_t start_tick = hbt.channels[channel].start_tick;
|
||||
uint32_t elapsed_ticks;
|
||||
|
||||
// Инит для следующего измерения
|
||||
hbt.channels[channel].start_tick = end_tick;
|
||||
hbt.channels[channel].tick_period = tick_period;
|
||||
|
||||
if (end_tick >= start_tick) {
|
||||
elapsed_ticks = end_tick - start_tick;
|
||||
} else {
|
||||
elapsed_ticks = (tick_period - start_tick) + end_tick + 1;
|
||||
}
|
||||
|
||||
if (elapsed_ticks > tick_period) {
|
||||
elapsed_ticks = tick_period;
|
||||
}
|
||||
|
||||
// Обновление статистики
|
||||
BenchTimeStats_t* stats = &hbt.channels[channel].stats;
|
||||
stats->last_ticks = elapsed_ticks;
|
||||
|
||||
if (elapsed_ticks < stats->min_ticks) {
|
||||
stats->min_ticks = elapsed_ticks;
|
||||
}
|
||||
|
||||
if (elapsed_ticks > stats->max_ticks) {
|
||||
stats->max_ticks = elapsed_ticks;
|
||||
}
|
||||
|
||||
stats->total_ticks += elapsed_ticks;
|
||||
stats->count++;
|
||||
|
||||
return elapsed_ticks;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @brief Получение минимального времени измерения
|
||||
*/
|
||||
@@ -254,4 +309,4 @@ static inline void BenchTime_ResetStats(uint8_t channel) {
|
||||
|
||||
/** BENCH_TIME
|
||||
* @}
|
||||
*/
|
||||
*/
|
||||
|
||||
@@ -119,6 +119,15 @@ typedef union
|
||||
}uint64_BitTypeDef;
|
||||
|
||||
|
||||
/**
|
||||
* @brief Получить n-й бит из любого числа
|
||||
* @param _uint8_ Переменная типа uint8_t
|
||||
* @param _bit_ Константный номер бита (0..7)
|
||||
* @return Значение выбранного бита (0 или 1)
|
||||
* @note Индекс бита вычисляется программно, а не на этапе компиляции
|
||||
*/
|
||||
#define read_bit(_val_, _bit_) ((_val_) & ((uint64_t)1 << _bit_))
|
||||
|
||||
/**
|
||||
* @brief Получить n-й бит из uint8_t
|
||||
* @param _uint8_ Переменная типа uint8_t
|
||||
@@ -156,4 +165,4 @@ typedef union
|
||||
|
||||
/** BIT_ACCESS_DEFINES
|
||||
* @}
|
||||
*/
|
||||
*/
|
||||
|
||||
@@ -85,13 +85,19 @@ int32_t process_value_int(int32_t raw_adc_quant) {
|
||||
#ifdef FILTERS_ENABLE
|
||||
|
||||
#ifdef ARM_MATH_CM4
|
||||
#include "arm_math.h"
|
||||
#define DSP_FITLERS 1
|
||||
#include "arm_math.h"
|
||||
#define DSP_FITLERS 1
|
||||
#else
|
||||
#include "math.h"
|
||||
#endif
|
||||
|
||||
|
||||
#ifndef FILTER_AVERAGE_MAX_SIZE
|
||||
#ifndef FILTERS_DISABLE_MOVING_AVERAGE
|
||||
#define FILTER_AVERAGE_MAX_SIZE 100 ///< Размер окна усредняющего фильтра
|
||||
#else
|
||||
#define FILTER_AVERAGE_MAX_SIZE 65535 ///< Размер окна усредняющего фильтра без буфера
|
||||
#endif
|
||||
#endif
|
||||
|
||||
#ifndef FILTER_MEDIAN_MAX_SIZE
|
||||
@@ -102,6 +108,9 @@ int32_t process_value_int(int32_t raw_adc_quant) {
|
||||
#define FILTER_POLY_MAX_ORDER 4 ///< Максимальный порядок полинома
|
||||
#endif
|
||||
|
||||
#ifndef FILTER_RMS_MAX_SIZE
|
||||
#define FILTER_RMS_MAX_SIZE 128 ///< Максимальный размер окна (рекомендуется степень 2)
|
||||
#endif
|
||||
|
||||
#define check_proccess_func(_ptr_) \
|
||||
((_fltr_)->process != NULL) 1 : \
|
||||
@@ -125,8 +134,8 @@ int32_t process_value_int(int32_t raw_adc_quant) {
|
||||
* @param filter Указатель на структуру фильтра
|
||||
* @details Запускает функцию инициализации, если указатель инициализирован
|
||||
*/
|
||||
#define Filter_Reset(_fltr_, _input_) \
|
||||
((_fltr_)->reset != NULL) ? (_fltr_)->reset(_fltr_, _input_): -1
|
||||
#define Filter_ReInit(_fltr_, ...) \
|
||||
((_fltr_)->reset != NULL) ? (_fltr_)->reset(_fltr_, __VA_ARGS__): -1
|
||||
|
||||
|
||||
/**
|
||||
@@ -155,11 +164,14 @@ do{ if(Filter_isReady(_fltr_)) (_fltr_)->state = FILTER_ENABLE; }while(0)
|
||||
#define Filter_Stop(_fltr_) \
|
||||
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_isInit(_fltr_) !(Filter_GetState(_fltr_) == FILTER_NOT_INIT)
|
||||
#define Filter_isReady(_fltr_) (Filter_GetState(_fltr_) == FILTER_READY)
|
||||
#define Filter_isEnable(_fltr_) (Filter_GetState(_fltr_) == FILTER_ENABLE)
|
||||
|
||||
|
||||
typedef enum
|
||||
{
|
||||
FILTER_NOT_INIT,
|
||||
@@ -184,8 +196,9 @@ typedef struct _FilterMedian_t{
|
||||
float buffer[FILTER_MEDIAN_MAX_SIZE]; ///< Буфер значений
|
||||
uint8_t index; ///< Текущий индекс
|
||||
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 init_val);
|
||||
float (*process)(struct _FilterMedian_t *filter, float input);
|
||||
} FilterMedian_t;
|
||||
|
||||
@@ -197,6 +210,7 @@ typedef struct _FilterExp_t {
|
||||
float alpha; ///< Коэффициент сглаживания (0..1)
|
||||
float value; ///< Текущее значение
|
||||
uint8_t initialized; ///< Флаг инициализации
|
||||
uint8_t dataProcessing; ///< Флаг - данные в обработке
|
||||
|
||||
int (*reset)(struct _FilterExp_t *filter, float alpha);
|
||||
float (*process)(struct _FilterExp_t *filter, float input);
|
||||
@@ -208,14 +222,17 @@ typedef struct _FilterExp_t {
|
||||
typedef struct _FilterAverage_t{
|
||||
FilterState_t state; ///< Состояние фильтра
|
||||
FilterMode_t mode; ///< Режим фильтра
|
||||
#ifndef FILTERS_DISABLE_MOVING_AVERAGE
|
||||
float buffer[FILTER_AVERAGE_MAX_SIZE]; ///< Буфер значений
|
||||
uint8_t size; ///< Фактический размер фильтра
|
||||
#endif
|
||||
uint32_t size; ///< Фактический размер фильтра
|
||||
float sum; ///< Сумма значений
|
||||
uint8_t index; ///< Текущий индекс
|
||||
uint8_t count; ///< Количество элементов
|
||||
uint32_t index; ///< Текущий индекс
|
||||
uint32_t count; ///< Количество элементов
|
||||
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);
|
||||
} FilterAverage_t;
|
||||
|
||||
@@ -226,6 +243,7 @@ typedef struct _FilterPoly_t{
|
||||
FilterState_t state; ///< Состояние фильтра
|
||||
float coefficients[FILTER_POLY_MAX_ORDER + 1]; ///< Коэффициенты полинома
|
||||
uint8_t order; ///< Порядок полинома
|
||||
uint8_t dataProcessing; ///< Флаг - данные в обработке
|
||||
|
||||
int (*reset)(struct _FilterPoly_t *filter, float* coeffs, uint8_t order);
|
||||
float (*process)(struct _FilterPoly_t *filter, float input);
|
||||
@@ -240,23 +258,91 @@ typedef struct _FilterLUT_t{
|
||||
float* output_values; // Массив выходных значений
|
||||
uint16_t size; // Размер таблицы
|
||||
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);
|
||||
float (*process)(struct _FilterLUT_t *filter, float input);
|
||||
} FilterLUT_t;
|
||||
|
||||
/**
|
||||
* @brief Структура True RMS фильтра (float)
|
||||
*/
|
||||
typedef struct _FilterRMS_t {
|
||||
FilterState_t state; ///< Состояние фильтра
|
||||
float buffer_sq[FILTER_RMS_MAX_SIZE]; ///< Буфер квадратов значений
|
||||
float sum_squares; ///< Текущая сумма квадратов
|
||||
float last_rms; ///< Последнее рассчитанное RMS значение
|
||||
uint32_t window_size; ///< Размер окна усреднения
|
||||
uint32_t index; ///< Текущий индекс в буфере
|
||||
uint32_t count; ///< Количество накопленных значений
|
||||
uint8_t dataProcessing; ///< Флаг - данные в обработке
|
||||
|
||||
int (*reset)(struct _FilterRMS_t *filter, uint32_t window_size);
|
||||
float (*process)(struct _FilterRMS_t *filter, float input);
|
||||
} FilterRMS_t;
|
||||
|
||||
// Float версии функций
|
||||
int FilterMedian_Init(FilterMedian_t* filter, uint8_t size);
|
||||
int FilterMedian_Init(FilterMedian_t* filter, uint8_t size, float init_val);
|
||||
float FilterMedian_Process(FilterMedian_t* filter, float input);
|
||||
int FilterExp_Init(FilterExp_t* filter, float alpha);
|
||||
float FilterExp_Process(FilterExp_t* filter, float input);
|
||||
int FilterAverage_Init(FilterAverage_t* filter, uint8_t size, FilterMode_t mode);
|
||||
float FilterExp_Process(FilterExp_t* filter, float input);
|
||||
int FilterAverage_Init(FilterAverage_t* filter, uint32_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);
|
||||
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);
|
||||
int FilterRMS_Init(FilterRMS_t* filter, uint32_t window_size);
|
||||
float FilterRMS_Process(FilterRMS_t* filter, float input);
|
||||
|
||||
|
||||
/**
|
||||
* @brief Расчет коэфициента альфа
|
||||
* @param tau Постоянная времени (время нарастания до 63% от уровня сигнала)
|
||||
* @param TsUs Период вызова фильтра в микросекундах
|
||||
* @return Коэфициент альфа
|
||||
*/
|
||||
#define FilterExp_CalcAlpha(tau, TsUs) (((float)TsUs/1000000) / (((float)TsUs/1000000) + (tau)))
|
||||
|
||||
/**
|
||||
* @brief Расчет alpha для времени нарастания до 63%
|
||||
* @param rise_time Требуемое время нарастания до 63%
|
||||
* @param TsUs Период вызова фильтра в микросекундах
|
||||
* @return Коэфициент альфа
|
||||
*/
|
||||
#define FilterExp_CalcAlpha63(rise_time, TsUs) FilterExp_CalcAlpha((rise_time) / 1.0f, TsUs)
|
||||
|
||||
/**
|
||||
* @brief Расчет alpha для времени нарастания до 86%
|
||||
* @param rise_time Требуемое время нарастания до 86%
|
||||
* @param TsUs Период вызова фильтра в микросекундах
|
||||
* @return Коэфициент альфа
|
||||
*/
|
||||
#define FilterExp_CalcAlpha86(rise_time, TsUs) FilterExp_CalcAlpha((rise_time) / 2.0f, TsUs)
|
||||
|
||||
/**
|
||||
* @brief Расчет alpha для времени нарастания до 95%
|
||||
* @param rise_time Требуемое время нарастания до 95%
|
||||
* @param TsUs Период вызова фильтра в микросекундах
|
||||
* @return Коэфициент альфа
|
||||
*/
|
||||
#define FilterExp_CalcAlpha95(rise_time, TsUs) FilterExp_CalcAlpha((rise_time) / 3.0f, TsUs)
|
||||
|
||||
/**
|
||||
* @brief Расчет alpha для времени нарастания до 98%
|
||||
* @param rise_time Требуемое время нарастания до 98%
|
||||
* @param TsUs Период вызова фильтра в микросекундах
|
||||
* @return Коэфициент альфа
|
||||
*/
|
||||
#define FilterExp_CalcAlpha98(rise_time, TsUs) FilterExp_CalcAlpha((rise_time) / 4.0f, TsUs)
|
||||
|
||||
/**
|
||||
* @brief Расчет alpha для времени нарастания до 99%
|
||||
* @param rise_time Требуемое время нарастания до 99%
|
||||
* @param TsUs Период вызова фильтра в микросекундах
|
||||
* @return Коэфициент альфа
|
||||
*/
|
||||
#define FilterExp_CalcAlpha99(rise_time, TsUs) FilterExp_CalcAlpha((rise_time) / 5.0f, TsUs)
|
||||
// ==================== INT32_T ВЕРСИИ ====================
|
||||
|
||||
/**
|
||||
@@ -265,10 +351,12 @@ float FilterLUT_Process(FilterLUT_t* filter, float input);
|
||||
typedef struct _FilterMedianInt_t{
|
||||
FilterState_t state; ///< Состояние фильтра
|
||||
int32_t buffer[FILTER_MEDIAN_MAX_SIZE]; ///< Буфер значений
|
||||
int32_t sorted[FILTER_MEDIAN_MAX_SIZE]; ///< Буфер отсортированных значений
|
||||
uint8_t index; ///< Текущий индекс
|
||||
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 init_val);
|
||||
int32_t (*process)(struct _FilterMedianInt_t *filter, int32_t input);
|
||||
} FilterMedianInt_t;
|
||||
|
||||
@@ -281,6 +369,7 @@ typedef struct _FilterExpInt_t{
|
||||
int32_t value; ///< Текущее значение
|
||||
uint8_t initialized; ///< Флаг инициализации
|
||||
int32_t scale; ///< Масштаб коэффициента (например 100 для 0.01)
|
||||
uint8_t dataProcessing; ///< Флаг - данные в обработке
|
||||
|
||||
int (*reset)(struct _FilterExpInt_t *filter, int32_t alpha, int32_t scale);
|
||||
int32_t (*process)(struct _FilterExpInt_t *filter, int32_t input);
|
||||
@@ -292,14 +381,17 @@ typedef struct _FilterExpInt_t{
|
||||
typedef struct _FilterAverageInt_t{
|
||||
FilterState_t state; ///< Состояние фильтра
|
||||
FilterMode_t mode; ///< Режим фильтра
|
||||
#ifndef FILTERS_DISABLE_MOVING_AVERAGE
|
||||
int32_t buffer[FILTER_AVERAGE_MAX_SIZE]; ///< Буфер значений
|
||||
uint8_t size; ///< Фактический размер фильтра
|
||||
#endif
|
||||
uint32_t size; ///< Фактический размер фильтра
|
||||
int64_t sum; ///< Сумма значений
|
||||
uint8_t index; ///< Текущий индекс
|
||||
uint8_t count; ///< Количество элементов
|
||||
uint32_t lastValue; ///< Последнее измеренное значение
|
||||
uint32_t index; ///< Текущий индекс
|
||||
uint32_t count; ///< Количество элементов
|
||||
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);
|
||||
} FilterAverageInt_t;
|
||||
|
||||
@@ -313,6 +405,7 @@ typedef struct _FilterPolyInt_t{
|
||||
int32_t coefficients[FILTER_POLY_MAX_ORDER + 1]; ///< Коэффициенты полинома
|
||||
uint8_t order; ///< Порядок полинома
|
||||
int32_t scale; ///< Масштаб коэффициентов
|
||||
uint8_t dataProcessing; ///< Флаг - данные в обработке
|
||||
|
||||
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);
|
||||
@@ -327,26 +420,82 @@ typedef struct _FilterLUTInt_t{
|
||||
int32_t* output_values; ///< Массив выходных значений
|
||||
uint16_t size; ///< Размер таблицы
|
||||
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);
|
||||
int32_t (*process)(struct _FilterLUTInt_t *filter, int32_t input);
|
||||
} FilterLUTInt_t;
|
||||
|
||||
/**
|
||||
* @brief Структура True RMS фильтра (int32_t)
|
||||
*/
|
||||
typedef struct _FilterRMSInt_t {
|
||||
FilterState_t state; ///< Состояние фильтра
|
||||
int64_t buffer_sq[FILTER_RMS_MAX_SIZE]; ///< Буфер квадратов значений
|
||||
int64_t sum_squares; ///< Текущая сумма квадратов
|
||||
int32_t last_rms; ///< Последнее рассчитанное RMS значение
|
||||
uint32_t window_size; ///< Размер окна усреднения
|
||||
uint32_t index; ///< Текущий индекс в буфере
|
||||
uint32_t count; ///< Количество накопленных значений
|
||||
uint8_t dataProcessing; ///< Флаг - данные в обработке
|
||||
|
||||
int (*reset)(struct _FilterRMSInt_t *filter, uint32_t window_size);
|
||||
int32_t (*process)(struct _FilterRMSInt_t *filter, int32_t input);
|
||||
} FilterRMSInt_t;
|
||||
|
||||
// Int32_t версии функций
|
||||
int FilterMedianInt_Init(FilterMedianInt_t* filter, uint8_t size);
|
||||
int FilterMedianInt_Init(FilterMedianInt_t* filter, uint8_t size, int32_t init_val);
|
||||
int32_t FilterMedianInt_Process(FilterMedianInt_t* filter, int32_t input);
|
||||
int FilterExpInt_Init(FilterExpInt_t* filter, int32_t alpha, int32_t scale);
|
||||
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);
|
||||
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);
|
||||
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);
|
||||
int FilterRMSInt_Init(FilterRMSInt_t* filter, uint32_t window_size);
|
||||
int32_t FilterRMSInt_Process(FilterRMSInt_t* filter, int32_t input);
|
||||
|
||||
|
||||
|
||||
// ==================== CMSIS ВЕРСИИ ====================
|
||||
// ==================== ДРУГИЕ ФИЛЬТРЫ ====================
|
||||
|
||||
|
||||
/**
|
||||
* @brief Структура полосового фильтра с дифференциатором
|
||||
* @details Комбинация дифференциатора и полосового фильтра 2-го порядка.
|
||||
* Используется для выделения сетевой частоты и подготовки к детектированию нуля.
|
||||
*/
|
||||
typedef struct _FilterBandPassDerivative_t {
|
||||
FilterState_t state; ///< Состояние фильтра
|
||||
|
||||
// Дифференциатор
|
||||
float prev_input; ///< Предыдущее входное значение
|
||||
|
||||
// Полосовой фильтр (биквадратный, прямая форма II)
|
||||
float b0, b1, b2; ///< Коэффициенты числителя
|
||||
float a1, a2; ///< Коэффициенты знаменателя
|
||||
|
||||
// Состояния фильтра
|
||||
float x1, x2; ///< Состояния входа
|
||||
float y1, y2; ///< Состояния выхода
|
||||
|
||||
float last_output; ///< Последнее выходное значение
|
||||
uint8_t dataProcessing; ///< Флаг обработки данных
|
||||
|
||||
// Указатели на функции
|
||||
int (*reset)(struct _FilterBandPassDerivative_t *filter,
|
||||
float center_freq_ratio, float bandwidth_ratio);
|
||||
float (*process)(struct _FilterBandPassDerivative_t *filter, float input);
|
||||
} FilterBandPassDerivative_t;
|
||||
|
||||
int FilterBandPassDerivative_Init(FilterBandPassDerivative_t* filter,
|
||||
float center_freq_ratio,
|
||||
float bandwidth_ratio);
|
||||
float FilterBandPassDerivative_Process(FilterBandPassDerivative_t* filter,
|
||||
float input);
|
||||
|
||||
#ifdef DSP_FITLERS
|
||||
/**
|
||||
* @brief Структура биквадратного фильтра с CMSIS-DSP
|
||||
@@ -374,4 +523,4 @@ float FilterBiquad_Process(FilterBiquad_t* filter, float input);
|
||||
|
||||
#endif // FILTERS_ENABLE
|
||||
|
||||
#endif // __FILTERS_H_
|
||||
#endif // __FILTERS_H_
|
||||
|
||||
@@ -79,8 +79,8 @@ extern void Error_Handler(void);
|
||||
* Этот блок содержит макросы для реализации задержек с использованием HAL или FreeRTOS:
|
||||
* - @ref msDelay — простая задержка заданной длительности;
|
||||
* - @ref msDelayStart — сохранение текущего времени начала задержки;
|
||||
* - @ref msDelayWhileActive — проверка, активна ли задержка;
|
||||
* - @ref msDelayWaitDone — проверка, завершена ли задержка.
|
||||
* - @ref msDelayActive — проверка, активна ли задержка;
|
||||
* - @ref msDelayDone — проверка, завершена ли задержка.
|
||||
* Эти макросы удобны для реализации неблокирующих задержек.
|
||||
* @{
|
||||
*/
|
||||
@@ -92,13 +92,18 @@ extern void Error_Handler(void);
|
||||
* @note Использует задержку через @ref local_time или osDelay в зависимости от @ref FREERTOS_DELAY.
|
||||
*/
|
||||
#ifdef FREERTOS_DELAY
|
||||
#define msDelay(_ms_) osDelay(_ms_)
|
||||
__STATIC_INLINE void msDelay(uint32_t _ms_)
|
||||
{
|
||||
osDelay(_ms_);
|
||||
}
|
||||
#else
|
||||
#define msDelay(_ms_) \
|
||||
do { \
|
||||
uint32_t _start_ = local_time(); \
|
||||
while (local_time() - _start_ < (_ms_)) {} \
|
||||
} while(0)
|
||||
__STATIC_INLINE void msDelay(uint32_t _ms_)
|
||||
{
|
||||
volatile uint32_t _start_ = local_time();
|
||||
while ((local_time() - _start_) < (_ms_))
|
||||
{
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
|
||||
@@ -110,7 +115,10 @@ extern void Error_Handler(void);
|
||||
*
|
||||
* Используется для реализации неблокирующих задержек.
|
||||
*/
|
||||
#define msDelayStart(_pvar_) *(_pvar_) = local_time()
|
||||
__STATIC_INLINE void msDelayStart(uint32_t *_pvar_)
|
||||
{
|
||||
*(_pvar_) = local_time();
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Проверяет, активна ли задержка.
|
||||
@@ -119,15 +127,18 @@ extern void Error_Handler(void);
|
||||
* @retval 1 Задержка еще активна.
|
||||
* @retval 0 Задержка завершена.
|
||||
* @details
|
||||
* Возвращает true, пока время задержки не истекло. Используется в проверках,
|
||||
* Возвращает true, пока задержка активна. Используется в проверках,
|
||||
* когда нужно **действовать, пока задержка выполняется**. Пример:
|
||||
* @code
|
||||
* while(msDelayWhileActive(1000, &tick)) {
|
||||
* while(msDelayActive(1000, &tick)) {
|
||||
* // выполняем другие задачи, задержка не блокирует поток
|
||||
* }
|
||||
* @endcode
|
||||
*/
|
||||
#define msDelayWhileActive(_ms_, _pvar_) (local_time() - *(_pvar_) < _ms_)
|
||||
__STATIC_INLINE int msDelayActive(uint32_t _ms_, uint32_t *_pvar_)
|
||||
{
|
||||
return (local_time() - *(_pvar_) < _ms_);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Проверяет, завершилась ли задержка.
|
||||
@@ -136,15 +147,18 @@ extern void Error_Handler(void);
|
||||
* @retval 1 Задержка завершена.
|
||||
* @retval 0 Задержка еще активна.
|
||||
* @details
|
||||
* Возвращает true, когда задержка уже завершена. Используется в проверках,
|
||||
* Возвращает true, когда задержка закончилась. Используется в проверках,
|
||||
* когда нужно **выполнить действие только после окончания задержки**. Пример:
|
||||
* @code
|
||||
* if(msDelayWaitDone(1000, &tick)) {
|
||||
* if(msDelayDone(1000, &tick)) {
|
||||
* // выполняем действие после завершения задержки
|
||||
* }
|
||||
* @endcode
|
||||
*/
|
||||
#define msDelayWaitDone(_ms_, _pvar_) (local_time() - *(_pvar_) >= _ms_)
|
||||
__STATIC_INLINE int msDelayDone(uint32_t _ms_, uint32_t *_pvar_)
|
||||
{
|
||||
return (local_time() - *(_pvar_) >= _ms_);
|
||||
}
|
||||
|
||||
/** DELAYS_DEFINES
|
||||
* @}
|
||||
@@ -201,6 +215,12 @@ extern void Error_Handler(void);
|
||||
*/
|
||||
#define ABS(x) ( ((x) > 0)? (x) : -(x))
|
||||
|
||||
/**
|
||||
* @brief Константа Пи
|
||||
*/
|
||||
#ifndef PI
|
||||
#define PI 3.14159265f
|
||||
#endif
|
||||
/** UTILS_DEFINES
|
||||
* @}
|
||||
*/
|
||||
@@ -224,4 +244,4 @@ do{ \
|
||||
|
||||
|
||||
/** @endcond */
|
||||
#endif //__MYLIBS_TOOLS_H_
|
||||
#endif //__MYLIBS_TOOLS_H_
|
||||
|
||||
@@ -587,4 +587,4 @@ __STATIC_FORCEINLINE void HF_HandleFault(void)
|
||||
|
||||
|
||||
|
||||
#endif //__TRACE_H_
|
||||
#endif //__TRACE_H_
|
||||
|
||||
@@ -159,4 +159,4 @@
|
||||
|
||||
#endif //TRACKERS_ENABLE
|
||||
|
||||
#endif //__TRACKERS_H_
|
||||
#endif //__TRACKERS_H_
|
||||
|
||||
@@ -32,11 +32,14 @@ static int Filter_float_compare(const void *a, const void *b) {
|
||||
* @param filter Указатель на структуру фильтра
|
||||
* @return 0 - успех, -1 - ошибка
|
||||
*/
|
||||
int FilterMedian_Init(FilterMedian_t* filter, uint8_t size) {
|
||||
int FilterMedian_Init(FilterMedian_t* filter, uint8_t size, float init_val) {
|
||||
check_init_filter(filter);
|
||||
if (size == 0 || size > FILTER_MEDIAN_MAX_SIZE) return -1;
|
||||
|
||||
memset(filter->buffer, 0, sizeof(filter->buffer));
|
||||
for (int i = 0; i < size; i++)
|
||||
{
|
||||
filter->buffer[i] = init_val;
|
||||
}
|
||||
filter->index = 0;
|
||||
filter->size = size;
|
||||
|
||||
@@ -111,11 +114,13 @@ float FilterExp_Process(FilterExp_t* filter, float input) {
|
||||
* @param filter Указатель на структуру фильтра
|
||||
* @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);
|
||||
if (size == 0 || size > FILTER_AVERAGE_MAX_SIZE) return -1;
|
||||
|
||||
#ifndef FILTERS_DISABLE_MOVING_AVERAGE
|
||||
memset(filter->buffer, 0, sizeof(filter->buffer));
|
||||
#endif
|
||||
filter->size = size;
|
||||
filter->sum = 0.0f;
|
||||
filter->index = 0;
|
||||
@@ -140,9 +145,10 @@ float FilterAverage_Process(FilterAverage_t* filter, float input) {
|
||||
// Общая логика для обоих режимов
|
||||
filter->sum += input;
|
||||
filter->count++;
|
||||
|
||||
filter->dataProcessing = 1;
|
||||
// Логика скользящего среднего
|
||||
if (filter->mode == FILTER_MODE_MOVING) {
|
||||
#ifndef FILTERS_DISABLE_MOVING_AVERAGE
|
||||
if (filter->count > filter->size) {
|
||||
filter->sum -= filter->buffer[filter->index];
|
||||
filter->count = filter->size; // Поддерживаем фиксированный размер окна
|
||||
@@ -150,14 +156,17 @@ float FilterAverage_Process(FilterAverage_t* filter, float input) {
|
||||
filter->buffer[filter->index] = input;
|
||||
filter->index = (filter->index + 1) % filter->size;
|
||||
filter->lastValue = filter->sum / filter->count;
|
||||
filter->dataProcessing = 0;
|
||||
#endif
|
||||
}
|
||||
else
|
||||
{
|
||||
if (filter->count > filter->size)
|
||||
if (filter->count >= filter->size)
|
||||
{
|
||||
filter->lastValue = filter->sum / filter->count;
|
||||
filter->count = 0;
|
||||
filter->sum = 0;
|
||||
filter->dataProcessing = 0;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -279,27 +288,96 @@ float FilterLUT_Process(FilterLUT_t* filter, float input) {
|
||||
|
||||
return y0 + (input - x0) * (y1 - y0) / (x1 - x0);
|
||||
}
|
||||
// ==================== INT32_T ВЕРСИИ ====================
|
||||
|
||||
// Вспомогательная функция для сравнения int32_t
|
||||
static int Filter_int32_compare(const void *a, const void *b) {
|
||||
int32_t ia = *(const int32_t*)a;
|
||||
int32_t ib = *(const int32_t*)b;
|
||||
if (ia < ib) return -1;
|
||||
if (ia > ib) return 1;
|
||||
/**
|
||||
* @brief Инициализация RMS фильтра (float)
|
||||
*/
|
||||
int FilterRMS_Init(FilterRMS_t* filter, uint32_t window_size) {
|
||||
check_init_filter(filter);
|
||||
|
||||
if (window_size == 0 || window_size > FILTER_RMS_MAX_SIZE) return -1;
|
||||
|
||||
filter->window_size = window_size;
|
||||
filter->sum_squares = 0.0f;
|
||||
filter->last_rms = 0.0f;
|
||||
filter->index = 0;
|
||||
filter->count = 0;
|
||||
|
||||
// Инициализируем буфер нулями
|
||||
memset(filter->buffer_sq, 0, sizeof(filter->buffer_sq[0]) * window_size);
|
||||
|
||||
filter->state = FILTER_READY;
|
||||
filter->reset = &FilterRMS_Init;
|
||||
filter->process = &FilterRMS_Process;
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Обработка значения RMS фильтром (float)
|
||||
* @details Эффективный алгоритм с циклическим буфером
|
||||
*/
|
||||
float FilterRMS_Process(FilterRMS_t* filter, float input) {
|
||||
check_process_filter(filter);
|
||||
|
||||
// Вычисляем квадрат входного значения
|
||||
float square = input * input;
|
||||
|
||||
if (filter->count < filter->window_size) {
|
||||
// Фаза накопления - просто добавляем в сумму
|
||||
filter->sum_squares += square;
|
||||
filter->buffer_sq[filter->index] = square;
|
||||
filter->count++;
|
||||
} else {
|
||||
// Фаза скользящего окна - вычитаем старое, добавляем новое
|
||||
filter->sum_squares -= filter->buffer_sq[filter->index]; // Вычитаем старое значение
|
||||
filter->sum_squares += square; // Добавляем новое
|
||||
filter->buffer_sq[filter->index] = square; // Сохраняем в буфер
|
||||
}
|
||||
|
||||
// Увеличиваем индекс с зацикливанием
|
||||
filter->index++;
|
||||
if (filter->index >= filter->window_size) {
|
||||
filter->index = 0;
|
||||
}
|
||||
|
||||
// Вычисляем RMS (проверяем что есть хотя бы одно значение)
|
||||
if (filter->count > 0) {
|
||||
float mean_square = filter->sum_squares / filter->count;
|
||||
// Защита от отрицательных значений из-за ошибок округления
|
||||
if (mean_square < 0.0f) mean_square = 0.0f;
|
||||
filter->last_rms = _sqrtf(mean_square);
|
||||
}
|
||||
|
||||
return filter->last_rms;
|
||||
}
|
||||
|
||||
|
||||
// ==================== INT32_T ВЕРСИИ ====================
|
||||
|
||||
//// Вспомогательная функция для сравнения int32_t
|
||||
//static int Filter_int32_compare(const void *a, const void *b) {
|
||||
// int32_t ia = *(const int32_t*)a;
|
||||
// int32_t ib = *(const int32_t*)b;
|
||||
// if (ia < ib) return -1;
|
||||
// if (ia > ib) return 1;
|
||||
// return 0;
|
||||
//}
|
||||
|
||||
/**
|
||||
* @brief Инициализация медианного фильтра (int32_t)
|
||||
* @param filter Указатель на структуру фильтра
|
||||
* @return 0 - успех, -1 - ошибка
|
||||
*/
|
||||
int FilterMedianInt_Init(FilterMedianInt_t* filter, uint8_t size) {
|
||||
int FilterMedianInt_Init(FilterMedianInt_t* filter, uint8_t size, int32_t init_val) {
|
||||
check_init_filter(filter);
|
||||
if (size == 0 || size > FILTER_MEDIAN_MAX_SIZE) return -1;
|
||||
|
||||
memset(filter->buffer, 0, sizeof(filter->buffer));
|
||||
for (int i = 0; i < size; i++)
|
||||
{
|
||||
filter->buffer[i] = init_val;
|
||||
filter->sorted[i] = init_val;
|
||||
}
|
||||
|
||||
filter->index = 0;
|
||||
filter->size = size;
|
||||
|
||||
@@ -316,19 +394,69 @@ int FilterMedianInt_Init(FilterMedianInt_t* filter, uint8_t size) {
|
||||
* @return Отфильтрованное значение
|
||||
*/
|
||||
int32_t FilterMedianInt_Process(FilterMedianInt_t* filter, int32_t input) {
|
||||
check_process_filter(filter);
|
||||
|
||||
// Добавляем значение в буфер
|
||||
filter->buffer[filter->index] = input;
|
||||
filter->index = (filter->index + 1) % filter->size;
|
||||
|
||||
// Копируем буфер для сортировки
|
||||
int32_t sort_buffer[FILTER_MEDIAN_MAX_SIZE];
|
||||
memcpy(sort_buffer, filter->buffer, sizeof(sort_buffer));
|
||||
|
||||
// Сортируем и возвращаем медиану
|
||||
qsort(sort_buffer, filter->size, sizeof(int32_t), Filter_int32_compare);
|
||||
return sort_buffer[filter->size / 2];
|
||||
check_process_filter(filter);
|
||||
|
||||
register int32_t old_value = filter->buffer[filter->index];
|
||||
register uint8_t size = filter->size;
|
||||
register uint8_t idx = filter->index;
|
||||
int32_t* sorted = filter->sorted;
|
||||
|
||||
// Обновляем circular buffer
|
||||
filter->buffer[idx] = input;
|
||||
idx++;
|
||||
if (idx >= size) idx = 0;
|
||||
filter->index = idx;
|
||||
|
||||
// Одновременно ищем позицию для удаления и вставки
|
||||
uint8_t remove_pos = 0;
|
||||
uint8_t insert_pos = 0;
|
||||
uint8_t found_remove = 0;
|
||||
|
||||
// Проход по массиву sorted (только один раз!)
|
||||
for (uint8_t i = 0; i < size; i++) {
|
||||
int32_t current = sorted[i];
|
||||
|
||||
// Ищем позицию для удаления старого значения
|
||||
if (!found_remove && current == old_value) {
|
||||
remove_pos = i;
|
||||
found_remove = 1;
|
||||
}
|
||||
|
||||
// Ищем позицию для вставки нового значения
|
||||
if (input > current) {
|
||||
insert_pos = i + 1;
|
||||
}
|
||||
}
|
||||
|
||||
// Если insert_pos указывает на место после удаляемого элемента,
|
||||
// нужно скорректировать, так как массив уменьшится на 1 элемент
|
||||
if (insert_pos > remove_pos) {
|
||||
insert_pos--;
|
||||
}
|
||||
|
||||
// Оптимизированное удаление и вставка за один проход
|
||||
if (insert_pos == remove_pos) {
|
||||
// Случай 1: Вставляем на то же место, откуда удаляем
|
||||
sorted[remove_pos] = input;
|
||||
}
|
||||
else if (insert_pos < remove_pos) {
|
||||
// Случай 2: Вставляем левее, чем удаляем
|
||||
// Сдвигаем элементы [insert_pos, remove_pos-1] вправо
|
||||
for (uint8_t j = remove_pos; j > insert_pos; j--) {
|
||||
sorted[j] = sorted[j - 1];
|
||||
}
|
||||
sorted[insert_pos] = input;
|
||||
}
|
||||
else {
|
||||
// Случай 3: Вставляем правее, чем удаляем (insert_pos > remove_pos)
|
||||
// Сдвигаем элементы [remove_pos+1, insert_pos] влево
|
||||
for (uint8_t j = remove_pos; j < insert_pos; j++) {
|
||||
sorted[j] = sorted[j + 1];
|
||||
}
|
||||
sorted[insert_pos - 1] = input;
|
||||
}
|
||||
|
||||
return sorted[size >> 1];
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -380,11 +508,12 @@ int32_t FilterExpInt_Process(FilterExpInt_t* filter, int32_t input) {
|
||||
* @param filter Указатель на структуру фильтра
|
||||
* @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);
|
||||
if (size == 0 || size > FILTER_AVERAGE_MAX_SIZE) return - 1;
|
||||
|
||||
#ifndef FILTERS_DISABLE_MOVING_AVERAGE
|
||||
memset(filter->buffer, 0, sizeof(filter->buffer));
|
||||
#endif
|
||||
filter->size = size;
|
||||
filter->sum = 0;
|
||||
filter->index = 0;
|
||||
@@ -412,6 +541,7 @@ int32_t FilterAverageInt_Process(FilterAverageInt_t* filter, int32_t input) {
|
||||
|
||||
// Логика скользящего среднего
|
||||
if (filter->mode == FILTER_MODE_MOVING) {
|
||||
#ifndef FILTERS_DISABLE_MOVING_AVERAGE
|
||||
if (filter->count > filter->size) {
|
||||
filter->sum -= filter->buffer[filter->index];
|
||||
filter->count = filter->size; // Поддерживаем фиксированный размер окна
|
||||
@@ -419,10 +549,11 @@ int32_t FilterAverageInt_Process(FilterAverageInt_t* filter, int32_t input) {
|
||||
filter->buffer[filter->index] = input;
|
||||
filter->index = (filter->index + 1) % filter->size;
|
||||
filter->lastValue = filter->sum / filter->count;
|
||||
#endif
|
||||
}
|
||||
else
|
||||
{
|
||||
if (filter->count == filter->size)
|
||||
if (filter->count >= filter->size)
|
||||
{
|
||||
filter->lastValue = filter->sum / filter->count;
|
||||
filter->count = 0;
|
||||
@@ -562,6 +693,172 @@ int32_t FilterLUTInt_Process(FilterLUTInt_t* filter, int32_t input) {
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* @brief Инициализация RMS фильтра (int32_t)
|
||||
*/
|
||||
int FilterRMSInt_Init(FilterRMSInt_t* filter, uint32_t window_size) {
|
||||
check_init_filter(filter);
|
||||
|
||||
if (window_size == 0 || window_size > FILTER_RMS_MAX_SIZE) return -1;
|
||||
|
||||
filter->window_size = window_size;
|
||||
filter->sum_squares = 0;
|
||||
filter->last_rms = 0;
|
||||
filter->index = 0;
|
||||
filter->count = 0;
|
||||
|
||||
// Инициализируем буфер нулями
|
||||
memset(filter->buffer_sq, 0, sizeof(filter->buffer_sq[0]) * window_size);
|
||||
|
||||
filter->state = FILTER_READY;
|
||||
filter->reset = &FilterRMSInt_Init;
|
||||
filter->process = &FilterRMSInt_Process;
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Обработка значения RMS фильтром (int32_t)
|
||||
*/
|
||||
int32_t FilterRMSInt_Process(FilterRMSInt_t* filter, int32_t input) {
|
||||
check_process_filter(filter);
|
||||
|
||||
// Вычисляем квадрат входного значения
|
||||
int64_t square = (int64_t)input * input;
|
||||
|
||||
if (filter->count < filter->window_size) {
|
||||
// Фаза накопления
|
||||
filter->sum_squares += square;
|
||||
filter->buffer_sq[filter->index] = square;
|
||||
filter->count++;
|
||||
} else {
|
||||
// Фаза скользящего окна
|
||||
filter->sum_squares -= filter->buffer_sq[filter->index]; // Вычитаем старое
|
||||
filter->sum_squares += square; // Добавляем новое
|
||||
filter->buffer_sq[filter->index] = square; // Сохраняем в буфер
|
||||
}
|
||||
|
||||
// Увеличиваем индекс с зацикливанием
|
||||
filter->index++;
|
||||
if (filter->index >= filter->window_size) {
|
||||
filter->index = 0;
|
||||
}
|
||||
|
||||
// Вычисляем RMS
|
||||
if (filter->count > 0) {
|
||||
int64_t mean_square = filter->sum_squares / filter->count;
|
||||
// Защита от отрицательных значений
|
||||
if (mean_square < 0) mean_square = 0;
|
||||
filter->last_rms = (int32_t)_sqrtf((float)mean_square);
|
||||
}
|
||||
|
||||
return filter->last_rms;
|
||||
}
|
||||
|
||||
|
||||
|
||||
// ==================== ДРУГИЕ ФИЛЬТРЫ ====================
|
||||
|
||||
/**
|
||||
* @brief Инициализация полосового фильтра с дифференциатором
|
||||
* @param filter Указатель на структуру фильтра
|
||||
* @param center_freq_ratio Отношение центральной частоты к частоте дискретизации (0..0.5)
|
||||
* Например: 50 Гц / 1000 Гц = 0.05
|
||||
* @param bandwidth_ratio Относительная ширина полосы (0..1)
|
||||
* Например: 0.1 = полоса 10% от центральной частоты
|
||||
* @return 0 - успех, -1 - ошибка
|
||||
*/
|
||||
int FilterBandPassDerivative_Init(FilterBandPassDerivative_t* filter,
|
||||
float center_freq_ratio,
|
||||
float bandwidth_ratio) {
|
||||
check_init_filter(filter);
|
||||
|
||||
// Проверка параметров
|
||||
if (center_freq_ratio <= 0.0f || center_freq_ratio >= 0.5f) return -1;
|
||||
if (bandwidth_ratio <= 0.0f || bandwidth_ratio > 1.0f) return -1;
|
||||
|
||||
// 1. Расчет параметров полосового фильтра
|
||||
float w0 = 2.0f * PI * center_freq_ratio; // Нормированная угловая частота
|
||||
float Q = 1.0f / bandwidth_ratio; // Добротность
|
||||
float alpha = sinf(w0) / (2.0f * Q);
|
||||
float cos_w0 = cosf(w0);
|
||||
|
||||
// Коэффициенты биквадратного полосового фильтра
|
||||
// H_bp(z) = (alpha - alpha*z^-2) / (1 + a1*z^-1 + a2*z^-2)
|
||||
float b0_bp = alpha;
|
||||
float b1_bp = 0.0f;
|
||||
float b2_bp = -alpha;
|
||||
float a0_bp = 1.0f + alpha;
|
||||
float a1_bp = -2.0f * cos_w0;
|
||||
float a2_bp = 1.0f - alpha;
|
||||
|
||||
// Нормализация (a0 = 1)
|
||||
filter->b0 = b0_bp / a0_bp;
|
||||
filter->b1 = b1_bp / a0_bp;
|
||||
filter->b2 = b2_bp / a0_bp;
|
||||
filter->a1 = a1_bp / a0_bp;
|
||||
filter->a2 = a2_bp / a0_bp;
|
||||
|
||||
// 2. Дифференциатор реализуется отдельно в process()
|
||||
filter->prev_input = 0.0f;
|
||||
|
||||
// 3. Инициализация состояний фильтра
|
||||
filter->x1 = 0.0f;
|
||||
filter->x2 = 0.0f;
|
||||
filter->y1 = 0.0f;
|
||||
filter->y2 = 0.0f;
|
||||
filter->last_output = 0.0f;
|
||||
filter->dataProcessing = 0;
|
||||
|
||||
filter->state = FILTER_READY;
|
||||
filter->reset = &FilterBandPassDerivative_Init;
|
||||
filter->process = &FilterBandPassDerivative_Process;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Обработка значения фильтром
|
||||
* @param filter Указатель на структуру фильтра
|
||||
* @param input Входное значение в диапазоне [-1.0, 1.0]
|
||||
* @return Отфильтрованное значение
|
||||
*
|
||||
* @details Алгоритм:
|
||||
* 1. Вычисление производной: diff = input - prev_input
|
||||
* 2. Полосовая фильтрация diff
|
||||
* 3. Обновление состояний
|
||||
*/
|
||||
float FilterBandPassDerivative_Process(FilterBandPassDerivative_t* filter,
|
||||
float input) {
|
||||
check_process_filter(filter);
|
||||
|
||||
// 1. Дифференциатор (разностный фильтр 1-го порядка)
|
||||
//float diff = input - filter->prev_input;
|
||||
//filter->prev_input = input;
|
||||
float diff = input;
|
||||
|
||||
// 2. Полосовой фильтр (биквадратный, прямая форма II)
|
||||
// y[n] = b0*x[n] + b1*x[n-1] + b2*x[n-2] - a1*y[n-1] - a2*y[n-2]
|
||||
float output = filter->b0 * diff +
|
||||
filter->b1 * filter->x1 +
|
||||
filter->b2 * filter->x2 -
|
||||
filter->a1 * filter->y1 -
|
||||
filter->a2 * filter->y2;
|
||||
|
||||
// 3. Обновление состояний
|
||||
filter->x2 = filter->x1; // x[n-2] = x[n-1]
|
||||
filter->x1 = diff; // x[n-1] = x[n]
|
||||
filter->y2 = filter->y1; // y[n-2] = y[n-1]
|
||||
filter->y1 = output; // y[n-1] = y[n]
|
||||
|
||||
filter->last_output = output;
|
||||
filter->dataProcessing = 0; // Данные обработаны
|
||||
|
||||
return output;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
#ifdef DSP_FITLERS
|
||||
|
||||
/**
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
# Обзор `ExtendedLibs`
|
||||
|
||||
ExtendedLibs - это набор библиотек для удобной работы с STM32. Данный субмодуль подключается напрямую из Git и содержит набор вспомогательных библиотек для работы МК, в частности STM32 и SEGGER RTT.
|
||||
ExtendedLibs - это набор библиотек для удобной работы с МК. Данный субмодуль подключается напрямую из Git и содержит набор вспомогательных библиотек для работы МК, в частности STM32 и SEGGER RTT.
|
||||
|
||||
## Основные возможности
|
||||
|
||||
@@ -28,11 +28,13 @@ ProjectRoot/
|
||||
│ │ ├── __mylibs_include.h # Главный include файл
|
||||
│ │ ├── __mylibs_config.h # Конфигурация библиотек
|
||||
│ │ ├── mylibs_defs.h # Общие определения и макросы
|
||||
│ │ ├── filters.h # Объявления функций для фильтрации
|
||||
│ │ ├── bit_access.h # Битовый доступ к регистрам
|
||||
│ │ ├── gen_optimizer.h # Оптимизатор (генетический алгоритм)
|
||||
│ │ ├── trackers.h # Трекеры для отладки
|
||||
│ │ └── trace.h # Трассировка и логирование
|
||||
│ └── src/
|
||||
│ └── filters.с # Реализация фильтров
|
||||
│
|
||||
└──RTT # Библиотека RTT
|
||||
├── __SEGGER_RTT_Conf.h # Конфигурационный файл RTT
|
||||
|
||||
@@ -22,8 +22,8 @@
|
||||
* @{
|
||||
*/
|
||||
|
||||
#define TRACKERS_ENABLE ///< Включить трекеры
|
||||
#define SERIAL_TRACE_ENABLE ///< Включить serial трассировку
|
||||
//#define TRACKERS_ENABLE ///< Включить трекеры
|
||||
//#define SERIAL_TRACE_ENABLE ///< Включить serial трассировку
|
||||
#define RTT_TRACE_ENABLE ///< Включить serial трассировку через RTT
|
||||
#define SWO_TRACE_ENABLE ///< Включить serial трассировку через SWO
|
||||
/**
|
||||
@@ -54,6 +54,25 @@
|
||||
*/
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* @addtogroup FILTER_CONFIG Filter configs
|
||||
* @ingroup MYLIBS_CONFIG
|
||||
* @brief Конфигурация фильтров
|
||||
* @{
|
||||
*/
|
||||
|
||||
|
||||
//#define FILTERS_ENABLE ///< Включить библиотеку фильтров
|
||||
//#define FILTER_MEDIAN_MAX_SIZE ///< Максимальный размер окна медианного фильтра (по умолчанию 5)
|
||||
//#define FILTER_AVERAGE_MAX_SIZE ///< Максимальный размер окна усредняющего фильтра (по умолчанию 8)
|
||||
//#define FILTER_POLY_MAX_ORDER ///< Максимальный порядок полинома (по умолчанию 4)
|
||||
#define FILTERS_DISABLE_MOVING_AVERAGE
|
||||
/** GEN_CONFIG
|
||||
* @}
|
||||
*/
|
||||
|
||||
|
||||
/**
|
||||
* @addtogroup GEN_CONFIG Genetic configs
|
||||
* @ingroup MYLIBS_CONFIG
|
||||
@@ -61,7 +80,7 @@
|
||||
* @{
|
||||
*/
|
||||
|
||||
#define GEN_OPTIMIZATION_ENABLE ///< Включить оптимизацию параметров
|
||||
//#define GEN_OPTIMIZATION_ENABLE ///< Включить оптимизацию параметров
|
||||
#define GEN_MAX_PARAMS 20 ///< Максимальное количество параметров
|
||||
#define GEN_MAX_CANDIDATES 100 ///< Максимальное количество кандидатов для обучения
|
||||
|
||||
@@ -78,7 +97,7 @@
|
||||
*/
|
||||
|
||||
|
||||
#define BENCH_TIME_ENABLE ///< Включить бенч времени
|
||||
//#define BENCH_TIME_ENABLE ///< Включить бенч времени
|
||||
#define BENCH_TIME_MAX_CHANNELS 16 ///< Максимальное количество каналов измерения
|
||||
|
||||
/** GEN_CONFIG
|
||||
@@ -101,7 +120,7 @@
|
||||
#define INCLUDE_TRACKERS_LIB ///< Подключить библиотеку с трекерами
|
||||
#define INCLUDE_TRACE_LIB ///< Подключить библиотеку с трейсами
|
||||
#define INCLUDE_GENERAL_PERIPH_LIBS ///< Подключить библиотеку с периферией
|
||||
#define FREERTOS_DELAY ///< Использовать FreeRTOS задержку, вместо HAL
|
||||
//#define FREERTOS_DELAY ///< Использовать FreeRTOS задержку, вместо HAL
|
||||
|
||||
/** LIBS_CONFIG
|
||||
* @}
|
||||
|
||||
@@ -37,58 +37,16 @@
|
||||
#ifdef INCLUDE_TRACKERS_LIB
|
||||
#include "trackers.h"
|
||||
#else
|
||||
#define TrackerTypeDef(num_user_vars) void *
|
||||
#define num_of_usercnts(_user_) 0
|
||||
#define assert_tracecnt(_cntstruct_, _uservarnumb_) 0
|
||||
#define if_assert_usertracker(_cntstruct_, _uservarnumb_) if(0)
|
||||
#define tern_assert_usertracker(_cntstruct_, _uservarnumb_) 0
|
||||
#define TrackerGet_Ok(_cntstruct_) dummy
|
||||
#define TrackerGet_Err(_cntstruct_) dummy
|
||||
#define TrackerGet_Warn(_cntstruct_) dummy
|
||||
#define TrackerGet_User(_cntstruct_, _uservarnumb_) dummy
|
||||
#define TrackerCnt_Ok(_cntstruct_)
|
||||
#define TrackerCnt_Err(_cntstruct_)
|
||||
#define TrackerCnt_Warn(_cntstruct_)
|
||||
#define TrackerCnt_User(_cntstruct_, _uservarnumb_)
|
||||
#define TrackerWrite_User(_cntstruct_, _uservarnumb_, _val_)
|
||||
#define TrackerClear_All(_cntstruct_)
|
||||
#define TrackerClear_Ok(_cntstruct_)
|
||||
#define TrackerClear_Err(_cntstruct_)
|
||||
#define TrackerClear_Warn(_cntstruct_)
|
||||
#define TrackerClear_User(_cntstruct_)
|
||||
#define TrackerClear_UserAll(_cntstruct_)
|
||||
#endif
|
||||
|
||||
#ifdef INCLUDE_TRACE_LIB
|
||||
#include "trace.h"
|
||||
#else
|
||||
#define my_printf(...)
|
||||
#define log_printf(TAG, fmt, ...)
|
||||
#define TRACE_GPIO_SET(_gpio_,_pin_)
|
||||
#define TRACE_GPIO_RESET(_gpio_,_pin_)
|
||||
#define RTT_FlashPrepare(...)
|
||||
#define RTT_EraseFlash(...) 0
|
||||
#define RTT_SaveToFlash(...) 0
|
||||
#define RTT_ReadFromFlash(...) 0
|
||||
#define HF_CheckRecovered(...) 0
|
||||
#define HF_HandleFault(...)
|
||||
#endif
|
||||
|
||||
#ifdef INCLUDE_GEN_OPTIMIZER
|
||||
#include "gen_optimizer.h"
|
||||
#else
|
||||
typedef struct {
|
||||
uint16_t n_params;
|
||||
uint16_t n_cand;
|
||||
uint16_t n_best;
|
||||
uint16_t iq_mutation;
|
||||
int32_t loss[0];
|
||||
int32_t candidates[0][0];
|
||||
} GenOptimizer_t;
|
||||
#define GenOptimizer_Init(opt, n_params, n_cand, n_best, iq_mutation, start_params)
|
||||
#define GenOptimizer_Step(opt, params, LossFunc)
|
||||
#define PARAM_SCALE_Q16(x, min_val, max_val) (x)
|
||||
#define PARAM_UNSCALE_Q16(q16_val, min_val, max_val) (q16_val)
|
||||
#endif
|
||||
|
||||
|
||||
@@ -96,17 +54,15 @@ typedef struct {
|
||||
#ifdef INCLUDE_BENCH_TEST
|
||||
#include "bench_time.h"
|
||||
#else //BENCH_TIME_ENABLE
|
||||
#define BenchTime_Init()
|
||||
#define BenchTime_Start(channel, ticks, tick_period) 0
|
||||
#define BenchTime_End(channel, ticks) 0
|
||||
#define BenchTime_GetMin(channel) 0
|
||||
#define BenchTime_GetMax(channel) 0
|
||||
#define BenchTime_GetAverage(channel) 0
|
||||
#define BenchTime_GetCount(channel) 0
|
||||
#define BenchTime_GetLast(channel) 0
|
||||
#define BenchTime_ResetStats(channel)
|
||||
#endif //BENCH_TIME_ENABLE
|
||||
|
||||
|
||||
#ifdef INCLUDE_FILTERS
|
||||
#include "filters.h"
|
||||
#else //INCLUDE_FILTERS
|
||||
#endif //INCLUDE_FILTERS
|
||||
|
||||
|
||||
#ifdef INCLUDE_GENERAL_PERIPH_LIBS
|
||||
|
||||
#include "__general_flash.h"
|
||||
|
||||
Reference in New Issue
Block a user