From 34c90c248587f904296527b72194e9dabd8a2caf Mon Sep 17 00:00:00 2001 From: Silvano Seva Date: Sat, 9 Jul 2022 18:07:42 +0200 Subject: [PATCH] Moved FIR filter class to a dedicated header file --- openrtx/include/core/dsp.h | 83 ++-------------------- openrtx/include/core/fir.hpp | 97 ++++++++++++++++++++++++++ openrtx/include/protocols/M17/M17DSP.h | 2 +- openrtx/src/core/dsp.cpp | 17 ----- 4 files changed, 105 insertions(+), 94 deletions(-) create mode 100644 openrtx/include/core/fir.hpp diff --git a/openrtx/include/core/dsp.h b/openrtx/include/core/dsp.h index 610d81a3..63eb31c5 100644 --- a/openrtx/include/core/dsp.h +++ b/openrtx/include/core/dsp.h @@ -21,8 +21,13 @@ #ifndef DSP_H #define DSP_H -#include -#include +#include +#include +#include + +#ifdef __cplusplus +extern "C" { +#endif typedef int16_t audio_sample_t; @@ -31,14 +36,6 @@ typedef int16_t audio_sample_t; * input or output signals when implementing digital modes on OpenRTX. */ -#ifdef __cplusplus - -#include -#include - -extern "C" { -#endif - /** * Data structure holding the internal state of a filter. */ @@ -90,72 +87,6 @@ void dsp_invertPhase(audio_sample_t *buffer, uint16_t length); #ifdef __cplusplus } - -/** - * Class for FIR filter with configurable coefficients. - * Adapted from the original implementation by Rob Riggs, Mobilinkd LLC. - */ -template < size_t N > -class Fir -{ -public: - - /** - * Constructor. - * - * @param taps: reference to a std::array of floating poing values representing - * the FIR filter coefficients. - */ - Fir(const std::array< float, N >& taps) : taps(taps), pos(0) - { - reset(); - } - - /** - * Destructor. - */ - ~Fir() { } - - /** - * Perform one step of the FIR filter, computing a new output value given - * the input value and the history of previous input values. - * - * @param input: FIR input value for the current time step. - * @return FIR output as a function of the current and past input values. - */ - float operator()(const float& input) - { - hist[pos++] = input; - if(pos >= N) pos = 0; - - float result = 0.0; - size_t index = pos; - - for(size_t i = 0; i < N; i++) - { - index = (index != 0 ? index - 1 : N - 1); - result += hist[index] * taps[i]; - } - - return result; - } - - /** - * Reset FIR history, clearing the memory of past values. - */ - void reset() - { - hist.fill(0); - pos = 0; - } - -private: - - const std::array< float, N >& taps; ///< FIR filter coefficients. - std::array< float, N > hist; ///< History of past inputs. - size_t pos; ///< Current position in history. -}; - #endif // __cplusplus #endif /* DSP_H */ diff --git a/openrtx/include/core/fir.hpp b/openrtx/include/core/fir.hpp new file mode 100644 index 00000000..6c4931c3 --- /dev/null +++ b/openrtx/include/core/fir.hpp @@ -0,0 +1,97 @@ +/*************************************************************************** + * Copyright (C) 2022 by Federico Amedeo Izzo IU2NUO, * + * Niccolò Izzo IU2KIN, * + * Silvano Seva IU2KWO, * + * Frederik Saraci IU2NRO * + * * + * This program 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. * + * * + * This program 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 this program; if not, see * + ***************************************************************************/ + +#ifndef FIR_H +#define FIR_H + +#ifndef __cplusplus +#error This header is C++ only! +#endif + +#include +#include +#include + +/** + * Class for FIR filter with configurable coefficients. + * Adapted from the original implementation by Rob Riggs, Mobilinkd LLC. + */ +template < size_t N > +class Fir +{ +public: + + /** + * Constructor. + * + * @param taps: reference to a std::array of floating poing values representing + * the FIR filter coefficients. + */ + Fir(const std::array< float, N >& taps) : taps(taps), pos(0) + { + reset(); + } + + /** + * Destructor. + */ + ~Fir() { } + + /** + * Perform one step of the FIR filter, computing a new output value given + * the input value and the history of previous input values. + * + * @param input: FIR input value for the current time step. + * @return FIR output as a function of the current and past input values. + */ + float operator()(const float& input) + { + hist[pos] = input; + pos = (pos + 1) % N; + + float result = 0.0; + size_t index = pos; + + for(size_t i = 0; i < N; i++) + { + index = (index != 0 ? index - 1 : N - 1); + result += hist[index] * taps[i]; + } + + return result; + } + + /** + * Reset FIR history, clearing the memory of past values. + */ + void reset() + { + hist.fill(0); + pos = 0; + } + +private: + + const std::array< float, N >& taps; ///< FIR filter coefficients. + std::array< float, N > hist; ///< History of past inputs. + size_t pos; ///< Current position in history. +}; + +#endif /* DSP_H */ diff --git a/openrtx/include/protocols/M17/M17DSP.h b/openrtx/include/protocols/M17/M17DSP.h index a7ebca29..0dc5e7ac 100644 --- a/openrtx/include/protocols/M17/M17DSP.h +++ b/openrtx/include/protocols/M17/M17DSP.h @@ -27,7 +27,7 @@ #error This header is C++ only! #endif -#include +#include #include namespace M17 diff --git a/openrtx/src/core/dsp.cpp b/openrtx/src/core/dsp.cpp index d1688144..2de2889a 100644 --- a/openrtx/src/core/dsp.cpp +++ b/openrtx/src/core/dsp.cpp @@ -111,20 +111,3 @@ void dsp_invertPhase(audio_sample_t *buffer, uint16_t length) buffer[i] = -buffer[i]; } } - -template -void dsp_applyFIR(audio_sample_t *buffer, - uint16_t length, - std::array taps) -{ - for(int i = length - 1; i >= 0; i--) - { - float acc = 0.0f; - for(uint16_t j = 0; j < order; j++) - { - if (i >= j) - acc += buffer[i - j] * taps[j]; - } - buffer[i] = (audio_sample_t) acc; - } -}