summaryrefslogtreecommitdiffstats
path: root/lib/alligator.c
blob: 81450b005df622e2228457dcf3b4a32d41caff0c (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
// Alligator Indicator
//
// William Elder's Alligator uses three SMAs shifted forward in time:
// Jaw:   SMA(13) shifted 8 bars
// Teeth: SMA(8) shifted 5 bars
// Lips:  SMA(5) shifted 3 bars
//
// X_n - 1
// Y_n - 3
// C_n - 0

#include "ts.h"


ts_parameter_t const ts_alligator_p = { .n_inputs = 1, .n_outputs = 3, .n_constants = 0, .params_size = 0 };

uint_t ts_alligator_offset( ts_constant_t const *c )
{
	( void )c;
	return ( 12 );
}

int ts_alligator(
	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_alligator_p );
	(void)c;
    if( n == 0 ) return -1;
    /* Jaw: SMA(13) shifted 8 positions */
    /* Teeth: SMA(8) shifted 5 positions */
    /* Lips: SMA(5) shifted 3 positions */

    /* Jaw: SMA13 of x[i-8..i+4], displayed at i */
    for (uint_t i = 8; i - 8 + 13 <= n; ++i)
    {
        fp_t sum = 0;
        for (uint_t j = 0; j < 13; ++j)
        {
            sum += x[0][i - 8 + j];
        }
        y[0][i] = sum / 13;
    }

    /* Teeth: SMA8 of x[i-5..i+2], displayed at i */
    for (uint_t i = 5; i - 5 + 8 <= n; ++i)
    {
        fp_t sum = 0;
        for (uint_t j = 0; j < 8; ++j)
        {
            sum += x[0][i - 5 + j];
        }
        y[1][i] = sum / 8;
    }

    /* Lips: SMA5 of x[i-3..i+1], displayed at i */
    for (uint_t i = 3; i - 3 + 5 <= n; ++i)
    {
        fp_t sum = 0;
        for (uint_t j = 0; j < 5; ++j)
        {
            sum += x[0][i - 3 + j];
        }
        y[2][i] = sum / 5;
    }
	return 0;
}