Skip to content

Commit

Permalink
Adding scattering test
Browse files Browse the repository at this point in the history
  • Loading branch information
Segfault1602 committed Oct 26, 2023
1 parent 9d5c4e5 commit b7c3c15
Show file tree
Hide file tree
Showing 7 changed files with 109 additions and 68 deletions.
4 changes: 2 additions & 2 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -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}",
Expand All @@ -84,7 +84,7 @@
"request": "launch",
"program": "${workspaceFolder}/build/tests/libdsp_tests.exe",
"args": [
"--gtest_filter=Basic*"
"--gtest_filter=ScatterTest*"
],
"stopAtEntry": false,
"cwd": "${fileDirname}",
Expand Down
24 changes: 0 additions & 24 deletions src/bowed_string.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,30 +6,6 @@
#include "basic_oscillators.h"
#include "window_functions.h"

// void PrintWaveguide(dsp::Waveguide& wave, size_t delay_size)
// {
// std::vector<float> 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
{

Expand Down
1 change: 1 addition & 0 deletions tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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})
Expand Down
76 changes: 58 additions & 18 deletions tests/scattering_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,28 +6,68 @@
#include <tuple>

#include "junction.h"
#include "test_utils.h"
#include "waveguide.h"

class ScatterTest : public ::testing::TestWithParam<std::tuple<std::array<float, 2>, 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<float, 2> 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<float, 2>{1.f, 1.f}, std::array<float, 2>{0.f, 1.f},
std::array<float, 2>{1.f, 0.f}, std::array<float, 2>{-1.f, -1.f},
std::array<float, 2>{0.25f, 0.50f}, std::array<float, 2>{0.75f, -1.f},
std::array<float, 2>{-0.5f, 0.5f}, std::array<float, 2>{0.f, 0.f}),
::testing::Range(-1.f, 1.f, 0.1f)));
// class ScatterTest : public ::testing::TestWithParam<std::tuple<std::array<float, 2>, float>>
// {
// };

// // Test that the energy is conserved
// TEST_P(ScatterTest, Scatter2)
// {
// std::array<float, 2> 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<float, 2>{1.f, 1.f}, std::array<float, 2>{0.f, 1.f},
// std::array<float, 2>{1.f, 0.f}, std::array<float, 2>{-1.f, -1.f},
// std::array<float, 2>{0.25f, 0.50f}, std::array<float, 2>{0.75f, -1.f},
// std::array<float, 2>{-0.5f, 0.5f}, std::array<float, 2>{0.f, 0.f}),
// ::testing::Range(-1.f, 1.f, 0.1f)));
38 changes: 38 additions & 0 deletions tests/test_utils.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#include "test_utils.h"

void PrintWaveguide(dsp::Waveguide& wave, size_t delay_size)
{
std::vector<float> 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;
}
9 changes: 9 additions & 0 deletions tests/test_utils.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#pragma once

#include <vector>

#include "waveguide.h"

void PrintWaveguide(dsp::Waveguide& wave, size_t delay_size);

float GetWaveguideTotalEnergy(dsp::Waveguide& wave, size_t delay_size);
25 changes: 1 addition & 24 deletions tests/waveguide_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<float> 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
Expand Down

0 comments on commit b7c3c15

Please sign in to comment.