diff options
author | Luca <deri@ntop.org> | 2019-08-28 14:02:39 +0200 |
---|---|---|
committer | Luca <deri@ntop.org> | 2019-08-28 14:02:39 +0200 |
commit | e4e40e3c70e2cd49fd537a526fa70805c8c391c5 (patch) | |
tree | 98c605df4ac69cdef449d4eec700f5b74f621feb /src | |
parent | 84aeee49bda71f71877ca114a1946a6d359be5d5 (diff) |
Added entropy, average, stddev, variance, bytes ratio calculation
Diffstat (limited to 'src')
-rw-r--r-- | src/include/ndpi_api.h | 7 | ||||
-rw-r--r-- | src/include/ndpi_typedefs.h | 5 | ||||
-rw-r--r-- | src/lib/ndpi_analyze.c | 35 |
3 files changed, 43 insertions, 4 deletions
diff --git a/src/include/ndpi_api.h b/src/include/ndpi_api.h index 03f21d4cd..c6d17a4b5 100644 --- a/src/include/ndpi_api.h +++ b/src/include/ndpi_api.h @@ -899,8 +899,11 @@ extern "C" { float ndpi_data_average(struct ndpi_analyze_struct *s); float ndpi_data_window_average(struct ndpi_analyze_struct *s); - float ndpi_entropy(struct ndpi_analyze_struct *s); - + float ndpi_data_entropy(struct ndpi_analyze_struct *s); + float ndpi_data_variance(struct ndpi_analyze_struct *s); + float ndpi_data_stddev(struct ndpi_analyze_struct *s); + float ndpi_data_ratio(u_int32_t sent, u_int32_t rcvd); + void ndpi_data_print_window_values(struct ndpi_analyze_struct *s); /* debug */ #ifdef __cplusplus } diff --git a/src/include/ndpi_typedefs.h b/src/include/ndpi_typedefs.h index 2aacf847a..0db1ccf91 100644 --- a/src/include/ndpi_typedefs.h +++ b/src/include/ndpi_typedefs.h @@ -1369,6 +1369,11 @@ struct ndpi_analyze_struct { u_int32_t *values; u_int32_t sum_total, num_data_entries, next_value_insert_index; u_int16_t num_values_array_len /* lenght of the values array */; + + struct { + /* https://www.johndcook.com/blog/standard_deviation/ */ + float mu, q; + } stddev; }; #define DEFAULT_SERIES_LEN 64 diff --git a/src/lib/ndpi_analyze.c b/src/lib/ndpi_analyze.c index 502c2d858..ce3168165 100644 --- a/src/lib/ndpi_analyze.c +++ b/src/lib/ndpi_analyze.c @@ -72,20 +72,42 @@ void ndpi_free_data_analysis(struct ndpi_analyze_struct *d) { Add a new point to analyze */ void ndpi_data_add_value(struct ndpi_analyze_struct *s, const u_int32_t value) { + float tmp_mu; + s->sum_total += value, s->num_data_entries++, s->values[s->next_value_insert_index] = value; + if(++s->next_value_insert_index == s->num_values_array_len) s->next_value_insert_index = 0; + + /* Update stddev */ + tmp_mu = s->stddev.mu; + s->stddev.mu = ((s->stddev.mu * (s->num_data_entries - 1)) + value) / s->num_data_entries; + s->stddev.q = s->stddev.q + (value - tmp_mu)*(value - s->stddev.mu); } /* ********************************************************************************* */ -/* Compute the average on all value */ +/* Compute the average on all values */ float ndpi_data_average(struct ndpi_analyze_struct *s) { return((float)s->sum_total / (float)s->num_data_entries); } /* ********************************************************************************* */ +/* Compute the variance on all values */ +float ndpi_data_variance(struct ndpi_analyze_struct *s) { + return(s->num_data_entries ? (s->stddev.q / s->num_data_entries) : 0); +} + +/* ********************************************************************************* */ + +/* Compute the standard deviation on all values */ +float ndpi_data_stddev(struct ndpi_analyze_struct *s) { + return(sqrt(ndpi_data_variance(s))); +} + +/* ********************************************************************************* */ + /* Compute the average only on the sliding window */ float ndpi_data_window_average(struct ndpi_analyze_struct *s) { float sum = 0.0; @@ -102,7 +124,7 @@ float ndpi_data_window_average(struct ndpi_analyze_struct *s) { /* Compute entropy on the last sliding window values */ -float ndpi_entropy(struct ndpi_analyze_struct *s) { +float ndpi_data_entropy(struct ndpi_analyze_struct *s) { int i; float sum = 0.0, total = 0.0; @@ -129,3 +151,12 @@ void ndpi_data_print_window_values(struct ndpi_analyze_struct *s) { printf("\n"); } + +/* ********************************************************************************* */ + +float ndpi_data_ratio(u_int32_t sent, u_int32_t rcvd) { + int64_t s = (int64_t)sent + (int64_t)rcvd; + int64_t d = (int64_t)sent - (int64_t)rcvd; + + return((s == 0) ? 0 : ((float)d)/((float)s)); +} |