summaryrefslogtreecommitdiffstats
path: root/lib/pretty_good_oscillator.c
blob: 2dbc6f12677b28115dc2f33b7388e21fd416f6b6 (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
// Pretty Good Oscillator
//
// pretty_good_oscillator
//
// Pretty Good Oscillator
//
// 
//
// Inputs:    1
// Outputs:   1
// Constants: 0


#include "ts.h"


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

uint_t ts_pretty_good_oscillator_offset( ts_constant_t const *c )
{
	( void )c;
	return ( 0 );
}

int ts_pretty_good_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_pretty_good_oscillator_p );
	(void)c;

    fp_t ema14 = x[0][0];
    fp_t ema30 = x[0][0];
    fp_t ema90 = x[0][0];
    fp_t a14 = 15.0 / (19.0 * 14);
    fp_t a30 = 15.0 / (19.0 * 30);
    fp_t a90 = 15.0 / (19.0 * 90);

    for( uint_t i = 0; i < n; ++i )
    {
        if( i > 0 )
        {
            ema14 = ema14 + a14 * (x[0][i] - ema14);
            ema30 = ema30 + a30 * (x[0][i] - ema30);
            ema90 = ema90 + a90 * (x[0][i] - ema90);
        }

        fp_t arr[3] = {ema14, ema30, ema90};
        for( uint_t a = 0; a < 2; ++a )
            for( uint_t b = a + 1; b < 3; ++b )
                if( arr[a] > arr[b] ) { fp_t t = arr[a]; arr[a] = arr[b]; arr[b] = t; }

        fp_t median = arr[1];

        if( median == 0 )
            y[0][i] = 0;
        else
            y[0][i] = (x[0][i] / median - 1.0);
    }
	return 0;
}