From e3670434cae800d1d419cd65b39979539ff6ca1c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mario=20H=C3=BCttel?= Date: Thu, 19 Aug 2021 21:44:50 +0200 Subject: [PATCH] Add FIR filter code --- stm-firmware/fir-filter.c | 64 +++++++++++++++++++ .../include/reflow-controller/fir-filter.h | 37 +++++++++++ 2 files changed, 101 insertions(+) create mode 100644 stm-firmware/fir-filter.c create mode 100644 stm-firmware/include/reflow-controller/fir-filter.h diff --git a/stm-firmware/fir-filter.c b/stm-firmware/fir-filter.c new file mode 100644 index 0000000..955f600 --- /dev/null +++ b/stm-firmware/fir-filter.c @@ -0,0 +1,64 @@ +/* Reflow Oven Controller +* +* Copyright (C) 2021 Mario Hüttel +* +* This file is part of the Reflow Oven Controller Project. +* +* The reflow oven controller is free software: you can redistribute it and/or modify +* it under the terms of the GNU General Public License version 2 as +* published by the Free Software Foundation. +* +* The Reflow Oven Control Firmware is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU General Public License for more details. +* +* You should have received a copy of the GNU General Public License +* along with the reflow oven controller project. +* If not, see . +*/ + +#include + +void fir_filter_init_null(struct fir_filter_fixed *filter) +{ + uint32_t idx; + + if (!filter) + return; + filter->put_idx = 0U; + for (idx = 0; idx < filter->tap_count; idx++) + filter->data = 0; +} + + +int32_t fir_filter_process_sample(struct fir_filter_fixed *filter, int32_t sample) +{ + uint32_t tap_idx; + uint32_t coeff_idx; + int32_t output = 0; + + if (!filter) + return 0; + + /* Store the new data point */ + filter->data[filter->put_idx] = sample; + + coeff_idx = filter->put_idx; + + /* Apply fir taps on data */ + for (tap_idx = 0; tap_idx < filter->tap_count; tap_idx++) { + output += filter->data[tap_idx] * filter->coefficients[coeff_idx]; + + if (coeff_idx) + coeff_idx--; + else + coeff_idx = filter->tap_count-1; + } + + filter->put_idx++; + if (filter->put_idx >= filter->tap_count) + filter->put_idx = 0; + + return output; +} diff --git a/stm-firmware/include/reflow-controller/fir-filter.h b/stm-firmware/include/reflow-controller/fir-filter.h new file mode 100644 index 0000000..b9e4221 --- /dev/null +++ b/stm-firmware/include/reflow-controller/fir-filter.h @@ -0,0 +1,37 @@ +/* Reflow Oven Controller +* +* Copyright (C) 2021 Mario Hüttel +* +* This file is part of the Reflow Oven Controller Project. +* +* The reflow oven controller is free software: you can redistribute it and/or modify +* it under the terms of the GNU General Public License version 2 as +* published by the Free Software Foundation. +* +* The Reflow Oven Control Firmware is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU General Public License for more details. +* +* You should have received a copy of the GNU General Public License +* along with the reflow oven controller project. +* If not, see . +*/ + +#ifndef __FIR_FILTER_H__ +#define __FIR_FILTER_H__ + +#include + +struct fir_filter_fixed { + uint32_t tap_count; + const int32_t *coefficients; + int32_t *data; + uint32_t put_idx; +}; + +void fir_filter_init_null(struct fir_filter_fixed *filter); + +int32_t fir_filter_process_sample(struct fir_filter_fixed *filter, int32_t sample); + +#endif /* __FIR_FILTER_H__ */