summaryrefslogtreecommitdiffstats
path: root/lib/prings_kst.c
blob: e33688eeef13bc9698d95377de51469b2f591898 (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
// Prings Kst
//
// prings_kst
//
// Prings KST
//
// 
//
// Inputs:    1
// Outputs:   2
// Constants: 0


#include <stdlib.h>
#include "ts.h"


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

uint_t ts_prings_kst_offset( ts_constant_t const *c )
{
	( void )c;
	return ( 8 );
}

int ts_prings_kst(
	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_prings_kst_p );
	(void)c;
    if (n < 23) return -1;

    fp_t *raw_kst = calloc( n, sizeof( fp_t ) );
    if ( !raw_kst ) return -1;

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

    for( uint_t i = 8; i < n; ++i )
    {
        if ( x[0][i - 2] == 0 || x[0][i - 3] == 0 || x[0][i - 5] == 0 || x[0][i - 8] == 0 )
        {
            raw_kst[i] = 0;
            continue;
        }
        fp_t r2 = ( x[0][i] - x[0][i - 2] ) / x[0][i - 2] * 100;
        fp_t r3 = ( x[0][i] - x[0][i - 3] ) / x[0][i - 3] * 100;
        fp_t r5 = ( x[0][i] - x[0][i - 5] ) / x[0][i - 5] * 100;
        fp_t r8 = ( x[0][i] - x[0][i - 8] ) / x[0][i - 8] * 100;
        raw_kst[i] = r2 + 2 * r3 + 3 * r5 + 4 * r8;
    }

    fp_t alpha1 = 2.0 / 11.0;
    y[0][8] = raw_kst[8];
    for( uint_t i = 9; i < n; ++i )
    {
        y[0][i] = y[0][i - 1] + alpha1 * ( raw_kst[i] - y[0][i - 1] );
    }

    fp_t alpha2 = 2.0 / 16.0;
    for( uint_t i = 8; i < 16; ++i )
        y[1][i] = 0;
    {
        fp_t seed = 0;
        for( uint_t i = 8; i <= 16; ++i ) seed += y[0][i];
        seed /= 9.0;
        y[1][16] = seed;
    }
    for( uint_t i = 17; i < n; ++i )
    {
        y[1][i] = y[1][i - 1] + alpha2 * ( y[0][i] - y[1][i - 1] );
    }

    free( raw_kst );
	return 0;
}