diff --git a/.github/workflows/cmake-single-platform.yml b/.github/workflows/cmake-single-platform.yml index f6e9f67..7c4be21 100644 --- a/.github/workflows/cmake-single-platform.yml +++ b/.github/workflows/cmake-single-platform.yml @@ -37,7 +37,7 @@ jobs: - name: Install Conan run: | - sudo pip install conan + pip install conan - name: Create profile run: conan profile detect --force diff --git a/include/sdsp/casc_2o_iir.h b/include/sdsp/casc_2o_iir.h index b44a53b..a950e87 100644 --- a/include/sdsp/casc_2o_iir.h +++ b/include/sdsp/casc_2o_iir.h @@ -5,51 +5,51 @@ namespace sdsp { -template +template class casc_2o_iir { private: - int pos{ 0 }; + int m_pos{ 0 }; - double gain{ 1.0 }; + double m_gain{ 1.0 }; - std::array, M + 1> mem{ 0 }; + std::array, m_t + 1> m_mem{ 0 }; - std::array, M> bCoeff{ 0 }; - std::array, M> aCoeff{ 0 }; + std::array, m_t> m_b_coeff{ 0 }; + std::array, m_t> m_a_coeff{ 0 }; - filter_type fType{ filter_type::none }; + filter_type m_f_type{ filter_type::none }; public: casc_2o_iir() { - static_assert(M % 2 == 0, "M must be even!"); + static_assert(m_t % 2 == 0, "M must be even!"); } - void copy_coeff_from(const casc_2o_iir &otherFilter) + void copy_coeff_from(const casc_2o_iir &other_filter) { - gain = otherFilter.gain; - bCoeff = otherFilter.bCoeff; - aCoeff = otherFilter.aCoeff; - fType = otherFilter.fType; + m_gain = other_filter.m_gain; + m_b_coeff = other_filter.m_b_coeff; + m_a_coeff = other_filter.m_a_coeff; + m_f_type = other_filter.m_f_type; } - template - void process(Iter begin, Iter end) + template + void process(iter_t begin, iter_t end) { constexpr int order{ 2 }; constexpr int j1{ 1 }; constexpr int j2{ 2 }; - int p{ pos }; + int p{ m_pos }; - std::array, M + 1> y = mem; + std::array, m_t + 1> y = m_mem; - std::array, M> b = bCoeff; + std::array, m_t> b = m_b_coeff; - std::array, M> a = aCoeff; + std::array, m_t> a = m_a_coeff; while (begin < end) { - y.at(0).at(p) = *begin * gain; + y.at(0).at(p) = *begin * m_gain; int d1{ p - j1 }; if (d1 < 0) @@ -61,7 +61,7 @@ class casc_2o_iir { uint j; - for (j = 0; j < M; j++) { + for (j = 0; j < m_t; j++) { y.at(j + 1).at(p) = y.at(j).at(p); y.at(j + 1).at(p) += y.at(j).at(d1) * b.at(j).at(j1) - y.at(j + 1).at(d1) * a.at(j).at(j1); @@ -75,34 +75,34 @@ class casc_2o_iir { p = 0; begin++; } - pos = p; - mem = y; + m_pos = p; + m_mem = y; } - void set_bp_coeff(double f0, double fs, double Q, double gainIn = 1.0) + void set_bp_coeff(double f0, double fs, double q, double gain_in = 1.0) { - gain = gainIn; - double q2{ 2 * Q }; - fType = filter_type::band_pass; + m_gain = gain_in; + double q2{ 2 * q }; + m_f_type = filter_type::band_pass; double e0{ 2 * M_PI * f0 / fs }; double dnm{ std::sin(e0) }; double de{ 2 * std::tan(e0 / q2) / dnm }; - for (unsigned int k = 0; k < M / 2; k++) { - double D{ 2 * std::sin((2 * k + 1) * M_PI / (2.0 * M)) }; + for (unsigned int k = 0; k < m_t / 2; k++) { + double d{ 2 * std::sin((2 * k + 1) * M_PI / (2.0 * m_t)) }; - double A{ (1 + de * de / 4.0) * 2 / D / de }; - double dk{ std::sqrt(de * D / (A + std::sqrt(A * A - 1))) }; + double a{ (1 + de * de / 4.0) * 2 / d / de }; + double dk{ std::sqrt(de * d / (a + std::sqrt(a * a - 1))) }; - double B{ D * de / dk / 2.0 }; - double W{ B + std::sqrt(B * B - 1) }; + double b{ d * de / dk / 2.0 }; + double w{ b + std::sqrt(b * b - 1) }; double t{ std::tan(e0 / 2.0) }; - double e1{ 2.0 * std::atan(t / W) }; - double e2{ 2.0 * std::atan(W * t) }; + double e1{ 2.0 * std::atan(t / w) }; + double e2{ 2.0 * std::atan(w * t) }; t = dk * std::sin(e1) / 2.0; dnm = (1 + t); @@ -115,36 +115,36 @@ class casc_2o_iir { double gamma1{ (0.5 + beta1) * std::cos(e1) }; double gamma2{ (0.5 + beta2) * std::cos(e2) }; - t = std::sqrt(1 + (W - 1 / W) / dk * (W - 1 / W) / dk); + t = std::sqrt(1 + (w - 1 / w) / dk * (w - 1 / w) / dk); double alpha1{ (0.5 - beta1) * t / 2.0 }; double alpha2{ (0.5 - beta2) * t / 2.0 }; - gain *= 4 * alpha1 * alpha2; - - bCoeff.at(2 * k).at(0) = 1.0; - bCoeff.at(2 * k + 1).at(0) = 1.0; - bCoeff.at(2 * k).at(1) = 0; - bCoeff.at(2 * k + 1).at(1) = 0; - bCoeff.at(2 * k).at(2) = -1.0; - bCoeff.at(2 * k + 1).at(2) = -1.0; - - aCoeff.at(2 * k).at(0) = 1; - aCoeff.at(2 * k + 1).at(0) = 1; - aCoeff.at(2 * k).at(1) = -2 * gamma1; - aCoeff.at(2 * k + 1).at(1) = -2 * gamma2; - aCoeff.at(2 * k).at(2) = 2 * beta1; - aCoeff.at(2 * k + 1).at(2) = 2 * beta2; + m_gain *= 4 * alpha1 * alpha2; + + m_b_coeff.at(2 * k).at(0) = 1.0; + m_b_coeff.at(2 * k + 1).at(0) = 1.0; + m_b_coeff.at(2 * k).at(1) = 0; + m_b_coeff.at(2 * k + 1).at(1) = 0; + m_b_coeff.at(2 * k).at(2) = -1.0; + m_b_coeff.at(2 * k + 1).at(2) = -1.0; + + m_a_coeff.at(2 * k).at(0) = 1; + m_a_coeff.at(2 * k + 1).at(0) = 1; + m_a_coeff.at(2 * k).at(1) = -2 * gamma1; + m_a_coeff.at(2 * k + 1).at(1) = -2 * gamma2; + m_a_coeff.at(2 * k).at(2) = 2 * beta1; + m_a_coeff.at(2 * k + 1).at(2) = 2 * beta2; } } - void set_hp_coeff(double f0, double fs, double gainIn = 1.0) + void set_hp_coeff(double f0, double fs, double gain_in = 1.0) { - gain = gainIn; - fType = filter_type::high_pass; + m_gain = gain_in; + m_f_type = filter_type::high_pass; double e0{ 2 * M_PI * f0 / fs }; - for (unsigned int k = 0; k < M; k++) { - double dk{ 2 * std::sin((2 * k + 1) * M_PI / (4.0 * M)) }; + for (unsigned int k = 0; k < m_t; k++) { + double dk{ 2 * std::sin((2 * k + 1) * M_PI / (4.0 * m_t)) }; double t{ dk * std::sin(e0) / 2 }; double dnm{ 1 + t }; @@ -153,26 +153,26 @@ class casc_2o_iir { double gamma1{ (0.5 + beta1) * std::cos(e0) }; double alpha1{ (0.5 + beta1 + gamma1) / 4 }; - gain *= 2 * alpha1; + m_gain *= 2 * alpha1; - bCoeff.at(k).at(0) = 1.0; - bCoeff.at(k).at(1) = -2.0; - bCoeff.at(k).at(2) = 1.0; + m_b_coeff.at(k).at(0) = 1.0; + m_b_coeff.at(k).at(1) = -2.0; + m_b_coeff.at(k).at(2) = 1.0; - aCoeff.at(k).at(0) = 1; - aCoeff.at(k).at(1) = -2 * gamma1; - aCoeff.at(k).at(2) = 2 * beta1; + m_a_coeff.at(k).at(0) = 1; + m_a_coeff.at(k).at(1) = -2 * gamma1; + m_a_coeff.at(k).at(2) = 2 * beta1; } } - void set_lp_coeff(double f0, double fs, double gainIn = 1.0) + void set_lp_coeff(double f0, double fs, double gain_in = 1.0) { - gain = gainIn; - fType = filter_type::low_pass; + m_gain = gain_in; + m_f_type = filter_type::low_pass; double e0{ 2 * M_PI * f0 / fs }; - for (unsigned int k = 0; k < M; k++) { - double dk{ 2 * std::sin((2 * k + 1) * M_PI / (4.0 * M)) }; + for (unsigned int k = 0; k < m_t; k++) { + double dk{ 2 * std::sin((2 * k + 1) * M_PI / (4.0 * m_t)) }; double t{ dk * std::sin(e0) / 2 }; double dnm{ 1 + t }; @@ -181,65 +181,65 @@ class casc_2o_iir { double gamma1{ (0.5 + beta1) * std::cos(e0) }; double alpha1{ (0.5 + beta1 - gamma1) / 4 }; - gain *= 2 * alpha1; + m_gain *= 2 * alpha1; - bCoeff.at(k).at(0) = 1.0; - bCoeff.at(k).at(1) = 2.0; - bCoeff.at(k).at(2) = 1.0; + m_b_coeff.at(k).at(0) = 1.0; + m_b_coeff.at(k).at(1) = 2.0; + m_b_coeff.at(k).at(2) = 1.0; - aCoeff.at(k).at(0) = 1; - aCoeff.at(k).at(1) = -2 * gamma1; - aCoeff.at(k).at(2) = 2 * beta1; + m_a_coeff.at(k).at(0) = 1; + m_a_coeff.at(k).at(1) = -2 * gamma1; + m_a_coeff.at(k).at(2) = 2 * beta1; } } // preload the filter memory for steady state input equal to value parameter void preload_filter(double value) { - double preload_value = value * gain; - std::array, M + 1> memVals{ 0 }; + double preload_value = value * m_gain; + std::array, m_t + 1> mem_vals{ 0 }; for (int i = 0; i < 3; i++) { - memVals.at(0).at(i) = preload_value; + mem_vals.at(0).at(i) = preload_value; } - if (fType == filter_type::low_pass) { - for (uint j = 1; j < M + 1; j++) { - preload_value /= 1 + aCoeff.at(j - 1).at(1) + aCoeff.at(j - 1).at(2); - preload_value *= bCoeff.at(j - 1).at(0) + bCoeff.at(j - 1).at(1) + bCoeff.at(j - 1).at(2); + if (m_f_type == filter_type::low_pass) { + for (uint j = 1; j < m_t + 1; j++) { + preload_value /= 1 + m_a_coeff.at(j - 1).at(1) + m_a_coeff.at(j - 1).at(2); + preload_value *= m_b_coeff.at(j - 1).at(0) + m_b_coeff.at(j - 1).at(1) + m_b_coeff.at(j - 1).at(2); for (uint i = 0; i < 3; i++) { - memVals.at(j).at(i) = preload_value; + mem_vals.at(j).at(i) = preload_value; } } } - mem = memVals; + m_mem = mem_vals; } }; -template +template class casc_2o_iir_base { protected: - int pos{ 0 }; + int m_pos{ 0 }; - double gain{ 1.0 }; + double m_gain{ 1.0 }; - std::array, M + 1> mem{ 0 }; + std::array, m_t + 1> m_mem{ 0 }; - std::array, M> aCoeff{ 0 }; + std::array, m_t> m_a_coeff{ 0 }; - template - void process_base(Iter begin, Iter end) + template + void process_base(iter_t begin, iter_t end) { constexpr int order{ 2 }; constexpr int j1{ 1 }; constexpr int j2{ 2 }; - int p{ pos }; + int p{ m_pos }; - std::array, M + 1> y = mem; + std::array, m_t + 1> y = m_mem; - std::array, M> a = aCoeff; + std::array, m_t> a = m_a_coeff; while (begin < end) { - y.at(0).at(p) = *begin * gain; + y.at(0).at(p) = *begin * m_gain; int d1{ p - j1 }; if (d1 < 0) @@ -249,44 +249,44 @@ class casc_2o_iir_base { if (d2 < 0) d2 += order + 1; - T::process_spec(y, a, p, d1, d2); + iir_t::process_spec(y, a, p, d1, d2); - *begin = y.at(M).at(p); + *begin = y.at(m_t).at(p); p++; if (p > order) p = 0; begin++; } - pos = p; - mem = y; + m_pos = p; + m_mem = y; } }; -template -class casc_2o_iir_lp : casc_2o_iir_base { +template +class casc_2o_iir_lp : casc_2o_iir_base { public: casc_2o_iir_lp() { - static_assert(M % 2 == 0, "M must be even!"); + static_assert(m_t % 2 == 0, "M must be even!"); } - void copy_coeff_from(const casc_2o_iir_lp &otherFilter) + void copy_coeff_from(const casc_2o_iir_lp &other_filter) { - this->gain = otherFilter.gain; - this->aCoeff = otherFilter.aCoeff; + this->gain = other_filter.gain; + this->aCoeff = other_filter.aCoeff; } - template - void process(Iter begin, Iter end) + template + void process(iter_t begin, iter_t end) { - this->template process_base>(begin, end); + this->template process_base>(begin, end); } - static void process_spec(std::array, M + 1> &y, const std::array, M> &a, - int p, int d1, int d2) + static void process_spec(std::array, m_t + 1> &y, + const std::array, m_t> &a, int p, int d1, int d2) { - for (uint j = 0; j < M; j++) { + for (uint j = 0; j < m_t; j++) { y.at(j + 1).at(p) = y.at(j).at(p); y.at(j + 1).at(p) += y.at(j).at(d1) + y.at(j).at(d1) - y.at(j + 1).at(d1) * a.at(j).at(1); @@ -294,13 +294,13 @@ class casc_2o_iir_lp : casc_2o_iir_base { } } - void set_lp_coeff(double f0, double fs, double gainIn = 1.0) + void set_lp_coeff(double f0, double fs, double gain_in = 1.0) { - this->gain = gainIn; + this->m_gain = gain_in; double e0{ 2 * M_PI * f0 / fs }; - for (unsigned int k = 0; k < M; k++) { - double dk{ 2 * std::sin((2 * k + 1) * M_PI / (4.0 * M)) }; + for (unsigned int k = 0; k < m_t; k++) { + double dk{ 2 * std::sin((2 * k + 1) * M_PI / (4.0 * m_t)) }; double t{ dk * std::sin(e0) / 2 }; double dnm{ 1 + t }; @@ -309,42 +309,42 @@ class casc_2o_iir_lp : casc_2o_iir_base { double gamma1{ (0.5 + beta1) * std::cos(e0) }; double alpha1{ (0.5 + beta1 - gamma1) / 4 }; - this->gain *= 2 * alpha1; + this->m_gain *= 2 * alpha1; // bCoeff.at(k).at(0) = 1.0; // bCoeff.at(k).at(1) = 2.0; // bCoeff.at(k).at(2) = 1.0; - this->aCoeff.at(k).at(0) = 1; - this->aCoeff.at(k).at(1) = -2 * gamma1; - this->aCoeff.at(k).at(2) = 2 * beta1; + this->m_a_coeff.at(k).at(0) = 1; + this->m_a_coeff.at(k).at(1) = -2 * gamma1; + this->m_a_coeff.at(k).at(2) = 2 * beta1; } } }; -template -class casc_2o_iir_hp : casc_2o_iir_base { +template +class casc_2o_iir_hp : casc_2o_iir_base { public: casc_2o_iir_hp() { - static_assert(M % 2 == 0, "M must be even!"); + static_assert(m_t % 2 == 0, "M must be even!"); } - void copy_coeff_from(const casc_2o_iir_hp &otherFilter) + void copy_coeff_from(const casc_2o_iir_hp &other_filter) { - this->gain = otherFilter.gain; - this->aCoeff = otherFilter.aCoeff; + this->gain = other_filter.gain; + this->aCoeff = other_filter.aCoeff; } - template - void process(Iter begin, Iter end) + template + void process(iter_t begin, iter_t end) { - this->template process_base>(begin, end); + this->template process_base>(begin, end); } - static void process_spec(std::array, M + 1> &y, const std::array, M> &a, - int p, int d1, int d2) + static void process_spec(std::array, m_t + 1> &y, + const std::array, m_t> &a, int p, int d1, int d2) { - for (uint j = 0; j < M; j++) { + for (uint j = 0; j < m_t; j++) { y.at(j + 1).at(p) = y.at(j).at(p); y.at(j + 1).at(p) += -y.at(j).at(d1) - y.at(j).at(d1) - y.at(j + 1).at(d1) * a.at(j).at(1); @@ -352,13 +352,13 @@ class casc_2o_iir_hp : casc_2o_iir_base { } } - void set_hp_coeff(double f0, double fs, double gainIn = 1.0) + void set_hp_coeff(double f0, double fs, double gain_in = 1.0) { - this->gain = gainIn; + this->m_gain = gain_in; double e0{ 2 * M_PI * f0 / fs }; - for (unsigned int k = 0; k < M; k++) { - double dk{ 2 * std::sin((2 * k + 1) * M_PI / (4.0 * M)) }; + for (unsigned int k = 0; k < m_t; k++) { + double dk{ 2 * std::sin((2 * k + 1) * M_PI / (4.0 * m_t)) }; double t{ dk * std::sin(e0) / 2 }; double dnm{ 1 + t }; @@ -367,42 +367,42 @@ class casc_2o_iir_hp : casc_2o_iir_base { double gamma1{ (0.5 + beta1) * std::cos(e0) }; double alpha1{ (0.5 + beta1 + gamma1) / 4 }; - this->gain *= 2 * alpha1; + this->m_gain *= 2 * alpha1; // bCoeff.at(k).at(0) = 1.0; // bCoeff.at(k).at(1) = -2.0; // bCoeff.at(k).at(2) = 1.0; - this->aCoeff.at(k).at(0) = 1; - this->aCoeff.at(k).at(1) = -2 * gamma1; - this->aCoeff.at(k).at(2) = 2 * beta1; + this->m_a_coeff.at(k).at(0) = 1; + this->m_a_coeff.at(k).at(1) = -2 * gamma1; + this->m_a_coeff.at(k).at(2) = 2 * beta1; } } }; -template -class casc_2o_iir_bp : casc_2o_iir_base { +template +class casc_2o_iir_bp : casc_2o_iir_base { public: casc_2o_iir_bp() { - static_assert(M % 2 == 0, "M must be even!"); + static_assert(m_t % 2 == 0, "M must be even!"); } - void copy_coeff_from(const casc_2o_iir_bp &otherFilter) + void copy_coeff_from(const casc_2o_iir_bp &other_filter) { - this->gain = otherFilter.gain; - this->aCoeff = otherFilter.aCoeff; + this->gain = other_filter.gain; + this->aCoeff = other_filter.aCoeff; } - template - void process(Iter begin, Iter end) + template + void process(iter_t begin, iter_t end) { - this->template process_base>(begin, end); + this->template process_base>(begin, end); } - static void process_spec(std::array, M + 1> &y, const std::array, M> &a, - int p, int d1, int d2) + static void process_spec(std::array, m_t + 1> &y, + const std::array, m_t> &a, int p, int d1, int d2) { - for (uint j = 0; j < M; j++) { + for (uint j = 0; j < m_t; j++) { y.at(j + 1).at(p) = y.at(j).at(p); y.at(j + 1).at(p) += -y.at(j + 1).at(d1) * a.at(j).at(1); @@ -410,29 +410,29 @@ class casc_2o_iir_bp : casc_2o_iir_base { } } - void set_bp_coeff(double f0, double fs, double Q, double gainIn = 1.0) + void set_bp_coeff(double f0, double fs, double q, double gain_in = 1.0) { - double q2{ 2 * Q }; - this->gain = gainIn; + double q2{ 2 * q }; + this->m_gain = gain_in; double e0{ 2 * M_PI * f0 / fs }; double dnm{ std::sin(e0) }; double de{ 2 * std::tan(e0 / q2) / dnm }; - for (unsigned int k = 0; k < M / 2; k++) { - double D{ 2 * std::sin((2 * k + 1) * M_PI / (2.0 * M)) }; + for (unsigned int k = 0; k < m_t / 2; k++) { + double d{ 2 * std::sin((2 * k + 1) * M_PI / (2.0 * m_t)) }; - double A{ (1 + de * de / 4.0) * 2 / D / de }; - double dk{ std::sqrt(de * D / (A + std::sqrt(A * A - 1))) }; + double a{ (1 + de * de / 4.0) * 2 / d / de }; + double dk{ std::sqrt(de * d / (a + std::sqrt(a * a - 1))) }; - double B{ D * de / dk / 2.0 }; - double W{ B + std::sqrt(B * B - 1) }; + double b{ d * de / dk / 2.0 }; + double w{ b + std::sqrt(b * b - 1) }; double t{ std::tan(e0 / 2.0) }; - double e1{ 2.0 * std::atan(t / W) }; - double e2{ 2.0 * std::atan(W * t) }; + double e1{ 2.0 * std::atan(t / w) }; + double e2{ 2.0 * std::atan(w * t) }; t = dk * std::sin(e1) / 2.0; dnm = (1 + t); @@ -445,11 +445,11 @@ class casc_2o_iir_bp : casc_2o_iir_base { double gamma1{ (0.5 + beta1) * std::cos(e1) }; double gamma2{ (0.5 + beta2) * std::cos(e2) }; - t = std::sqrt(1 + (W - 1 / W) / dk * (W - 1 / W) / dk); + t = std::sqrt(1 + (w - 1 / w) / dk * (w - 1 / w) / dk); double alpha1{ (0.5 - beta1) * t / 2.0 }; double alpha2{ (0.5 - beta2) * t / 2.0 }; - this->gain *= 4 * alpha1 * alpha2; + this->m_gain *= 4 * alpha1 * alpha2; // bCoeff.at(2 * k).at(0) = 1.0; // bCoeff.at(2 * k + 1).at(0) = 1.0; // bCoeff.at(2 * k).at(1) = 0; @@ -457,12 +457,12 @@ class casc_2o_iir_bp : casc_2o_iir_base { // bCoeff.at(2 * k).at(2) = -1.0; // bCoeff.at(2 * k + 1).at(2) = -1.0; - this->aCoeff.at(2 * k).at(0) = 1; - this->aCoeff.at(2 * k + 1).at(0) = 1; - this->aCoeff.at(2 * k).at(1) = -2 * gamma1; - this->aCoeff.at(2 * k + 1).at(1) = -2 * gamma2; - this->aCoeff.at(2 * k).at(2) = 2 * beta1; - this->aCoeff.at(2 * k + 1).at(2) = 2 * beta2; + this->m_a_coeff.at(2 * k).at(0) = 1; + this->m_a_coeff.at(2 * k + 1).at(0) = 1; + this->m_a_coeff.at(2 * k).at(1) = -2 * gamma1; + this->m_a_coeff.at(2 * k + 1).at(1) = -2 * gamma2; + this->m_a_coeff.at(2 * k).at(2) = 2 * beta1; + this->m_a_coeff.at(2 * k + 1).at(2) = 2 * beta2; } } };