From c33d644fb0382662474979ddf78969b80e3e4bd2 Mon Sep 17 00:00:00 2001 From: robtor Date: Wed, 18 Jun 2025 13:51:40 +0200 Subject: [PATCH] simple crude fir filter --- Tests/CMakeLists.txt | 19 ++++++++++++++++ Tests/modules.cpp | 18 ++++++++++++++++ floatpump/Modules/filter_fir_crude.cpp | 30 ++++++++++++++++++++++++++ floatpump/Modules/filter_fir_crude.h | 19 ++++++++++++++++ 4 files changed, 86 insertions(+) create mode 100644 Tests/modules.cpp create mode 100644 floatpump/Modules/filter_fir_crude.cpp create mode 100644 floatpump/Modules/filter_fir_crude.h diff --git a/Tests/CMakeLists.txt b/Tests/CMakeLists.txt index 534e69c..ec367d0 100644 --- a/Tests/CMakeLists.txt +++ b/Tests/CMakeLists.txt @@ -3,6 +3,25 @@ add_executable(testtests experiments/example-test.cpp) target_link_libraries(testtests PRIVATE Catch2::Catch2WithMain) + +# Firmware modules tests +file(GLOB_RECURSE FWMODULES_SOURCES + "${CMAKE_SOURCE_DIR}/floatpump/Modules/*.cpp" +) + +add_executable(fwmodules + ${FWMODULES_SOURCES} + modules.cpp +) + + + +target_include_directories(fwmodules PUBLIC + ${CMAKE_SOURCE_DIR}/floatpump/Modules +) + +target_link_libraries(fwmodules PRIVATE Catch2::Catch2WithMain) + # General catch configuration include(CTest) include(Catch) diff --git a/Tests/modules.cpp b/Tests/modules.cpp new file mode 100644 index 0000000..42e971e --- /dev/null +++ b/Tests/modules.cpp @@ -0,0 +1,18 @@ +#include + +#include "filter_fir_crude.h" + +TEST_CASE("Filter Modules", "[filter]") +{ + SECTION("FIR Filter Crude Implementation") + { + float coeffs[] = {1,1,1}; + FilterFIRCrude filter1(coeffs, 3); + + filter1.process(10); + filter1.process(10); + auto res = filter1.process(10); + + REQUIRE(res == 10); + } +} \ No newline at end of file diff --git a/floatpump/Modules/filter_fir_crude.cpp b/floatpump/Modules/filter_fir_crude.cpp new file mode 100644 index 0000000..6017e87 --- /dev/null +++ b/floatpump/Modules/filter_fir_crude.cpp @@ -0,0 +1,30 @@ +#include "filter_fir_crude.h" + +FilterFIRCrude::FilterFIRCrude(const float* coeffs, size_t numTaps) : +_coeffs(coeffs), _histsize(numTaps), _index(0) +{ + _history = new float[_histsize]; + std::memset(_history, 0 , numTaps*sizeof(float)); +} + +float FilterFIRCrude::process(float input) +{ + _history[_index] = input; + + float result = 0.0f; + size_t idx = _index; + for (size_t i = 0; i < _histsize; ++i) + { + result += _coeffs[i] * _history[idx]; + if (idx == 0) + { + idx = _histsize - 1; + } else + { + --idx; + } + _index = (_index + 1) % _histsize; + } + return result; +} + diff --git a/floatpump/Modules/filter_fir_crude.h b/floatpump/Modules/filter_fir_crude.h new file mode 100644 index 0000000..d127dc1 --- /dev/null +++ b/floatpump/Modules/filter_fir_crude.h @@ -0,0 +1,19 @@ +#pragma once + +#include +#include +#include + +class FilterFIRCrude +{ +public: + FilterFIRCrude(const float* coeffs, size_t numTaps); + float process(float input); + +private: + const float* _coeffs; + size_t _histsize; + float* _history; + size_t _index; +}; +