summaryrefslogtreecommitdiffstats
path: root/lib/fractal_chaos_oscillator.c
diff options
context:
space:
mode:
Diffstat (limited to 'lib/fractal_chaos_oscillator.c')
-rw-r--r--lib/fractal_chaos_oscillator.c73
1 files changed, 73 insertions, 0 deletions
diff --git a/lib/fractal_chaos_oscillator.c b/lib/fractal_chaos_oscillator.c
new file mode 100644
index 0000000..5c845d3
--- /dev/null
+++ b/lib/fractal_chaos_oscillator.c
@@ -0,0 +1,73 @@
+// Fractal Chaos Oscillator
+//
+// fractal_chaos_oscillator
+//
+// Fractal Chaos Oscillator
+//
+// Oscillator derived from fractal chaos bands
+//
+// Inputs: 4
+// Outputs: 1
+// Constants: 1
+
+
+#include "ts.h"
+
+typedef struct {
+ uint_t k;
+} ts_fractal_chaos_oscillator_params_t;
+
+ts_parameter_t const ts_fractal_chaos_oscillator_p = { .n_inputs = 4, .n_outputs = 1, .n_constants = 1, .params_size = sizeof(ts_fractal_chaos_oscillator_params_t) };
+
+uint_t ts_fractal_chaos_oscillator_offset( ts_constant_t const *c )
+{
+ ( void )c;
+ return ( c ? ((ts_fractal_chaos_oscillator_params_t *)c)->k / 2 : 0 );
+}
+
+int ts_fractal_chaos_oscillator(
+ fp_t * const *y, uint_t y_n,
+ fp_t const * const *x, uint_t x_n,
+ uint_t n, ts_constant_t const *c
+)
+{
+ TS_PROLOGUE( ts_fractal_chaos_oscillator_p );
+ if ( !c ) return -1;
+ uint_t k = ((ts_fractal_chaos_oscillator_params_t *)c)->k;
+ if (k == 0 || n < k) return -1;
+
+ fp_t alpha = 2.0 / (k + 1.0);
+ fp_t ema_upper = 0;
+ fp_t ema_lower = 0;
+ uint_t half_k = k / 2;
+ uint_t init = 0;
+
+ for (uint_t i = half_k; i < n - half_k; ++i)
+ {
+ fp_t fh = x[1][i];
+ fp_t fl = x[2][i];
+ for (int_t j = -(int_t)half_k; j <= (int_t)half_k; ++j)
+ {
+ uint_t idx = (uint_t)((int_t)i + j);
+ if (x[1][idx] > fh) fh = x[1][idx];
+ if (x[2][idx] < fl) fl = x[2][idx];
+ }
+
+ if (!init)
+ {
+ ema_upper = fh;
+ ema_lower = fl;
+ init = 1;
+ }
+ else
+ {
+ ema_upper = ema_upper + alpha * (fh - ema_upper);
+ ema_lower = ema_lower + alpha * (fl - ema_lower);
+ }
+
+ fp_t denom = ema_upper + ema_lower;
+ y[0][i] = (denom != 0) ? (ema_upper - ema_lower) / denom * 100.0 : 0;
+ }
+ return 0;
+}
+