summaryrefslogtreecommitdiffstats
path: root/lib/starc_lower.c
diff options
context:
space:
mode:
Diffstat (limited to 'lib/starc_lower.c')
-rw-r--r--lib/starc_lower.c74
1 files changed, 74 insertions, 0 deletions
diff --git a/lib/starc_lower.c b/lib/starc_lower.c
new file mode 100644
index 0000000..97ac02f
--- /dev/null
+++ b/lib/starc_lower.c
@@ -0,0 +1,74 @@
+// Starc Lower
+//
+// starc_lower
+//
+// STARC Lower Band
+//
+//
+//
+// Inputs: 4
+// Outputs: 1
+// Constants: 2
+
+
+#include <math.h>
+#include "ts.h"
+
+typedef struct {
+ uint_t k;
+ fp_t mult;
+} ts_starc_lower_params_t;
+
+ts_parameter_t const ts_starc_lower_p = { .n_inputs = 4, .n_outputs = 1, .n_constants = 2, .params_size = sizeof(ts_starc_lower_params_t) };
+
+uint_t ts_starc_lower_offset( ts_constant_t const *c )
+{
+ ( void )c;
+ return ( c ? ((ts_starc_lower_params_t *)c)->k > 0 ? ((ts_starc_lower_params_t *)c)->k - 1 : 0 : 0 );
+}
+
+int ts_starc_lower(
+ 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_starc_lower_p );
+ if ( !c ) return -1;
+ uint_t k = ((ts_starc_lower_params_t *)c)->k;
+ if( k == 0 ) return -1;
+ fp_t mult = ((ts_starc_lower_params_t *)c)->mult;
+
+ uint_t half_k = k / 2;
+ if (n <= half_k) return -1;
+
+ uint_t last = k - 1;
+ for( uint_t i = k - 1; i < n - half_k; ++i )
+ {
+ fp_t middle_sum = 0;
+ fp_t atr_sum = 0;
+
+ for( uint_t j = 0; j < k; ++j )
+ {
+ uint_t idx = i - half_k + j;
+ middle_sum += ts_c( x, idx );
+
+ fp_t tr = ts_h( x, idx ) - ts_l( x, idx );
+ if( idx > 0 )
+ {
+ fp_t h_pc = fabs( ts_h( x, idx ) - ts_c( x, idx - 1 ) );
+ fp_t l_pc = fabs( ts_l( x, idx ) - ts_c( x, idx - 1 ) );
+ if( h_pc > tr ) tr = h_pc;
+ if( l_pc > tr ) tr = l_pc;
+ }
+ atr_sum += tr;
+ }
+
+ y[0][i] = middle_sum / k - mult * atr_sum / k;
+ last = i;
+ }
+ for( uint_t i = last + 1; i < n; ++i )
+ y[0][i] = y[0][last];
+ return 0;
+}
+