From 4524acfce1eab700c3781e0e40f6357b0cf2c24c Mon Sep 17 00:00:00 2001 From: "J.A. de Jong - Redu-Sone B.V., ASCEE V.O.F" Date: Tue, 5 Mar 2024 09:53:23 +0100 Subject: [PATCH 1/2] Const-correct initialization with one constructor --- Fir1.cpp | 2 +- Fir1.h | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/Fir1.cpp b/Fir1.cpp index 82edde1..a5bd01e 100644 --- a/Fir1.cpp +++ b/Fir1.cpp @@ -31,7 +31,7 @@ THE SOFTWARE. #include // give the filter an array of doubles for the coefficients -Fir1::Fir1(double *_coefficients, unsigned number_of_taps) : +Fir1::Fir1(const double *_coefficients,const unsigned number_of_taps) : coefficients(new double[number_of_taps]), buffer(new double[number_of_taps]()), taps(number_of_taps) { diff --git a/Fir1.h b/Fir1.h index 4b36bd6..1720963 100644 --- a/Fir1.h +++ b/Fir1.h @@ -63,7 +63,7 @@ class Fir1 { * \param coefficients Coefficients as double array. * \param number_of_taps Number of taps (needs to match the number of coefficients **/ - Fir1(double *coefficients, unsigned number_of_taps); + Fir1(const double *coefficients,const unsigned number_of_taps); /** Coefficients as a text file (for example from Python) * The number of taps is automatically detected From 839a341e8b0c885877d3946989f5ddad133297cd Mon Sep 17 00:00:00 2001 From: "J.A. de Jong - Redu-Sone B.V., ASCEE V.O.F" Date: Tue, 5 Mar 2024 09:56:46 +0100 Subject: [PATCH 2/2] Added setCoeff --- Fir1.cpp | 9 +++++++++ Fir1.h | 12 ++++++++++++ 2 files changed, 21 insertions(+) diff --git a/Fir1.cpp b/Fir1.cpp index a5bd01e..d99541f 100644 --- a/Fir1.cpp +++ b/Fir1.cpp @@ -107,4 +107,13 @@ void Fir1::getCoeff(double* coeff_data, unsigned number_of_taps) const { if (number_of_taps > taps) memset(&coeff_data[taps], 0, (number_of_taps - taps)*sizeof(double)); } +void Fir1::setCoeff(const double* coeff_data, const unsigned number_of_taps) { + + if (number_of_taps != taps) { + throw std::runtime_error("Invalid number of taps in new coefficient array"); + } + for (unsigned int i = 0; i < number_of_taps; i++) { + coefficients[i] = coeff_data[i]; + } +} diff --git a/Fir1.h b/Fir1.h index 1720963..107546a 100644 --- a/Fir1.h +++ b/Fir1.h @@ -169,6 +169,18 @@ class Fir1 { */ void getCoeff(double* coeff_data, unsigned number_of_taps) const; + /** + * @brief Externally sets the coefficient array. This is useful when the + * actually running filter is at a different place as where the updating + * filter is employed. + * + * @param coeff_data New coefficients to set. + * @param number_of_taps Number of taps in the coefficient array. If this is + * not equal to the number of taps used in this filter, a runtime error is + * thrown. + */ + void setCoeff(const double *coeff_data, const unsigned number_of_taps); + /** * Returns the coefficients as a vector **/