diff options
-rw-r--r-- | example/ndpiReader.c | 20 | ||||
-rw-r--r-- | src/include/ndpi_api.h.in | 6 | ||||
-rw-r--r-- | src/include/ndpi_typedefs.h | 8 | ||||
-rw-r--r-- | src/lib/ndpi_analyze.c | 62 |
4 files changed, 96 insertions, 0 deletions
diff --git a/example/ndpiReader.c b/example/ndpiReader.c index f837a5c29..a2caeaad9 100644 --- a/example/ndpiReader.c +++ b/example/ndpiReader.c @@ -3765,6 +3765,25 @@ void hwUnitTest() { /* *********************************************** */ +void jitterUnitTest() { + struct ndpi_jitter_struct jitter; + float v[] = { 10, 14, 8, 25, 16, 22, 14, 35, 15, 27, 218, 40, 28, 40, 25, 65 }; + u_int i, num = sizeof(v) / sizeof(float); + u_int num_learning_points = 4; + u_int8_t trace = 0; + + assert(ndpi_jitter_init(&jitter, num_learning_points) == 0); + + for(i=0; i<num; i++) { + float rc = ndpi_jitter_add_value(&jitter, v[i]); + + if(trace) + printf("%2u)\t%.3f\t%.3f\n", i, v[i], rc); + } +} + +/* *********************************************** */ + /** @brief MAIN FUNCTION **/ @@ -3788,6 +3807,7 @@ int orginal_main(int argc, char **argv) { /* Internal checks */ // binUnitTest(); hwUnitTest(); + jitterUnitTest(); rsiUnitTest(); hashUnitTest(); dgaUnitTest(); diff --git a/src/include/ndpi_api.h.in b/src/include/ndpi_api.h.in index 5e2082bbd..6ecae02d2 100644 --- a/src/include/ndpi_api.h.in +++ b/src/include/ndpi_api.h.in @@ -1404,6 +1404,12 @@ extern "C" { /* ******************************* */ + int ndpi_jitter_init(struct ndpi_jitter_struct *hw, u_int16_t num_periods); + void ndpi_jitter_free(struct ndpi_jitter_struct *hw); + float ndpi_jitter_add_value(struct ndpi_jitter_struct *s, const float value); + + /* ******************************* */ + const char* ndpi_data_ratio2str(float ratio); void ndpi_data_print_window_values(struct ndpi_analyze_struct *s); /* debug */ diff --git a/src/include/ndpi_typedefs.h b/src/include/ndpi_typedefs.h index 7656cb603..6bccd0e77 100644 --- a/src/include/ndpi_typedefs.h +++ b/src/include/ndpi_typedefs.h @@ -1553,6 +1553,14 @@ struct ndpi_rsi_struct { /* **************************************** */ +struct ndpi_jitter_struct { + u_int8_t empty:1, jitter_ready:1, _notused:6; + u_int16_t num_values, next_index; + float *observations, last_value, jitter_total; +}; + +/* **************************************** */ + typedef struct ndpi_ptree ndpi_ptree_t; /* **************************************** */ diff --git a/src/lib/ndpi_analyze.c b/src/lib/ndpi_analyze.c index d4966791e..2f21dde78 100644 --- a/src/lib/ndpi_analyze.c +++ b/src/lib/ndpi_analyze.c @@ -1058,3 +1058,65 @@ int ndpi_hw_add_value(struct ndpi_hw_struct *hw, const u_int32_t _value, double return(1); /* We're in business: forecast is meaningful now */ } } + +/* ********************************************************************************* */ +/* ********************************************************************************* */ + +/* + Jitter calculator + + Used to determine how noisy is a signal +*/ + +int ndpi_jitter_init(struct ndpi_jitter_struct *s, u_int16_t num_learning_values) { + memset(s, 0, sizeof(struct ndpi_jitter_struct)); + + if(num_learning_values < 2) num_learning_values = 2; + + s->empty = 1, s->num_values = num_learning_values; + s->observations = (float*)ndpi_calloc(num_learning_values, sizeof(float)); + + if(s->observations) { + s->last_value = 0; + return(0); + } else + return(-1); +} + +/* ************************************* */ + +void ndpi_free_jitter(struct ndpi_jitter_struct *s) { + ndpi_free(s->observations); +} + +/* ************************************* */ + +/* + This function adds a new value and returns the computed Jitter +*/ +float ndpi_jitter_add_value(struct ndpi_jitter_struct *s, const float value) { + float val = fabsf(value - s->last_value); + + if(s->empty && (s->next_index == 0)) + ; /* Skip the first value as we are unable to calculate the difference */ + else { + s->jitter_total -= s->observations[s->next_index]; + s->observations[s->next_index] = val; + s->jitter_total += val; + } + + s->last_value = value, s->next_index = (s->next_index + 1) % s->num_values; + if(s->next_index == 0) s->jitter_ready = 1; /* We have completed one round */ + +#ifdef DEBUG_JITTER + printf("[JITTER] [value: %.3f][diff: %.3f][jitter_total: %.3f] -> %.3f\n", + value, val, s->jitter_total, + s->jitter_ready ? (s->jitter_total / s->num_values) : -1); +#endif + + if(!s->jitter_ready) + return(-1); /* Too early */ + else + return(s->jitter_total / s->num_values); +} + |