summaryrefslogtreecommitdiffstats
path: root/lib/starc_lower.c
blob: 97ac02f9a4317c2cc5e32bf2ba7d3a54402c462b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
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;
}