/** * */ #include #include #include "ButterworthFilterDesigner.h" #include "BiquadFilter.h" #include "BiquadFilter.h" #include "BiquadParams.h" #include "BiquadParams.h" #include "BiquadState.h" #include "BiquadState.h" template void testState_0() { BiquadState p; for (int i = 0; i < N; ++i) { assert(p.z0(i) == 0); assert(p.z1(i) == 0); } p.z0(0) = 5; p.z1(0) = 6; assert(p.z0(0) == 5); assert(p.z1(0) == 6); if (N > 1) { p.z0(N - 1) = 55; p.z1(N - 1) = 66; assert(p.z0(N - 1) == 55); assert(p.z1(N - 1) == 66); } assert(p.z0(0) == 5); assert(p.z1(0) == 6); } template void testParam_0() { BiquadParams p; for (int i = 0; i < N; ++i) { assert(p.A2(i) == 0); assert(p.A1(i) == 0); assert(p.B0(i) == 0); assert(p.B1(i) == 0); assert(p.B2(i) == 0); } p.A1(0) = 1; p.A2(0) = 2; p.B0(0) = 10; p.B1(0) = 11; p.B2(0) = 12; assert(p.A1(0) == 1); assert(p.A2(0) == 2); assert(p.B0(0) == 10); assert(p.B1(0) == 11); assert(p.B2(0) == 12); if (N > 1) { p.A1(N - 1) = 111; p.A2(N - 1) = 112; p.B0(N - 1) = 1110; p.B1(N - 1) = 1111; p.B2(N - 1) = 1112; assert(p.A1(N - 1) == 111); assert(p.A2(N - 1) == 112); assert(p.B0(N - 1) == 1110); assert(p.B1(N - 1) == 1111); assert(p.B2(N - 1) == 1112); } assert(p.A1(0) == 1); assert(p.A2(0) == 2); assert(p.B0(0) == 10); assert(p.B1(0) == 11); assert(p.B2(0) == 12); } template static void test2() { BiquadParams p; ButterworthFilterDesigner::designThreePoleLowpass(p, T(.1)); BiquadState s; T d = BiquadFilter::run(0, s, p); (void) d; } // test that filter designer does something (more than just generate zero template void testBasicDesigner2() { BiquadParams p; ButterworthFilterDesigner::designTwoPoleLowpass(p, T(.1)); assert(p.A1(0) != 0); assert(p.A2(0) != 0); assert(p.B1(0) != 0); assert(p.B2(0) != 0); assert(p.B0(0) != 0); } // test that filter designer does something (more than just generate zero template void testBasicDesigner3() { BiquadParams p; ButterworthFilterDesigner::designThreePoleLowpass(p, T(.1)); assert(p.A1(0) != 0); assert(p.A2(0) != 0); assert(p.B1(0) != 0); assert(p.B2(0) != 0); assert(p.B0(0) != 0); } // test that filter does something template void testBasicFilter2() { BiquadParams params; BiquadState state; ButterworthFilterDesigner::designTwoPoleLowpass(params, T(.1)); T lastValue = -1; // the first five values of the step increase for (int i = 0; i < 100; ++i) { T temp = BiquadFilter::run(1, state, params); if (i < 5) { // the first 5 are strictly increasing and below 1 assert(temp < 1); assert(temp > lastValue); } else if (i < 10) { // the next are all overshoot assert(temp > 1 && temp < 1.05); } else if (i > 50) { //settled assert(temp > .999 && temp < 1.001); } lastValue = temp; } const T val = BiquadFilter::run(1, state, params); (void) val; } // test that filter does something template void testBasicFilter3() { BiquadParams params; BiquadState state; ButterworthFilterDesigner::designThreePoleLowpass(params, T(.1)); T lastValue = 1; //the first five values of the step decrease (to -1) for (int i = 0; i < 100; ++i) { T temp = BiquadFilter::run(1, state, params); if (i < 6) { // the first 5 are strictly increasing and below 1 assert(temp > -1); assert(temp < lastValue); } else if (i < 10) { // the next are all overshoot assert(temp < -1); assert(temp > -1.1); } else if (i > 400) { //settled assert(temp < -.999 && temp > -1.001); } lastValue = temp; } } void testBiquad() { testState_0(); testState_0(); testParam_0(); testParam_0(); test2(); test2(); testBasicDesigner2(); testBasicDesigner2(); testBasicDesigner3(); testBasicDesigner3(); testBasicFilter2(); testBasicFilter2(); testBasicFilter3(); testBasicFilter3(); // TODO: actually measure the freq resp }