summaryrefslogtreecommitdiffstats
path: root/lib/swing_index.c
diff options
context:
space:
mode:
Diffstat (limited to 'lib/swing_index.c')
-rw-r--r--lib/swing_index.c52
1 files changed, 52 insertions, 0 deletions
diff --git a/lib/swing_index.c b/lib/swing_index.c
new file mode 100644
index 0000000..2f7c563
--- /dev/null
+++ b/lib/swing_index.c
@@ -0,0 +1,52 @@
+// Swing Index
+//
+// swing_index
+//
+// Swing Index
+//
+//
+//
+// Inputs: 5
+// Outputs: 1
+// Constants: 0
+
+
+#include <math.h>
+#include "ts.h"
+
+
+ts_parameter_t const ts_swing_index_p = { .n_inputs = 5, .n_outputs = 1, .n_constants = 0, .params_size = 0 };
+
+uint_t ts_swing_index_offset( ts_constant_t const *c )
+{
+ ( void )c;
+ return ( 1 );
+}
+
+int ts_swing_index(
+ 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_swing_index_p );
+ (void)c;
+ if (n > 0) y[0][0] = 0;
+ for( uint_t i = 1; i < n; ++i )
+ {
+ fp_t a1 = ts_h( x, i ) - ts_h( x, i - 1 );
+ fp_t a2 = ts_l( x, i - 1 ) - ts_l( x, i );
+ fp_t a = fmax( a1, a2 );
+ fp_t b = ts_c( x, i ) - ts_c( x, i - 1 );
+ fp_t ldpl = ts_l( x, i ) - ts_l( x, i - 1 );
+ fp_t phmh = ts_h( x, i - 1 ) - ts_h( x, i );
+ fp_t c_val = 0;
+ if( b < 0 && ldpl > phmh ) c_val = ldpl;
+ else if( b >= 0 && phmh > ldpl ) c_val = phmh;
+ fp_t hl = ts_h( x, i ) - ts_l( x, i );
+ fp_t swing = ( hl != 0 ) ? ( a * 2.0 + b - c_val ) * ts_v( x, i ) / hl : 0;
+ y[0][i] = ( i > 1 ? y[0][i - 1] : 0 ) + swing;
+ }
+ return 0;
+}
+