From b7c3c15b971eed7c932dcf667332e7d714f8451b Mon Sep 17 00:00:00 2001 From: Segfault1602 Date: Wed, 25 Oct 2023 18:47:13 -0700 Subject: [PATCH] Adding scattering test --- .vscode/launch.json | 4 +- src/bowed_string.cpp | 24 ------------ tests/CMakeLists.txt | 1 + tests/scattering_tests.cpp | 76 +++++++++++++++++++++++++++++--------- tests/test_utils.cpp | 38 +++++++++++++++++++ tests/test_utils.h | 9 +++++ tests/waveguide_tests.cpp | 25 +------------ 7 files changed, 109 insertions(+), 68 deletions(-) create mode 100644 tests/test_utils.cpp create mode 100644 tests/test_utils.h diff --git a/.vscode/launch.json b/.vscode/launch.json index 42dabfd..76a2326 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -70,7 +70,7 @@ "request": "launch", "program": "${workspaceFolder}/build/tests/libdsp_tests", "args": [ - "--gtest_filter=ScatterTests/ScatterTest.Scatter2/135" + "--gtest_filter=ScatterTests/ScatterTest*" ], "stopAtEntry": false, "cwd": "${fileDirname}", @@ -84,7 +84,7 @@ "request": "launch", "program": "${workspaceFolder}/build/tests/libdsp_tests.exe", "args": [ - "--gtest_filter=Basic*" + "--gtest_filter=ScatterTest*" ], "stopAtEntry": false, "cwd": "${fileDirname}", diff --git a/src/bowed_string.cpp b/src/bowed_string.cpp index 487936f..e13194f 100644 --- a/src/bowed_string.cpp +++ b/src/bowed_string.cpp @@ -6,30 +6,6 @@ #include "basic_oscillators.h" #include "window_functions.h" -// void PrintWaveguide(dsp::Waveguide& wave, size_t delay_size) -// { -// std::vector right_samples, left_samples; -// for (size_t i = 1; i <= delay_size; ++i) -// { -// float right, left; -// wave.TapOut(i, right, left); -// right_samples.push_back(right); -// left_samples.push_back(left); -// } - -// for (auto sample : right_samples) -// { -// printf("%s%5.3f ", sample >= 0 ? " " : "", sample); -// } -// printf("\n"); - -// for (auto sample : left_samples) -// { -// printf("%s%5.3f ", sample >= 0 ? " " : "", sample); -// } -// printf("\n"); -// } - namespace dsp { diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index de48e2e..d9da9a0 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -6,6 +6,7 @@ set(TEST_SOURCES rms_tests.cpp scattering_tests.cpp sinc_resampler_tests.cpp + test_utils.cpp waveguide_tests.cpp) set(SOURCES ${TEST_SOURCES}) diff --git a/tests/scattering_tests.cpp b/tests/scattering_tests.cpp index 3640303..3d17c49 100644 --- a/tests/scattering_tests.cpp +++ b/tests/scattering_tests.cpp @@ -6,28 +6,68 @@ #include #include "junction.h" +#include "test_utils.h" +#include "waveguide.h" -class ScatterTest : public ::testing::TestWithParam, float>> +TEST(ScatterTest, ConserveEnergy) { -}; + constexpr size_t max_size = 10; + constexpr size_t delay_size = 6; + dsp::Waveguide wave_(max_size); + wave_.SetDelay(delay_size); -// Test that the energy is conserved -TEST_P(ScatterTest, Scatter2) -{ - std::array inputs = std::get<0>(GetParam()); - float k = std::get<1>(GetParam()); - auto outputs = dsp::Scatter2(inputs, k); + // insert dirac + wave_.TapIn(3, 1.f, 0.f); + + PrintWaveguide(wave_, delay_size); + + constexpr size_t loop_size = 100; + + float start_energy = GetWaveguideTotalEnergy(wave_, delay_size); + printf("Start energy: %f\n", start_energy); + + for (size_t i = 0; i < loop_size; ++i) + { + float right, left; + wave_.TapOut(3, right, left); + + auto scatter_out = dsp::Scatter2({right, left}, 0.5f); - auto input_sum = std::accumulate(inputs.begin(), inputs.end(), 0.f); - auto output_sum = std::accumulate(outputs.begin(), outputs.end(), 0.f); + wave_.TapIn(3, scatter_out[0] - right, scatter_out[1] - left); - ASSERT_THAT(output_sum, ::testing::FloatNear(input_sum, 0.001f)); + wave_.NextOut(right, left); + wave_.Tick(left * -1, right * -1); + + float new_energy = GetWaveguideTotalEnergy(wave_, delay_size); + // printf("%f\n", new_energy); + printf("iter #%zu, energy: %f\n", i, new_energy); + PrintWaveguide(wave_, delay_size); + + // ASSERT_THAT(new_energy, ::testing::FloatNear(start_energy, 0.001f)); + } } -INSTANTIATE_TEST_SUITE_P( - ScatterTests, ScatterTest, - ::testing::Combine(::testing::Values(std::array{1.f, 1.f}, std::array{0.f, 1.f}, - std::array{1.f, 0.f}, std::array{-1.f, -1.f}, - std::array{0.25f, 0.50f}, std::array{0.75f, -1.f}, - std::array{-0.5f, 0.5f}, std::array{0.f, 0.f}), - ::testing::Range(-1.f, 1.f, 0.1f))); \ No newline at end of file +// class ScatterTest : public ::testing::TestWithParam, float>> +// { +// }; + +// // Test that the energy is conserved +// TEST_P(ScatterTest, Scatter2) +// { +// std::array inputs = std::get<0>(GetParam()); +// float k = std::get<1>(GetParam()); +// auto outputs = dsp::Scatter2(inputs, k); + +// auto input_sum = std::accumulate(inputs.begin(), inputs.end(), 0.f); +// auto output_sum = std::accumulate(outputs.begin(), outputs.end(), 0.f); + +// ASSERT_THAT(output_sum, ::testing::FloatNear(input_sum, 0.001f)); +// } + +// INSTANTIATE_TEST_SUITE_P( +// ScatterTests, ScatterTest, +// ::testing::Combine(::testing::Values(std::array{1.f, 1.f}, std::array{0.f, 1.f}, +// std::array{1.f, 0.f}, std::array{-1.f, -1.f}, +// std::array{0.25f, 0.50f}, std::array{0.75f, -1.f}, +// std::array{-0.5f, 0.5f}, std::array{0.f, 0.f}), +// ::testing::Range(-1.f, 1.f, 0.1f))); \ No newline at end of file diff --git a/tests/test_utils.cpp b/tests/test_utils.cpp new file mode 100644 index 0000000..28ef288 --- /dev/null +++ b/tests/test_utils.cpp @@ -0,0 +1,38 @@ +#include "test_utils.h" + +void PrintWaveguide(dsp::Waveguide& wave, size_t delay_size) +{ + std::vector right_samples, left_samples; + for (size_t i = 1; i <= delay_size; ++i) + { + float right, left; + wave.TapOut(i, right, left); + right_samples.push_back(right); + left_samples.push_back(left); + } + + for (auto sample : right_samples) + { + printf("%5.1f ", sample); + } + printf("\n"); + + for (auto sample : left_samples) + { + printf("%5.1f ", sample); + } + printf("\n"); +} + +float GetWaveguideTotalEnergy(dsp::Waveguide& wave, size_t delay_size) +{ + float total_energy = 0.f; + for (size_t i = 1; i <= delay_size; ++i) + { + float right, left; + wave.TapOut(i, right, left); + total_energy += right * right + left * left; + } + + return total_energy; +} \ No newline at end of file diff --git a/tests/test_utils.h b/tests/test_utils.h new file mode 100644 index 0000000..1f9150b --- /dev/null +++ b/tests/test_utils.h @@ -0,0 +1,9 @@ +#pragma once + +#include + +#include "waveguide.h" + +void PrintWaveguide(dsp::Waveguide& wave, size_t delay_size); + +float GetWaveguideTotalEnergy(dsp::Waveguide& wave, size_t delay_size); \ No newline at end of file diff --git a/tests/waveguide_tests.cpp b/tests/waveguide_tests.cpp index d079bbe..90a2f5a 100644 --- a/tests/waveguide_tests.cpp +++ b/tests/waveguide_tests.cpp @@ -2,33 +2,10 @@ #include "gtest/gtest.h" #include "termination.h" +#include "test_utils.h" #include "waveguide.h" #include "window_functions.h" -void PrintWaveguide(dsp::Waveguide& wave, size_t delay_size) -{ - std::vector right_samples, left_samples; - for (size_t i = 1; i <= delay_size; ++i) - { - float right, left; - wave.TapOut(i, right, left); - right_samples.push_back(right); - left_samples.push_back(left); - } - - for (auto sample : right_samples) - { - printf("%5.1f ", sample); - } - printf("\n"); - - for (auto sample : left_samples) - { - printf("%5.1f ", sample); - } - printf("\n"); -} - TEST(WaveguideTests, EmptyWaveguide) { // Check that energy is not magically introduce into the waveguide