This is a trivial test for the Gaussian Noise class but also serves as implementation example
#include <cmath>
#include <memory>
#include <random>
#include <gtest/gtest.h>
{
std::random_device rd;
std::mt19937 eng(rd());
std::uniform_real_distribution<> distr(min, max);
return distr(eng);
}
TEST(NoiseTest, gaussianNoiseTest)
{
size_t nTests = 100;
size_t nSamples = 100000;
for (
size_t i = 0;
i < nTests;
i++)
{
double meanMeas = 0;
double variance = 0;
for (size_t j = 0; j < nSamples; j++)
{
double sample = gNoise();
meanMeas += sample / nSamples;
double deviation = (sample - mean);
variance += (deviation * deviation) / nSamples;
}
ASSERT_NEAR(mean, meanMeas, std::fabs(stdDev / 20.0));
double stdDevMeasured = std::sqrt(variance);
ASSERT_NEAR(stdDev, stdDevMeasured, stdDev / 15.0);
}
}
TEST(NoiseTest, quantizationNoiseTest)
{
size_t nTests = 1000;
size_t nSamples = 10000;
double bias = 0;
for (
size_t i = 0;
i < nTests;
i++)
{
double meanMeas = 0;
double lastSample = 0;
for (size_t j = 0; j < nSamples; j++)
{
double sample = gNoise();
if (j > 0)
{
double quantization =
std::fmod(std::fabs(sample - lastSample) + quantizationInterval / 1000.0, quantizationInterval);
ASSERT_NEAR(std::fabs(quantization), 0.0, quantizationInterval / 100.0);
}
meanMeas += sample / nSamples;
lastSample = sample;
}
ASSERT_NEAR(mean, meanMeas, 999.0 / 1000.0);
bias += ((meanMeas - mean) / mean) / nTests;
}
ASSERT_NEAR(bias, 0, 1e-2);
}
int main(
int argc,
char** argv)
{
testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}