From 07e18043145f3d91c8ea7ec3ad41b3c905585277 Mon Sep 17 00:00:00 2001 From: Ethan Werner Date: Wed, 1 Jul 2026 09:42:39 +0400 Subject: initial commit: ts time series analysis library v0.1.0 - 354 financial and mathematical time-series indicators - Uniform C11 API with consistent function signatures - 349 passing tests with -Werror - Static and shared library builds - pkg-config integration - Python bindings - Fuzzing and sanitizer support - Security hardening flags --- lib/zigzag.c | 99 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 99 insertions(+) create mode 100644 lib/zigzag.c (limited to 'lib/zigzag.c') diff --git a/lib/zigzag.c b/lib/zigzag.c new file mode 100644 index 0000000..50549e2 --- /dev/null +++ b/lib/zigzag.c @@ -0,0 +1,99 @@ +// Zigzag +// +// zigzag +// +// ZigZag Filter +// +// +// +// Inputs: 1 +// Outputs: 2 +// Constants: 1 + + +#include "ts.h" + + +typedef struct { + fp_t threshold; +} ts_zigzag_params_t; + +ts_parameter_t const ts_zigzag_p = { .n_inputs = 1, .n_outputs = 2, .n_constants = 1, .params_size = sizeof(ts_zigzag_params_t) }; + + +uint_t ts_zigzag_offset( ts_constant_t const *c ) +{ + ( void )c; + return ( 0 ); +} + +int ts_zigzag( + 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_zigzag_p ); + if ( !c ) return -1; + if (n == 0) return -1; + + fp_t threshold = ((ts_zigzag_params_t *)c)->threshold; + if (threshold <= 0 || isnan(threshold) || isinf(threshold)) return -1; + + y[0][0] = x[0][0]; + y[1][0] = 0; + + for( uint_t i = 1; i < n; ++i ) + { + fp_t last_zz = y[0][i - 1]; + fp_t dir = y[1][i - 1]; + fp_t change = last_zz != 0 ? ( x[0][i] - last_zz ) / last_zz * 100.0 : 0; + + if( dir == 0 ) + { + if( change >= threshold ) + { + y[0][i] = x[0][i]; + y[1][i] = 1; + } + else if( change <= -threshold ) + { + y[0][i] = x[0][i]; + y[1][i] = -1; + } + else + { + y[0][i] = last_zz; + y[1][i] = 0; + } + } + else if( dir == 1 ) + { + if( change <= -threshold ) + { + y[0][i] = x[0][i]; + y[1][i] = -1; + } + else + { + y[0][i] = last_zz; + y[1][i] = 1; + } + } + else + { + if( change >= threshold ) + { + y[0][i] = x[0][i]; + y[1][i] = 1; + } + else + { + y[0][i] = last_zz; + y[1][i] = -1; + } + } + } + return 0; +} + -- cgit v1.3.1