diff --git a/Fir1.cpp b/Fir1.cpp index 82edde1..d99541f 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) { @@ -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 4b36bd6..107546a 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 @@ -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 **/