The open source OpenXR runtime
at main 3.9 kB view raw
1// Copyright 2022, Collabora, Ltd. 2// SPDX-License-Identifier: BSL-1.0 3/*! 4 * @file 5 * @brief Scalar float low pass filter tests. 6 * @author Rylie Pavlik <rylie.pavlik@collabora.com> 7 */ 8 9#include <math/m_lowpass_float.hpp> 10#include <math/m_lowpass_float.h> 11 12#include "catch_amalgamated.hpp" 13 14using xrt::auxiliary::math::LowPassIIRFilter; 15static constexpr float InitialState = 300; 16static constexpr timepoint_ns InitialTime = 12345; 17static constexpr timepoint_ns StepSize = U_TIME_1MS_IN_NS * 20; 18 19TEMPLATE_TEST_CASE("LowPassIIRFilter", "", float, double) 20{ 21 LowPassIIRFilter<TestType> filter(100); 22 23 CHECK_FALSE(filter.isInitialized()); 24 timepoint_ns now = InitialTime; 25 26 filter.addSample(InitialState, now); 27 CHECK(filter.getState() == InitialState); 28 CHECK(filter.getTimestampNs() == now); 29 CHECK(filter.isInitialized()); 30 31 auto prev = filter.getState(); 32 SECTION("Increase") 33 { 34 constexpr auto newTarget = InitialState * 2; 35 for (int i = 0; i < 20; ++i) { 36 now += StepSize; 37 filter.addSample(newTarget, now); 38 REQUIRE(filter.isInitialized()); 39 CHECK(filter.getTimestampNs() == now); 40 // not going to exceed this 41 if (prev == newTarget) { 42 REQUIRE(filter.getState() == prev); 43 } else { 44 REQUIRE(filter.getState() > prev); 45 prev = filter.getState(); 46 } 47 } 48 } 49 50 SECTION("Decrease") 51 { 52 constexpr auto newTarget = InitialState / 2; 53 for (int i = 0; i < 20; ++i) { 54 now += StepSize; 55 filter.addSample(newTarget, now); 56 REQUIRE(filter.isInitialized()); 57 CHECK(filter.getTimestampNs() == now); 58 if (prev == newTarget) { 59 REQUIRE(filter.getState() == newTarget); 60 } else { 61 REQUIRE(filter.getState() < prev); 62 prev = filter.getState(); 63 } 64 } 65 } 66 SECTION("Stay Same") 67 { 68 for (int i = 0; i < 20; ++i) { 69 now += StepSize; 70 filter.addSample(InitialState, now); 71 REQUIRE(filter.isInitialized()); 72 CHECK(filter.getTimestampNs() == now); 73 REQUIRE(filter.getState() == InitialState); 74 } 75 } 76} 77 78TEST_CASE("m_lowpass_float") 79{ 80 81 struct m_lowpass_float *filter = m_lowpass_float_create(100); 82 CHECK(filter); 83 84 CHECK_FALSE(m_lowpass_float_is_initialized(filter)); 85 86 timepoint_ns now = InitialTime; 87 88 m_lowpass_float_add_sample(filter, InitialState, now); 89 REQUIRE(m_lowpass_float_is_initialized(filter)); 90 CHECK(m_lowpass_float_get_state(filter) == InitialState); 91 92 m_lowpass_float_add_sample(filter, InitialState, now); 93 CHECK(m_lowpass_float_get_state(filter) == InitialState); 94 CHECK(m_lowpass_float_get_timestamp_ns(filter) == now); 95 CHECK(m_lowpass_float_is_initialized(filter)); 96 97 auto prev = m_lowpass_float_get_state(filter); 98 SECTION("Increase") 99 { 100 constexpr auto newTarget = InitialState * 2; 101 for (int i = 0; i < 20; ++i) { 102 now += StepSize; 103 m_lowpass_float_add_sample(filter, newTarget, now); 104 REQUIRE(m_lowpass_float_is_initialized(filter)); 105 CHECK(m_lowpass_float_get_timestamp_ns(filter) == now); 106 // not going to exceed this 107 if (prev == newTarget) { 108 REQUIRE(m_lowpass_float_get_state(filter) == prev); 109 } else { 110 REQUIRE(m_lowpass_float_get_state(filter) > prev); 111 prev = m_lowpass_float_get_state(filter); 112 } 113 } 114 } 115 116 SECTION("Decrease") 117 { 118 constexpr auto newTarget = InitialState / 2; 119 for (int i = 0; i < 20; ++i) { 120 now += StepSize; 121 m_lowpass_float_add_sample(filter, newTarget, now); 122 REQUIRE(m_lowpass_float_is_initialized(filter)); 123 CHECK(m_lowpass_float_get_timestamp_ns(filter) == now); 124 if (prev == newTarget) { 125 REQUIRE(m_lowpass_float_get_state(filter) == newTarget); 126 } else { 127 REQUIRE(m_lowpass_float_get_state(filter) < prev); 128 prev = m_lowpass_float_get_state(filter); 129 } 130 } 131 } 132 SECTION("Stay Same") 133 { 134 for (int i = 0; i < 20; ++i) { 135 now += StepSize; 136 m_lowpass_float_add_sample(filter, InitialState, now); 137 REQUIRE(m_lowpass_float_is_initialized(filter)); 138 CHECK(m_lowpass_float_get_timestamp_ns(filter) == now); 139 REQUIRE(m_lowpass_float_get_state(filter) == InitialState); 140 } 141 } 142}