summaryrefslogtreecommitdiffstats
path: root/lib/prings_kst.c
diff options
context:
space:
mode:
Diffstat (limited to 'lib/prings_kst.c')
-rw-r--r--lib/prings_kst.c79
1 files changed, 79 insertions, 0 deletions
diff --git a/lib/prings_kst.c b/lib/prings_kst.c
new file mode 100644
index 0000000..e33688e
--- /dev/null
+++ b/lib/prings_kst.c
@@ -0,0 +1,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;
+}
+