BogaudioModules

BogaudioModules for VCV Rack
Log | Files | Refs | README | LICENSE

commit 20caf7be23b8f2e4d51ce152e8f29325636ea645
parent 97d8e0662db1db25083e94196732cbefa6c24a7f
Author: Matt Demanett <matt@demanett.net>
Date:   Mon, 13 Jan 2020 01:04:36 -0500

Better plotting; scater plotting.

Diffstat:
M.gitignore | 3+++
MMakefile | 20+++++++++++++++++---
Atest/plot.cpp | 48++++++++++++++++++++++++++++++++++++++++++++++++
Atest/plot.gp | 7+++++++
Atest/scatter.cpp | 93+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Atest/scatter.gp | 7+++++++
6 files changed, 175 insertions(+), 3 deletions(-)

diff --git a/.gitignore b/.gitignore @@ -38,4 +38,7 @@ testmain releasenote.md plot plot.* +scatter +scatter.* lldb.cmd +!test/* diff --git a/Makefile b/Makefile @@ -43,8 +43,22 @@ plot: $(PLOT_OBJECTS) plotrun: plot ./plot plotrungp: plot - ./plot > plot.tmp && gnuplot -e "set yrange [0:1.1]; plot 'plot.tmp' using 1:2 with lines" + # ./plot > plot.tmp && gnuplot -p -e "set yrange [0:1.1]; plot 'plot.tmp' using 1:2 with lines" + ./plot > plot.tmp && gnuplot -p test/plot.gp plot_clean: - rm -f plot $(PLOT_OBJECTS) + rm -f plot plot.tmp $(PLOT_OBJECTS) -clean: benchmark_clean testmain_clean plot_clean +SCATTER_SOURCES = $(wildcard test/scatter.cpp src/dsp/*cpp) +SCATTER_OBJECTS = $(patsubst %, build/%.o, $(SCATTER_SOURCES)) +SCATTER_DEPS = $(patsubst %, build/%.d, $(SCATTER_SOURCES)) +-include $(SCATTER_DEPS) +scatter: $(SCATTER_OBJECTS) + $(CXX) -o $@ $^ +scatterrun: scatter + ./scatter +scatterrungp: scatter + ./scatter > scatter.tmp && gnuplot -p test/scatter.gp +scatter_clean: + rm -f scatter scatter.tmp $(SCATTER_OBJECTS) + +clean: benchmark_clean testmain_clean plot_clean scatter_clean diff --git a/test/plot.cpp b/test/plot.cpp @@ -0,0 +1,48 @@ +#include <stdlib.h> +#include <stdio.h> +#include <stdint.h> +#include <string.h> +#include <math.h> +#include <algorithm> + +#include "dsp/analyzer.hpp" +#include "dsp/signal.hpp" + +using namespace bogaudio::dsp; + +// KaiserWindow w(1024); +// float y(float x) { +// return w._window[(int)x]; +// } + +// Chebyshev gain coefficient; x is ripple. +// float y(float x) { +// float e = x / 10.0; +// e = std::pow(10.0f, e); +// e -= 1.0f; +// e = std::sqrt(e); +// +// return 1.0f / (2.0f * e); +// } + +// Q https://en.wikipedia.org/wiki/Q_factor +float y(float x) { + x *= 2.0f; + return std::pow(2.0f, x / 2.0f) / (std::pow(2.0f, x) - 1.0f); +} + +int main() { + const float xMin = 0.1f; + // const float xMax = 1023.0f; + const float xMax = 3.0f; + const float samples = 1024.0f; + + const float delta = (xMax - xMin) / samples; + float x = xMin; + while (x <= xMax) { + printf("%f, %f\n", x, y(x)); + x += delta; + } + // printf("%f\n", y(1.0f)); + return 0; +} diff --git a/test/plot.gp b/test/plot.gp @@ -0,0 +1,7 @@ +set zeroaxis +# r = 2 +# set xrange[-r:r] +# set yrange[-r:r] +set grid + +plot 'plot.tmp' using 1:2 with lines diff --git a/test/scatter.cpp b/test/scatter.cpp @@ -0,0 +1,93 @@ +#include <stdlib.h> +#include <stdio.h> +#include <stdint.h> +#include <string.h> +#include <math.h> +#include <cmath> +#include <algorithm> + +struct Scatter { + virtual ~Scatter() {} + virtual bool next(float& x, float &y) = 0; +}; + +struct TestScatter : Scatter { + int i = 0; + + bool next(float& x, float &y) override { + x = i; + y = i; + ++i; + return i < 5; + } +}; + +struct ButterworthPoles : Scatter { + int k = 1; + int n; + bool conjugate = false; + + ButterworthPoles(int n) : n(n) {} + + bool next(float& x, float &y) override { + float a = ((float)(2 * k + n - 1)) * M_PI / (float)(2 * n); + + x = std::cos(a); + y = std::sin(a); + if (conjugate) { + y = -y; + } + conjugate = !conjugate; + k += !conjugate; + return k <= n / 2; + } +}; + +struct ChebyshevPoles : Scatter { + int k = 1; + int n; + double ripple; + double e; + double ef; + bool conjugate = false; + + ChebyshevPoles(int n, double ripple) : n(n), ripple(ripple) { + e = ripple / 10.0; + e = std::pow(10.0, e); + e -= 1.0f; + e = std::sqrt(e); + + ef = std::asinh(1.0 / e) / (float)n; + } + + bool next(float& x, float &y) override { + double a = ((double)(2 * k + n - 1)) * M_PI / (double)(2 * n); + + x = -std::sinh(ef) * std::sin(a); + y = std::cosh(ef) * std::cos(a); + if (conjugate) { + y = -y; + } + conjugate = !conjugate; + k += !conjugate; + return k <= n / 2; + } +}; + +int main() { + // std::unique_ptr<Scatter> s(new TestScatter()); + // std::unique_ptr<Scatter> s(new ButterworthPoles(16)); + std::unique_ptr<Scatter> s(new ChebyshevPoles(16, 0.01)); + + float x = 0.0f; + float y = 0.0f; + while (true) { + bool c = s->next(x, y); + printf("%f, %f\n", x, y); + if (!c) { + break; + } + } + + return 0; +} diff --git a/test/scatter.gp b/test/scatter.gp @@ -0,0 +1,7 @@ +set zeroaxis +r = 2 +set xrange[-r:r] +set yrange[-r:r] +set grid + +plot "scatter.tmp" using 1:2:(sprintf("(%0.4f, %0.4f)", $1, $2)) with labels point pt 7 offset char 1,1 notitle