From de64b7014b9dc54fe06b52e14dfc685d72f6ab0b Mon Sep 17 00:00:00 2001 From: Vito Caputo Date: Mon, 3 Feb 2020 05:41:11 -0800 Subject: libs/sig: add sig_ops_rand random generator (Ab)uses rand_r by feeding ticks_ms as seedp for pseudo-random numbers deterministically derived from ticks_ms. --- src/libs/sig/Makefile.am | 2 +- src/libs/sig/ops_rand.c | 16 ++++++++++++++++ src/libs/sig/sig.c | 6 ++++++ src/libs/sig/sig.h | 1 + 4 files changed, 24 insertions(+), 1 deletion(-) create mode 100644 src/libs/sig/ops_rand.c (limited to 'src/libs') diff --git a/src/libs/sig/Makefile.am b/src/libs/sig/Makefile.am index 7c35742..55032b4 100644 --- a/src/libs/sig/Makefile.am +++ b/src/libs/sig/Makefile.am @@ -1,3 +1,3 @@ noinst_LIBRARIES = libsig.a -libsig_a_SOURCES = ops_const.c ops_lerp.c ops_mult.c ops_sin.c sig.c sig.h +libsig_a_SOURCES = ops_const.c ops_lerp.c ops_mult.c ops_rand.c ops_sin.c sig.c sig.h libsig_a_CPPFLAGS = -I@top_srcdir@/src diff --git a/src/libs/sig/ops_rand.c b/src/libs/sig/ops_rand.c new file mode 100644 index 0000000..49954c6 --- /dev/null +++ b/src/libs/sig/ops_rand.c @@ -0,0 +1,16 @@ +#include +#include + +#include "sig.h" + + +static float ops_rand_output(void *context, unsigned ticks_ms) +{ + return rand_r(&ticks_ms) * 1.f / (float)RAND_MAX; +} + + +sig_ops_t sig_ops_rand = { + .output = ops_rand_output, +}; + diff --git a/src/libs/sig/sig.c b/src/libs/sig/sig.c index cd179e2..fea83b6 100644 --- a/src/libs/sig/sig.c +++ b/src/libs/sig/sig.c @@ -83,6 +83,12 @@ int main(int argc, char *argv[]) printf("null output=%f\n", sig_output(sig, 0)); sig = sig_free(sig); + sig = sig_new(&sig_ops_rand); + for (unsigned j = 0; j < 2; j++) { + for (unsigned i = 0; i < 10; i++) + printf("rand j=%u i=%u output=%f\n", j, i, sig_output(sig, i)); + } + sig = sig_new(&sig_ops_sin, sig_new(&sig_ops_const, 2.f)); for (unsigned i = 0; i < 1000; i++) printf("sin 2hz output %i=%f\n", i, sig_output(sig, i)); diff --git a/src/libs/sig/sig.h b/src/libs/sig/sig.h index 0a374f5..32bf446 100644 --- a/src/libs/sig/sig.h +++ b/src/libs/sig/sig.h @@ -18,6 +18,7 @@ sig_t * sig_free(sig_t *sig); float sig_output(sig_t *sig, unsigned ticks_ms); extern sig_ops_t sig_ops_const; +extern sig_ops_t sig_ops_rand; extern sig_ops_t sig_ops_sin; /* TODO: -- cgit v1.2.3