~mrp/aerodramus

439562959274e9c82497fa0b9e82d449b14d553c — Mark Penner 5 months ago 99ed758
add rms function
3 files changed, 20 insertions(+), 0 deletions(-)

M autotests/lib/aerodramus.test.cpp
M src/lib/aerodramus.cpp
M src/lib/aerodramus.h
M autotests/lib/aerodramus.test.cpp => autotests/lib/aerodramus.test.cpp +10 -0
@@ 4,6 4,7 @@
#include "../../src/lib/aerodramus.h"
#include <QTemporaryFile>
#include <QtTest>
#include <cmath>
#include <sndfile.hh>
#include <vector>



@@ 14,6 15,7 @@ class TestLibAerodramus : public QObject
private:
private Q_SLOTS:
    void test_generate_signal();
    void test_rms();
    void test_SoundFile();
    void test_dB_from_point_source();
    void test_watts_needed_to_point_source();


@@ 28,6 30,14 @@ void TestLibAerodramus::test_generate_signal()
    QCOMPARE(data.size(), length * sample_rate);
}

void TestLibAerodramus::test_rms()
{
    QCOMPARE(Aerodramus::rms({1.0, -1.0, 1.0}), 1);
    QCOMPARE(Aerodramus::rms({-123456789.987, 987654321.123, -456321789.321}), 632174082.679749);
    double expected{std::round(100.0 / std::numbers::sqrt2)}; // factor of 100 and round because the extra precision isn't relevant for testing
    QCOMPARE(std::round(Aerodramus::rms(Aerodramus::generate_signal(200, 20000, 20, 44100)) * 100), expected);
}

void TestLibAerodramus::test_SoundFile()
{
    Aerodramus::SoundFile soundfile1{};

M src/lib/aerodramus.cpp => src/lib/aerodramus.cpp +9 -0
@@ 7,6 7,7 @@
#include <climits>
#include <cmath>
#include <numbers>
#include <numeric>
#include <ranges>
#include <string>
#include <vector>


@@ 64,6 65,14 @@ std::vector<double> Aerodramus::generate_signal(double start_freq, double end_fr
    return data;
}

double Aerodramus::rms(std::vector<double> const &data)
{
    double summed_squares = std::transform_reduce(data.cbegin(), data.cend(), 0.0L, std::plus{}, [](auto x) {
        return x * x;
    });
    return std::sqrt(summed_squares / data.size());
}

double Aerodramus::dB_from_point_source(double sensitivity, double distance, double watts)
{
    double dB_loss{20 * std::log10(1 / distance)};

M src/lib/aerodramus.h => src/lib/aerodramus.h +1 -0
@@ 23,6 23,7 @@ SoundFile read(std::string const &path);
bool write(SoundFile const &soundfile);

std::vector<double> generate_signal(double start_freq, double end_freq, double seconds, long sample_rate);
double rms(std::vector<double> const &data);
double dB_from_point_source(double sensitivity, double distance, double watts);
double watts_needed_to_point_source(double sensitivity, double distance, double dB);
std::vector<int> zero_cross_histogram(const std::vector<float> &samples, int bins);