diff --git a/include/sdsp/casc2orderIIR.h b/include/sdsp/casc2orderIIR.h index 955e2a9..8be5ab5 100644 --- a/include/sdsp/casc2orderIIR.h +++ b/include/sdsp/casc2orderIIR.h @@ -6,7 +6,7 @@ namespace sdsp { template -class casc2orderIIR { +class casc_2o_IIR { private: int pos{ 0 }; @@ -20,12 +20,12 @@ class casc2orderIIR { FilterType fType{ FilterType::None }; public: - casc2orderIIR() + casc_2o_IIR() { static_assert(M % 2 == 0, "M must be even!"); } - void copy_coeff_from(const casc2orderIIR &otherFilter) + void copy_coeff_from(const casc_2o_IIR &otherFilter) { gain = otherFilter.gain; bCoeff = otherFilter.bCoeff; @@ -34,7 +34,7 @@ class casc2orderIIR { } template - void Process(Iter begin, Iter end) + void process(Iter begin, Iter end) { constexpr int order{ 2 }; constexpr int j1{ 1 }; @@ -79,7 +79,7 @@ class casc2orderIIR { mem = y; } - void SetBPCoeff(double f0, double fs, double Q, double gainIn = 1.0) + void set_bp_coeff(double f0, double fs, double Q, double gainIn = 1.0) { gain = gainIn; double q2{ 2 * Q }; @@ -137,7 +137,7 @@ class casc2orderIIR { } } - void SetHPCoeff(double f0, double fs, double gainIn = 1.0) + void set_hp_coeff(double f0, double fs, double gainIn = 1.0) { gain = gainIn; fType = FilterType::HighPass; @@ -165,7 +165,7 @@ class casc2orderIIR { } } - void SetLPCoeff(double f0, double fs, double gainIn = 1.0) + void set_lp_coeff(double f0, double fs, double gainIn = 1.0) { gain = gainIn; fType = FilterType::LowPass; @@ -194,7 +194,7 @@ class casc2orderIIR { } // preload the filter memory for steady state input equal to value parameter - void PreloadFilter(double value) + void preload_filter(double value) { double preload_value = value * gain; std::array, M + 1> memVals{ 0 }; @@ -215,7 +215,7 @@ class casc2orderIIR { }; template -class casc_2o_IIR { +class casc_2o_IIR_base { protected: int pos{ 0 }; @@ -264,7 +264,7 @@ class casc_2o_IIR { }; template -class casc_2o_IIR_lp : casc_2o_IIR { +class casc_2o_IIR_lp : casc_2o_IIR_base { public: casc_2o_IIR_lp() { @@ -322,7 +322,7 @@ class casc_2o_IIR_lp : casc_2o_IIR { }; template -class casc_2o_IIR_hp : casc_2o_IIR { +class casc_2o_IIR_hp : casc_2o_IIR_base { public: casc_2o_IIR_hp() { @@ -380,7 +380,7 @@ class casc_2o_IIR_hp : casc_2o_IIR { }; template -class casc_2o_IIR_bp : casc_2o_IIR { +class casc_2o_IIR_bp : casc_2o_IIR_base { public: casc_2o_IIR_bp() { diff --git a/test/testIIR.cpp b/test/testIIR.cpp index a49476c..eaad816 100644 --- a/test/testIIR.cpp +++ b/test/testIIR.cpp @@ -34,21 +34,21 @@ TEST_CASE("Filter test") std::string path = "../../../test_data/impulse_response"; for (const auto &entry : std::filesystem::directory_iterator(path)) { auto [readImpulse, fType, fs, f0, Q] = csvreadImpulse2(entry.path().string()); - sdsp::casc2orderIIR<4> df; + sdsp::casc_2o_IIR<4> df; if (fType == sdsp::FilterType::LowPass) { - df.SetLPCoeff(f0, fs); + df.set_lp_coeff(f0, fs); } else if (fType == sdsp::FilterType::HighPass) { - df.SetHPCoeff(f0, fs); + df.set_hp_coeff(f0, fs); } else if (fType == sdsp::FilterType::BandPass) { - df.SetBPCoeff(f0, fs, Q); + df.set_bp_coeff(f0, fs, Q); } else { throw std::runtime_error("Unknown filter type"); } - sdsp::casc2orderIIR<4> df2 = df; + sdsp::casc_2o_IIR<4> df2 = df; std::vector data(readImpulse.size()); data.at(0) = 1.0; - df.Process(data.begin(), data.end()); + df.process(data.begin(), data.end()); std::vector error(readImpulse.size()); @@ -65,11 +65,11 @@ TEST_CASE("Filter test") constexpr unsigned int blockSize{ 32 }; unsigned int index{ 0 }; for (index = 0; index <= data2.size() - blockSize; index += blockSize) { - df2.Process(std::next(data2.begin(), index), std::next(data2.begin(), index + blockSize)); + df2.process(std::next(data2.begin(), index), std::next(data2.begin(), index + blockSize)); } if (index < data2.size()) { - df2.Process(std::next(data2.begin(), index), data2.end()); + df2.process(std::next(data2.begin(), index), data2.end()); } REQUIRE(data == data2); @@ -86,14 +86,14 @@ TEST_CASE("Filter test") std::array impulse2{ 0 }; impulse2.at(0) = 1.0; - sdsp::casc2orderIIR<4> df; - df.SetLPCoeff(f0, fs); + sdsp::casc_2o_IIR<4> df; + df.set_lp_coeff(f0, fs); - sdsp::casc2orderIIR<4> df2; - df2.SetLPCoeff(f0, fs, 2.0); + sdsp::casc_2o_IIR<4> df2; + df2.set_lp_coeff(f0, fs, 2.0); - df.Process(impulse1.begin(), impulse1.end()); - df2.Process(impulse2.begin(), impulse2.end()); + df.process(impulse1.begin(), impulse1.end()); + df2.process(impulse2.begin(), impulse2.end()); auto times_two = [](double &n) { n = 2.0 * n; }; std::for_each(impulse1.begin(), impulse1.end(), times_two); @@ -117,14 +117,14 @@ TEST_CASE("Filter test") std::array impulse2{ 0 }; impulse2.at(0) = 1.0; - sdsp::casc2orderIIR<4> df; - df.SetHPCoeff(f0, fs); + sdsp::casc_2o_IIR<4> df; + df.set_hp_coeff(f0, fs); - sdsp::casc2orderIIR<4> df2; - df2.SetHPCoeff(f0, fs, 2.0); + sdsp::casc_2o_IIR<4> df2; + df2.set_hp_coeff(f0, fs, 2.0); - df.Process(impulse1.begin(), impulse1.end()); - df2.Process(impulse2.begin(), impulse2.end()); + df.process(impulse1.begin(), impulse1.end()); + df2.process(impulse2.begin(), impulse2.end()); auto times_two = [](double &n) { n = 2.0 * n; }; std::for_each(impulse1.begin(), impulse1.end(), times_two); @@ -149,14 +149,14 @@ TEST_CASE("Filter test") std::array impulse2{ 0 }; impulse2.at(0) = 1.0; - sdsp::casc2orderIIR<4> df; - df.SetBPCoeff(f0, fs, Q); + sdsp::casc_2o_IIR<4> df; + df.set_bp_coeff(f0, fs, Q); - sdsp::casc2orderIIR<4> df2; - df2.SetBPCoeff(f0, fs, Q, 2.0); + sdsp::casc_2o_IIR<4> df2; + df2.set_bp_coeff(f0, fs, Q, 2.0); - df.Process(impulse1.begin(), impulse1.end()); - df2.Process(impulse2.begin(), impulse2.end()); + df.process(impulse1.begin(), impulse1.end()); + df2.process(impulse2.begin(), impulse2.end()); auto times_two = [](double &n) { n = 2.0 * n; }; std::for_each(impulse1.begin(), impulse1.end(), times_two); @@ -180,10 +180,10 @@ TEST_CASE("Filter test") { std::array steadyLP; steadyLP.fill(steadyValue); - sdsp::casc2orderIIR<4> lpFilter; - lpFilter.SetLPCoeff(f0, fs); - lpFilter.PreloadFilter(steadyValue); - lpFilter.Process(steadyLP.begin(), steadyLP.end()); + sdsp::casc_2o_IIR<4> lpFilter; + lpFilter.set_lp_coeff(f0, fs); + lpFilter.preload_filter(steadyValue); + lpFilter.process(steadyLP.begin(), steadyLP.end()); auto calcError = [steadyValue](double &n) { n = std::abs(n - steadyValue); }; std::for_each(steadyLP.begin(), steadyLP.end(), calcError); double maxErrorLP = *std::max_element(steadyLP.begin(), steadyLP.end()); @@ -193,10 +193,10 @@ TEST_CASE("Filter test") { std::array steadyHP; steadyHP.fill(steadyValue); - sdsp::casc2orderIIR<4> hpFilter; - hpFilter.SetHPCoeff(f0, fs); - hpFilter.PreloadFilter(steadyValue); - hpFilter.Process(steadyHP.begin(), steadyHP.end()); + sdsp::casc_2o_IIR<4> hpFilter; + hpFilter.set_hp_coeff(f0, fs); + hpFilter.preload_filter(steadyValue); + hpFilter.process(steadyHP.begin(), steadyHP.end()); auto calcError = [](double &n) { n = std::abs(n); }; std::for_each(steadyHP.begin(), steadyHP.end(), calcError); double maxErrorHP = *std::max_element(steadyHP.begin(), steadyHP.end()); @@ -206,10 +206,10 @@ TEST_CASE("Filter test") { std::array steadyBP; steadyBP.fill(steadyValue); - sdsp::casc2orderIIR<4> bpFilter; - bpFilter.SetBPCoeff(f0, fs, Q); - bpFilter.PreloadFilter(steadyValue); - bpFilter.Process(steadyBP.begin(), steadyBP.end()); + sdsp::casc_2o_IIR<4> bpFilter; + bpFilter.set_bp_coeff(f0, fs, Q); + bpFilter.preload_filter(steadyValue); + bpFilter.process(steadyBP.begin(), steadyBP.end()); auto calcError = [](double &n) { n = std::abs(n); }; std::for_each(steadyBP.begin(), steadyBP.end(), calcError); double maxErrorBP = *std::max_element(steadyBP.begin(), steadyBP.end()); @@ -470,8 +470,8 @@ TEST_CASE("Filter benchmarks") constexpr double f0{ 10e3 }; // constexpr double Q {1.1}; - sdsp::casc2orderIIR<4> df; - df.SetLPCoeff(f0, fs); + sdsp::casc_2o_IIR<4> df; + df.set_lp_coeff(f0, fs); sdsp::casc_2o_IIR_lp<4> df2; df2.set_coeff(f0, fs); @@ -482,7 +482,7 @@ TEST_CASE("Filter benchmarks") BENCHMARK("Runtime configurable LP filter benchmark") { std::array data2 = data; - df.Process(data2.begin(), data2.end()); + df.process(data2.begin(), data2.end()); return data2; }; @@ -501,8 +501,8 @@ TEST_CASE("Filter benchmarks") constexpr double f0{ 10e3 }; // constexpr double Q {1.1}; - sdsp::casc2orderIIR<4> df; - df.SetHPCoeff(f0, fs); + sdsp::casc_2o_IIR<4> df; + df.set_hp_coeff(f0, fs); sdsp::casc_2o_IIR_hp<4> df2; df2.set_coeff(f0, fs); @@ -513,7 +513,7 @@ TEST_CASE("Filter benchmarks") BENCHMARK("Runtime configurable HP filter benchmark") { std::array data2 = data; - df.Process(data2.begin(), data2.end()); + df.process(data2.begin(), data2.end()); return data2; }; @@ -532,8 +532,8 @@ TEST_CASE("Filter benchmarks") constexpr double f0{ 10e3 }; constexpr double Q{ 1.1 }; - sdsp::casc2orderIIR<4> df; - df.SetBPCoeff(f0, fs, Q); + sdsp::casc_2o_IIR<4> df; + df.set_bp_coeff(f0, fs, Q); sdsp::casc_2o_IIR_bp<4> df2; df2.set_coeff(f0, fs, Q); @@ -544,7 +544,7 @@ TEST_CASE("Filter benchmarks") BENCHMARK("Runtime configurable BP filter benchmark") { std::array data2 = data; - df.Process(data2.begin(), data2.end()); + df.process(data2.begin(), data2.end()); return data2; };