summaryrefslogtreecommitdiffstats
path: root/test/oscillator_cross.c
diff options
context:
space:
mode:
Diffstat (limited to 'test/oscillator_cross.c')
-rw-r--r--test/oscillator_cross.c40
1 files changed, 40 insertions, 0 deletions
diff --git a/test/oscillator_cross.c b/test/oscillator_cross.c
new file mode 100644
index 0000000..323718b
--- /dev/null
+++ b/test/oscillator_cross.c
@@ -0,0 +1,40 @@
+#include <math.h>
+#include <stddef.h>
+#include "ts.h"
+
+bool ts_test_oscillator_cross( void )
+{
+ /* Data that goes down then up, producing a clear crossover */
+ fp_t const x[] = { 10, 9, 8, 7, 6, 5, 6, 7, 8, 9 };
+
+ fp_t const * const inp[1] = { x };
+
+ /* fast=2, slow=4 */
+ fp_t fast_e = 10, slow_e = 10;
+ fp_t fast_a = 1.0 / 2, slow_a = 1.0 / 4;
+ fp_t e[6] = { 0 };
+
+ for( uint_t i = 1; i < 10; ++i )
+ {
+ fp_t pf = fast_e, ps = slow_e;
+ fast_e = fast_e + fast_a * ( x[i] - fast_e );
+ slow_e = slow_e + slow_a * ( x[i] - slow_e );
+
+ if( i >= 4 )
+ {
+ if( fast_e > slow_e && pf <= ps )
+ e[i - 4] = 1;
+ else if( fast_e < slow_e && pf >= ps )
+ e[i - 4] = -1;
+ else
+ e[i - 4] = 0;
+ }
+ }
+
+ fp_t const * const exp[1] = { e };
+
+ typedef struct { fp_t fast; fp_t slow; } c_t;
+ c_t c = { .fast = 2, .slow = 4 };
+
+ return ts_test(ts_oscillator_cross, ts_oscillator_cross_offset, inp, exp, sizeof( x ) / sizeof( *x ), (ts_constant_t *)&c, &ts_oscillator_cross_p);
+}