From 1331e0aec90f5a648371eee1a3a1117669485a11 Mon Sep 17 00:00:00 2001 From: Luca Deri Date: Tue, 9 Feb 2021 15:56:03 +0100 Subject: Extended the API to calculate jitter - ndpi_jitter_init() - ndpi_jitter_free() - ndpi_jitter_add_value() --- example/ndpiReader.c | 20 +++++++++++++++ src/include/ndpi_api.h.in | 6 +++++ src/include/ndpi_typedefs.h | 8 ++++++ src/lib/ndpi_analyze.c | 62 +++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 96 insertions(+) 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; iempty = 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); +} + -- cgit v1.2.3