kfr

Fast, modern C++ DSP framework, FFT, Sample Rate Conversion, FIR/IIR/Biquad Filters (SSE, AVX, AVX-512, ARM NEON)
Log | Files | Refs | README

commit 32b1dc2e67c08b969033ddf4accae65ce42ad176
parent 7c946c1e10e64d8662502955a338bd8e20178e13
Author: d.levin256@gmail.com <d.levin256@gmail.com>
Date:   Wed,  9 Nov 2016 12:55:37 +0300

New filter API

Diffstat:
Minclude/kfr/base.hpp | 1+
Ainclude/kfr/base/filter.hpp | 85+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Msources.cmake | 1+
Mtests/dsp_test.cpp | 2+-
4 files changed, 88 insertions(+), 1 deletion(-)

diff --git a/include/kfr/base.hpp b/include/kfr/base.hpp @@ -33,6 +33,7 @@ #include "base/constants.hpp" #include "base/digitreverse.hpp" #include "base/expression.hpp" +#include "base/filter.hpp" #include "base/function.hpp" #include "base/gamma.hpp" #include "base/generators.hpp" diff --git a/include/kfr/base/filter.hpp b/include/kfr/base/filter.hpp @@ -0,0 +1,85 @@ +/** @addtogroup math + * @{ + */ +/* + Copyright (C) 2016 D Levin (https://www.kfrlib.com) + This file is part of KFR + + KFR is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + KFR 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 KFR. + + If GPL is not suitable for your project, you must purchase a commercial license to use KFR. + Buying a commercial license is mandatory as soon as you develop commercial activities without + disclosing the source code of your own applications. + See https://www.kfrlib.com for details. + */ +#pragma once + +#include "basic_expressions.hpp" +#include "pointer.hpp" +#include "univector.hpp" + +namespace kfr +{ + +template <typename T> +class filter +{ +public: + virtual ~filter() {} + virtual void reset() {} + + template <size_t Tag> + void apply(univector<T, Tag>& buffer) + { + process_buffer(buffer.data(), buffer.data(), buffer.size()); + } + + template <size_t Tag1, size_t Tag2> + void apply(univector<T, Tag1>& dest, const univector<T, Tag2>& src) + { + process_buffer(dest.data(), src.data(), std::min(dest.size(), src.size())); + } + + void apply(T* buffer, size_t size) { process_buffer(buffer, buffer, size); } + + void apply(T* dest, const T* src, size_t size) { process_buffer(dest, src, size); } + + template <size_t Tag> + void apply(univector<T, Tag>& dest, const expression_pointer<T>& src) + { + process_expression(dest.data(), src, size_min(dest.size(), src.size())); + } + + void apply(T* dest, const expression_pointer<T>& src, size_t size) + { + process_expression(dest, src, size_min(size, src.size())); + } + + template <size_t Tag, typename Expr, KFR_ENABLE_IF(is_input_expression<Expr>::value)> + void apply(univector<T, Tag>& dest, const Expr& src) + { + process_expression(dest.data(), to_pointer(src), size_min(dest.size(), src.size())); + } + + template <typename Expr, KFR_ENABLE_IF(is_input_expression<Expr>::value)> + void apply(T* dest, const Expr& src, size_t size) + { + process_expression(dest, to_pointer(src), size_min(size, src.size())); + } + +private: + virtual void process_buffer(T* dest, const T* src, size_t size) = 0; + virtual void process_expression(T* dest, const expression_pointer<T>& src, size_t size) = 0; +}; +} diff --git a/sources.cmake b/sources.cmake @@ -26,6 +26,7 @@ set( ${PROJECT_SOURCE_DIR}/include/kfr/base/constants.hpp ${PROJECT_SOURCE_DIR}/include/kfr/base/digitreverse.hpp ${PROJECT_SOURCE_DIR}/include/kfr/base/expression.hpp + ${PROJECT_SOURCE_DIR}/include/kfr/base/filter.hpp ${PROJECT_SOURCE_DIR}/include/kfr/base/function.hpp ${PROJECT_SOURCE_DIR}/include/kfr/base/gamma.hpp ${PROJECT_SOURCE_DIR}/include/kfr/base/generators.hpp diff --git a/tests/dsp_test.cpp b/tests/dsp_test.cpp @@ -67,7 +67,7 @@ TEST(phasor) TEST(fir) { const univector<double, 100> data = counter() + sequence(1, 2, -10, 100) + sequence(0, -7, 0.5); - const univector<double, 7> taps{ 1, 2, -2, 0.5, 0.0625, 4, -8 }; + const univector<double, 6> taps{ 1, 2, -2, 0.5, 0.0625, 4 }; CHECK_EXPRESSION(fir(data, taps), 100, [&](size_t index) -> double { double result = 0.0;