summaryrefslogtreecommitdiffstats
path: root/lib/fractal_chaos_bands.c
blob: 1cbef901b8df2b8cdf92fd724e6f59e471c10828 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
// Fractal Chaos Bands
//
// fractal_chaos_bands
//
// Elder's Fractal Chaos Bands using fractal channels
//
// Upper/Lower: fractal-based channel boundaries
//
// Inputs:    4
// Outputs:   2
// Constants: 1


#include "ts.h"

typedef struct {
    uint_t k;
} ts_fractal_chaos_bands_params_t;

ts_parameter_t const ts_fractal_chaos_bands_p = { .n_inputs = 4, .n_outputs = 2, .n_constants = 1, .params_size = sizeof(ts_fractal_chaos_bands_params_t) };

uint_t ts_fractal_chaos_bands_offset( ts_constant_t const *c )
{
	( void )c;
	return ( c ? ((ts_fractal_chaos_bands_params_t const *)c)->k / 2 : 0 );
}

int ts_fractal_chaos_bands(
	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_bands_p );
	if ( !c ) return -1;
	TS_CHECK_INPUT(x, x_n, n);
    uint_t k = ((ts_fractal_chaos_bands_params_t const *)c)->k;
    if (k == 0 || n < k) return -1;

    fp_t alpha = 2.0 / (k + 1.0);
    uint_t half_k = k / 2;

    fp_t ema_upper = 0;
    fp_t ema_lower = 0;
    uint_t ema_count = 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 (ema_count == 0)
        {
            ema_upper = fh;
            ema_lower = fl;
        }
        else
        {
            ema_upper = ema_upper + alpha * (fh - ema_upper);
            ema_lower = ema_lower + alpha * (fl - ema_lower);
        }
        ema_count++;

        y[0][i] = ema_upper;
        y[1][i] = ema_lower;
    }

    for ( uint_t i = n - half_k; i < n; ++i )
    {
        y[0][i] = 0;
        y[1][i] = 0;
    }

	return 0;
}