2019-08-09 18:24:27 +02:00
|
|
|
#include "../sources/OnePoleFilter.h"
|
|
|
|
|
#include "catch2/catch.hpp"
|
|
|
|
|
#include "cnpy.h"
|
2019-08-21 18:18:12 +02:00
|
|
|
#include <absl/types/span.h>
|
2019-08-25 14:01:03 +02:00
|
|
|
#include <algorithm>
|
|
|
|
|
#include <filesystem>
|
|
|
|
|
#include <string>
|
2019-08-09 18:24:27 +02:00
|
|
|
using namespace Catch::literals;
|
|
|
|
|
|
2019-08-25 14:01:03 +02:00
|
|
|
template <class Type>
|
2019-08-11 11:10:50 +02:00
|
|
|
inline bool approxEqual(const std::vector<Type>& lhs, const std::vector<Type>& rhs)
|
|
|
|
|
{
|
|
|
|
|
if (lhs.size() != rhs.size())
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
for (size_t i = 0; i < rhs.size(); ++i)
|
2019-08-25 14:01:03 +02:00
|
|
|
if (lhs[i] != Approx(rhs[i]).epsilon(1e-3)) {
|
2019-08-11 11:10:50 +02:00
|
|
|
std::cerr << lhs[i] << " != " << rhs[i] << " at index " << i << '\n';
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2019-08-25 14:01:03 +02:00
|
|
|
template <class Type>
|
2019-08-11 11:10:50 +02:00
|
|
|
void testLowpass(const std::filesystem::path& inputNumpyFile, const std::filesystem::path& outputNumpyFile, Type gain)
|
2019-08-09 18:24:27 +02:00
|
|
|
{
|
|
|
|
|
const auto input = cnpy::npy_load(inputNumpyFile.string());
|
2019-08-25 14:01:03 +02:00
|
|
|
REQUIRE(input.word_size == 8);
|
2019-08-21 18:18:12 +02:00
|
|
|
const auto inputSpan = absl::MakeSpan(input.data<double>(), input.shape[0]);
|
2019-08-09 18:24:27 +02:00
|
|
|
|
|
|
|
|
const auto output = cnpy::npy_load(outputNumpyFile.string());
|
2019-08-25 14:01:03 +02:00
|
|
|
REQUIRE(output.word_size == 8);
|
2019-08-21 18:18:12 +02:00
|
|
|
const auto outputSpan = absl::MakeSpan(output.data<double>(), output.shape[0]);
|
2019-08-09 18:24:27 +02:00
|
|
|
auto size = std::min(outputSpan.size(), inputSpan.size());
|
2019-08-25 14:01:03 +02:00
|
|
|
REQUIRE(size > 0);
|
2019-08-09 18:24:27 +02:00
|
|
|
|
|
|
|
|
std::vector<Type> inputData;
|
|
|
|
|
std::vector<Type> expectedData;
|
|
|
|
|
inputData.reserve(size);
|
|
|
|
|
expectedData.reserve(size);
|
2019-08-25 14:01:03 +02:00
|
|
|
for (auto& data : inputSpan)
|
2019-08-09 18:24:27 +02:00
|
|
|
inputData.push_back(static_cast<Type>(data));
|
2019-08-25 14:01:03 +02:00
|
|
|
for (auto& data : outputSpan)
|
2019-08-09 18:24:27 +02:00
|
|
|
expectedData.push_back(static_cast<Type>(data));
|
2019-08-25 14:01:03 +02:00
|
|
|
|
2019-08-09 18:24:27 +02:00
|
|
|
OnePoleFilter filter { gain };
|
2019-08-25 14:01:03 +02:00
|
|
|
std::vector<Type> outputData(size);
|
2019-08-21 18:18:12 +02:00
|
|
|
filter.processLowpass(inputData, absl::MakeSpan(outputData));
|
2019-08-25 14:01:03 +02:00
|
|
|
REQUIRE(approxEqual(outputData, expectedData));
|
2019-08-09 18:24:27 +02:00
|
|
|
|
|
|
|
|
filter.reset();
|
|
|
|
|
std::fill(outputData.begin(), outputData.end(), 0.0);
|
|
|
|
|
std::vector<Type> gains(size);
|
|
|
|
|
std::fill(gains.begin(), gains.end(), gain);
|
2019-08-21 18:18:12 +02:00
|
|
|
filter.processLowpassVariableGain(inputData, absl::MakeSpan(outputData), gains);
|
2019-08-25 14:01:03 +02:00
|
|
|
REQUIRE(approxEqual(outputData, expectedData));
|
2019-08-11 11:10:50 +02:00
|
|
|
}
|
|
|
|
|
|
2019-08-25 14:01:03 +02:00
|
|
|
template <class Type>
|
2019-08-11 11:10:50 +02:00
|
|
|
void testHighpass(const std::filesystem::path& inputNumpyFile, const std::filesystem::path& outputNumpyFile, Type gain)
|
|
|
|
|
{
|
|
|
|
|
const auto input = cnpy::npy_load(inputNumpyFile.string());
|
2019-08-25 14:01:03 +02:00
|
|
|
REQUIRE(input.word_size == 8);
|
2019-08-21 18:18:12 +02:00
|
|
|
const auto inputSpan = absl::MakeSpan(input.data<double>(), input.shape[0]);
|
2019-08-11 11:10:50 +02:00
|
|
|
|
|
|
|
|
const auto output = cnpy::npy_load(outputNumpyFile.string());
|
2019-08-25 14:01:03 +02:00
|
|
|
REQUIRE(output.word_size == 8);
|
2019-08-21 18:18:12 +02:00
|
|
|
const auto outputSpan = absl::MakeSpan(output.data<double>(), output.shape[0]);
|
2019-08-11 11:10:50 +02:00
|
|
|
auto size = std::min(outputSpan.size(), inputSpan.size());
|
2019-08-25 14:01:03 +02:00
|
|
|
REQUIRE(size > 0);
|
2019-08-11 11:10:50 +02:00
|
|
|
|
|
|
|
|
std::vector<Type> inputData;
|
|
|
|
|
std::vector<Type> expectedData;
|
|
|
|
|
inputData.reserve(size);
|
|
|
|
|
expectedData.reserve(size);
|
2019-08-25 14:01:03 +02:00
|
|
|
for (auto& data : inputSpan)
|
2019-08-11 11:10:50 +02:00
|
|
|
inputData.push_back(static_cast<Type>(data));
|
2019-08-25 14:01:03 +02:00
|
|
|
for (auto& data : outputSpan)
|
2019-08-11 11:10:50 +02:00
|
|
|
expectedData.push_back(static_cast<Type>(data));
|
2019-08-25 14:01:03 +02:00
|
|
|
|
2019-08-11 11:10:50 +02:00
|
|
|
OnePoleFilter filter { gain };
|
2019-08-25 14:01:03 +02:00
|
|
|
std::vector<Type> outputData(size);
|
2019-08-21 18:18:12 +02:00
|
|
|
filter.processHighpass(inputData, absl::MakeSpan(outputData));
|
2019-08-25 14:01:03 +02:00
|
|
|
REQUIRE(approxEqual(outputData, expectedData));
|
2019-08-11 11:10:50 +02:00
|
|
|
|
|
|
|
|
filter.reset();
|
|
|
|
|
std::fill(outputData.begin(), outputData.end(), 0.0);
|
|
|
|
|
std::vector<Type> gains(size);
|
|
|
|
|
std::fill(gains.begin(), gains.end(), gain);
|
2019-08-21 18:18:12 +02:00
|
|
|
filter.processHighpassVariableGain(inputData, absl::MakeSpan(outputData), gains);
|
2019-08-25 14:01:03 +02:00
|
|
|
REQUIRE(approxEqual(outputData, expectedData));
|
2019-08-11 11:10:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
TEST_CASE("[OnePoleFilter] Lowpass Float")
|
|
|
|
|
{
|
|
|
|
|
testLowpass<float>(
|
|
|
|
|
std::filesystem::current_path() / "tests/TestFiles/OnePoleFilter/OPF_input_gain_0.1.npy",
|
|
|
|
|
std::filesystem::current_path() / "tests/TestFiles/OnePoleFilter/OPF_low_gain_0.1.npy",
|
2019-08-25 14:01:03 +02:00
|
|
|
0.1f);
|
2019-08-11 11:10:50 +02:00
|
|
|
testLowpass<float>(
|
|
|
|
|
std::filesystem::current_path() / "tests/TestFiles/OnePoleFilter/OPF_input_gain_0.3.npy",
|
|
|
|
|
std::filesystem::current_path() / "tests/TestFiles/OnePoleFilter/OPF_low_gain_0.3.npy",
|
2019-08-25 14:01:03 +02:00
|
|
|
0.3f);
|
2019-08-11 11:10:50 +02:00
|
|
|
testLowpass<float>(
|
|
|
|
|
std::filesystem::current_path() / "tests/TestFiles/OnePoleFilter/OPF_input_gain_0.5.npy",
|
|
|
|
|
std::filesystem::current_path() / "tests/TestFiles/OnePoleFilter/OPF_low_gain_0.5.npy",
|
2019-08-25 14:01:03 +02:00
|
|
|
0.5f);
|
2019-08-11 11:10:50 +02:00
|
|
|
testLowpass<float>(
|
|
|
|
|
std::filesystem::current_path() / "tests/TestFiles/OnePoleFilter/OPF_input_gain_0.7.npy",
|
|
|
|
|
std::filesystem::current_path() / "tests/TestFiles/OnePoleFilter/OPF_low_gain_0.7.npy",
|
2019-08-25 14:01:03 +02:00
|
|
|
0.7f);
|
2019-08-11 11:10:50 +02:00
|
|
|
testLowpass<float>(
|
|
|
|
|
std::filesystem::current_path() / "tests/TestFiles/OnePoleFilter/OPF_input_gain_0.9.npy",
|
|
|
|
|
std::filesystem::current_path() / "tests/TestFiles/OnePoleFilter/OPF_low_gain_0.9.npy",
|
2019-08-25 14:01:03 +02:00
|
|
|
0.9f);
|
2019-08-11 11:10:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
TEST_CASE("[OnePoleFilter] Lowpass Double")
|
|
|
|
|
{
|
|
|
|
|
testLowpass<double>(
|
|
|
|
|
std::filesystem::current_path() / "tests/TestFiles/OnePoleFilter/OPF_input_gain_0.1.npy",
|
|
|
|
|
std::filesystem::current_path() / "tests/TestFiles/OnePoleFilter/OPF_low_gain_0.1.npy",
|
2019-08-25 14:01:03 +02:00
|
|
|
0.1f);
|
2019-08-11 11:10:50 +02:00
|
|
|
testLowpass<double>(
|
|
|
|
|
std::filesystem::current_path() / "tests/TestFiles/OnePoleFilter/OPF_input_gain_0.3.npy",
|
|
|
|
|
std::filesystem::current_path() / "tests/TestFiles/OnePoleFilter/OPF_low_gain_0.3.npy",
|
2019-08-25 14:01:03 +02:00
|
|
|
0.3f);
|
2019-08-11 11:10:50 +02:00
|
|
|
testLowpass<double>(
|
|
|
|
|
std::filesystem::current_path() / "tests/TestFiles/OnePoleFilter/OPF_input_gain_0.5.npy",
|
|
|
|
|
std::filesystem::current_path() / "tests/TestFiles/OnePoleFilter/OPF_low_gain_0.5.npy",
|
2019-08-25 14:01:03 +02:00
|
|
|
0.5f);
|
2019-08-11 11:10:50 +02:00
|
|
|
testLowpass<double>(
|
|
|
|
|
std::filesystem::current_path() / "tests/TestFiles/OnePoleFilter/OPF_input_gain_0.7.npy",
|
|
|
|
|
std::filesystem::current_path() / "tests/TestFiles/OnePoleFilter/OPF_low_gain_0.7.npy",
|
2019-08-25 14:01:03 +02:00
|
|
|
0.7f);
|
2019-08-11 11:10:50 +02:00
|
|
|
testLowpass<double>(
|
|
|
|
|
std::filesystem::current_path() / "tests/TestFiles/OnePoleFilter/OPF_input_gain_0.9.npy",
|
|
|
|
|
std::filesystem::current_path() / "tests/TestFiles/OnePoleFilter/OPF_low_gain_0.9.npy",
|
2019-08-25 14:01:03 +02:00
|
|
|
0.9f);
|
2019-08-09 18:24:27 +02:00
|
|
|
}
|
|
|
|
|
|
2019-08-11 11:10:50 +02:00
|
|
|
TEST_CASE("[OnePoleFilter] Highpass Float")
|
2019-08-09 18:24:27 +02:00
|
|
|
{
|
2019-08-11 11:10:50 +02:00
|
|
|
testHighpass<float>(
|
2019-08-09 18:24:27 +02:00
|
|
|
std::filesystem::current_path() / "tests/TestFiles/OnePoleFilter/OPF_input_gain_0.1.npy",
|
2019-08-11 11:10:50 +02:00
|
|
|
std::filesystem::current_path() / "tests/TestFiles/OnePoleFilter/OPF_high_gain_0.1.npy",
|
2019-08-25 14:01:03 +02:00
|
|
|
0.1f);
|
2019-08-11 11:10:50 +02:00
|
|
|
testHighpass<float>(
|
2019-08-09 18:24:27 +02:00
|
|
|
std::filesystem::current_path() / "tests/TestFiles/OnePoleFilter/OPF_input_gain_0.3.npy",
|
2019-08-11 11:10:50 +02:00
|
|
|
std::filesystem::current_path() / "tests/TestFiles/OnePoleFilter/OPF_high_gain_0.3.npy",
|
2019-08-25 14:01:03 +02:00
|
|
|
0.3f);
|
2019-08-11 11:10:50 +02:00
|
|
|
testHighpass<float>(
|
2019-08-09 18:24:27 +02:00
|
|
|
std::filesystem::current_path() / "tests/TestFiles/OnePoleFilter/OPF_input_gain_0.5.npy",
|
2019-08-11 11:10:50 +02:00
|
|
|
std::filesystem::current_path() / "tests/TestFiles/OnePoleFilter/OPF_high_gain_0.5.npy",
|
2019-08-25 14:01:03 +02:00
|
|
|
0.5f);
|
2019-08-11 11:10:50 +02:00
|
|
|
testHighpass<float>(
|
2019-08-09 18:24:27 +02:00
|
|
|
std::filesystem::current_path() / "tests/TestFiles/OnePoleFilter/OPF_input_gain_0.7.npy",
|
2019-08-11 11:10:50 +02:00
|
|
|
std::filesystem::current_path() / "tests/TestFiles/OnePoleFilter/OPF_high_gain_0.7.npy",
|
2019-08-25 14:01:03 +02:00
|
|
|
0.7f);
|
2019-08-11 11:10:50 +02:00
|
|
|
testHighpass<float>(
|
2019-08-09 18:24:27 +02:00
|
|
|
std::filesystem::current_path() / "tests/TestFiles/OnePoleFilter/OPF_input_gain_0.9.npy",
|
2019-08-11 11:10:50 +02:00
|
|
|
std::filesystem::current_path() / "tests/TestFiles/OnePoleFilter/OPF_high_gain_0.9.npy",
|
2019-08-25 14:01:03 +02:00
|
|
|
0.9f);
|
2019-08-09 18:24:27 +02:00
|
|
|
}
|
|
|
|
|
|
2019-08-11 11:10:50 +02:00
|
|
|
TEST_CASE("[OnePoleFilter] Highpass Double")
|
2019-08-09 18:24:27 +02:00
|
|
|
{
|
2019-08-11 11:10:50 +02:00
|
|
|
testHighpass<double>(
|
2019-08-09 18:24:27 +02:00
|
|
|
std::filesystem::current_path() / "tests/TestFiles/OnePoleFilter/OPF_input_gain_0.1.npy",
|
2019-08-11 11:10:50 +02:00
|
|
|
std::filesystem::current_path() / "tests/TestFiles/OnePoleFilter/OPF_high_gain_0.1.npy",
|
2019-08-25 14:01:03 +02:00
|
|
|
0.1f);
|
2019-08-11 11:10:50 +02:00
|
|
|
testHighpass<double>(
|
2019-08-09 18:24:27 +02:00
|
|
|
std::filesystem::current_path() / "tests/TestFiles/OnePoleFilter/OPF_input_gain_0.3.npy",
|
2019-08-11 11:10:50 +02:00
|
|
|
std::filesystem::current_path() / "tests/TestFiles/OnePoleFilter/OPF_high_gain_0.3.npy",
|
2019-08-25 14:01:03 +02:00
|
|
|
0.3f);
|
2019-08-11 11:10:50 +02:00
|
|
|
testHighpass<double>(
|
2019-08-09 18:24:27 +02:00
|
|
|
std::filesystem::current_path() / "tests/TestFiles/OnePoleFilter/OPF_input_gain_0.5.npy",
|
2019-08-11 11:10:50 +02:00
|
|
|
std::filesystem::current_path() / "tests/TestFiles/OnePoleFilter/OPF_high_gain_0.5.npy",
|
2019-08-25 14:01:03 +02:00
|
|
|
0.5f);
|
2019-08-11 11:10:50 +02:00
|
|
|
testHighpass<double>(
|
2019-08-09 18:24:27 +02:00
|
|
|
std::filesystem::current_path() / "tests/TestFiles/OnePoleFilter/OPF_input_gain_0.7.npy",
|
2019-08-11 11:10:50 +02:00
|
|
|
std::filesystem::current_path() / "tests/TestFiles/OnePoleFilter/OPF_high_gain_0.7.npy",
|
2019-08-25 14:01:03 +02:00
|
|
|
0.7f);
|
2019-08-11 11:10:50 +02:00
|
|
|
testHighpass<double>(
|
2019-08-09 18:24:27 +02:00
|
|
|
std::filesystem::current_path() / "tests/TestFiles/OnePoleFilter/OPF_input_gain_0.9.npy",
|
2019-08-11 11:10:50 +02:00
|
|
|
std::filesystem::current_path() / "tests/TestFiles/OnePoleFilter/OPF_high_gain_0.9.npy",
|
2019-08-25 14:01:03 +02:00
|
|
|
0.9f);
|
2019-08-09 18:24:27 +02:00
|
|
|
}
|