diff --git a/app/src/app_service/ecg_service/algo/MedianFilter.c b/app/src/app_service/ecg_service/algo/MedianFilter.c index a785900..db5f928 100644 --- a/app/src/app_service/ecg_service/algo/MedianFilter.c +++ b/app/src/app_service/ecg_service/algo/MedianFilter.c @@ -6,6 +6,7 @@ */ #include "MedianFilter.h" +#include int MEDIANFILTER_Init(sMedianFilter_t *medianFilter) { diff --git a/app/src/app_service/ecg_service/algo/iflytop_simple_filter.c b/app/src/app_service/ecg_service/algo/iflytop_simple_filter.c index 9f9d1fd..54d53ca 100644 --- a/app/src/app_service/ecg_service/algo/iflytop_simple_filter.c +++ b/app/src/app_service/ecg_service/algo/iflytop_simple_filter.c @@ -250,3 +250,21 @@ float BaselineDriftRemoval_Update(BaselineDriftRemoval_t *filter, float vin) { return min_data_in_fifo - mid; } + +void fix_delay_fifo_init(fix_delay_fifo_t *buffer, float *fifo, int fifosize) { + buffer->fifo = fifo; + buffer->fifosize = fifosize; + buffer->head = 0; + buffer->tail = 0; + memset(buffer->fifo, 0, fifosize * sizeof(float)); +} +float fix_delay_fifo_update(fix_delay_fifo_t *buffer, float vin) { + buffer->fifo[buffer->tail] = vin; + buffer->tail = (buffer->tail + 1) % buffer->fifosize; + float fifoheadval = 0; + if (buffer->tail == buffer->head) { + fifoheadval = buffer->fifo[buffer->head]; + buffer->head = (buffer->head + 1) % buffer->fifosize; + } + return fifoheadval; +} \ No newline at end of file diff --git a/app/src/app_service/ecg_service/algo/iflytop_simple_filter.h b/app/src/app_service/ecg_service/algo/iflytop_simple_filter.h index fdff1bf..a3be706 100644 --- a/app/src/app_service/ecg_service/algo/iflytop_simple_filter.h +++ b/app/src/app_service/ecg_service/algo/iflytop_simple_filter.h @@ -67,7 +67,7 @@ float NOTCHFilter_Update(NOTCHFilter_t *filter, float vin); // #ifdef __cplusplus // } // #endif -#define MEDIAN_FILTER_MAX_SIZE 50 +#define MEDIAN_FILTER_MAX_SIZE 200 typedef struct { float val[MEDIAN_FILTER_MAX_SIZE]; @@ -93,7 +93,6 @@ typedef struct { void SmoothingFilter_Init(SmoothingFilter_t *filter, int windows_size, bool enable); float SmoothingFilter_Update(SmoothingFilter_t *filter, float vin); - #define MEDIAN_FILTER_SIZE 300 typedef struct { float fifo[MEDIAN_FILTER_SIZE]; @@ -107,4 +106,16 @@ typedef struct { void BaselineDriftRemoval_Init(BaselineDriftRemoval_t *filter, int windows_size, bool enable); float BaselineDriftRemoval_Update(BaselineDriftRemoval_t *filter, float vin); +typedef struct { + float *fifo; + int fifosize; + + int head; + int tail; + +} fix_delay_fifo_t; + +void fix_delay_fifo_init(fix_delay_fifo_t *buffer, float *fifo, int fifosize); +float fix_delay_fifo_update(fix_delay_fifo_t *buffer, float vin); + #endif \ No newline at end of file diff --git a/app/src/app_service/ecg_service/ecg_algo.c b/app/src/app_service/ecg_service/ecg_algo.c index c54f37b..31a6590 100644 --- a/app/src/app_service/ecg_service/ecg_algo.c +++ b/app/src/app_service/ecg_service/ecg_algo.c @@ -8,38 +8,52 @@ #define REPORT_MEDIAN_WINDOWS_SIZE 151 // 必须是奇数 -LPFilterExt_t m_lp_filter; -HPFilterExt_t m_hp_filter; -NOTCHFilterExt_t m_notch_filter; +LPFilterExt_t m_lp_filter; +HPFilterExt_t m_hp_filter; +NOTCHFilterExt_t m_notch_filter; + static sMedianFilter_t report_medianFilter; static sMedianNode_t report_medianBuffer[REPORT_MEDIAN_WINDOWS_SIZE]; -SmoothingFilter_t m_report_smoothing_filter; -int32_t m_data_statistics_buf[STATISTICS_BUF_SIZE]; // 心率判断 -zdata_statistics_t m_data_statistics; +static float m_data_fifo[REPORT_MEDIAN_WINDOWS_SIZE / 2]; +fix_delay_fifo_t m_fix_delay_fifo; + +SmoothingFilter_t m_report_smoothing_filter; + +int32_t m_data_statistics_buf[STATISTICS_BUF_SIZE]; +zdata_statistics_t m_leadoff_statistics; // 脱落检测 int32_t reportdata; -bool m_leadoff; +bool m_leadoff; + +static int32_t m_frame_cnt; void ecg_algo_init() { ecg_algo_reset(); } void report_data_process(int32_t indata) { + m_frame_cnt++; float data = indata; data = HPFilterExt_update(&m_hp_filter, data); // 高通 data = LPFilterExt_update(&m_lp_filter, data); // 低通 data = NOTCHFilterExt_update(&m_notch_filter, data); // 带阻 - int medianValue = MEDIANFILTER_Insert(&report_medianFilter, data); - data = indata - medianValue; - data = SmoothingFilter_Update(&m_report_smoothing_filter, data); - reportdata = data; + float medianValue = MEDIANFILTER_Insert(&report_medianFilter, data); + float delay_data = fix_delay_fifo_update(&m_fix_delay_fifo, data); + + if (m_frame_cnt < REPORT_MEDIAN_WINDOWS_SIZE) { + reportdata = 0; + return; + } + data = delay_data - medianValue; + data = SmoothingFilter_Update(&m_report_smoothing_filter, data); + reportdata = data; } void leadoff_data_process(int32_t indata) { - zdata_statistics_push(&m_data_statistics, indata); - if (zdata_statistics_is_full(&m_data_statistics)) { - int32_t max = zdata_statistics_get_max(&m_data_statistics); - int32_t min = zdata_statistics_get_min(&m_data_statistics); + zdata_statistics_push(&m_leadoff_statistics, indata); + if (zdata_statistics_is_full(&m_leadoff_statistics)) { + int32_t max = zdata_statistics_get_max(&m_leadoff_statistics); + int32_t min = zdata_statistics_get_min(&m_leadoff_statistics); if (max - min > 45000) { m_leadoff = true; } else { @@ -70,9 +84,13 @@ void ecg_algo_reset() { SmoothingFilter_Init(&m_report_smoothing_filter, 8, true); - zdata_statistics_init(&m_data_statistics, m_data_statistics_buf, STATISTICS_BUF_SIZE); - zdata_statistics_clear(&m_data_statistics); + zdata_statistics_init(&m_leadoff_statistics, m_data_statistics_buf, STATISTICS_BUF_SIZE); + + fix_delay_fifo_init(&m_fix_delay_fifo, m_data_fifo, REPORT_MEDIAN_WINDOWS_SIZE / 2); + + zdata_statistics_clear(&m_leadoff_statistics); + m_frame_cnt = 0; } int32_t ecg_algo_get_report_data() { return reportdata; } -bool ecg_algo_get_leadoff_state() { return m_leadoff; } \ No newline at end of file +bool ecg_algo_get_leadoff_state() { return m_leadoff; } \ No newline at end of file diff --git a/test/a.exe b/test/a.exe new file mode 100644 index 0000000..d807dab Binary files /dev/null and b/test/a.exe differ diff --git a/test/test.c b/test/test.c index b4b75e4..ebfaf6e 100644 --- a/test/test.c +++ b/test/test.c @@ -14,9 +14,10 @@ int main() { medianFilter.medianBuffer = medianBuffer; MEDIANFILTER_Init(&medianFilter); + int newValue = 1; while (1) { - int newValue = rand() % 100; + newValue++; int medianValue = MEDIANFILTER_Insert(&medianFilter, newValue); printf("New value: %d \tMedian value: %d\r\n", newValue, medianValue);