diff options
author | Luca Deri <deri@ntop.org> | 2020-07-15 18:13:06 +0200 |
---|---|---|
committer | Luca Deri <deri@ntop.org> | 2020-07-15 18:13:06 +0200 |
commit | 46e97d5da394aed103b6e41336699c7209fb03b0 (patch) | |
tree | 615a3d5eef58c0427386eda4f6366295570bd50b /src | |
parent | 9f3e3e8456f204edfc2d626b6473bb2278a1b9cb (diff) |
Added ndpi_data_window_variance() and ndpi_data_window_stddev() API calls
Diffstat (limited to 'src')
-rw-r--r-- | src/include/ndpi_api.h.in | 6 | ||||
-rw-r--r-- | src/lib/ndpi_analyze.c | 26 |
2 files changed, 31 insertions, 1 deletions
diff --git a/src/include/ndpi_api.h.in b/src/include/ndpi_api.h.in index c272d66c8..5d77d6221 100644 --- a/src/include/ndpi_api.h.in +++ b/src/include/ndpi_api.h.in @@ -1030,9 +1030,13 @@ extern "C" { void ndpi_free_data_analysis(struct ndpi_analyze_struct *d); void ndpi_data_add_value(struct ndpi_analyze_struct *s, const u_int32_t value); - float ndpi_data_average(struct ndpi_analyze_struct *s); + /* Sliding-window only */ float ndpi_data_window_average(struct ndpi_analyze_struct *s); + float ndpi_data_window_variance(struct ndpi_analyze_struct *s); + float ndpi_data_window_stddev(struct ndpi_analyze_struct *s); + /* All data */ + float ndpi_data_average(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); diff --git a/src/lib/ndpi_analyze.c b/src/lib/ndpi_analyze.c index 4b4fbf72b..7d4aa5f47 100644 --- a/src/lib/ndpi_analyze.c +++ b/src/lib/ndpi_analyze.c @@ -157,6 +157,32 @@ float ndpi_data_window_average(struct ndpi_analyze_struct *s) { /* ********************************************************************************* */ +/* Compute the variance only on the sliding window */ +float ndpi_data_window_variance(struct ndpi_analyze_struct *s) { + if(s->num_values_array_len) { + float sum = 0.0, avg = ndpi_data_window_average(s); + u_int16_t i, n = ndpi_min(s->num_data_entries, s->num_values_array_len); + + if(n == 0) + return(0); + + for(i=0; i<n; i++) + sum += pow(s->values[i]-avg, 2); + + return((float)sum / (float)n); + } else + return(0); +} + +/* ********************************************************************************* */ + +/* Compute the variance only on the sliding window */ +float ndpi_data_window_stddev(struct ndpi_analyze_struct *s) { + return(sqrt(ndpi_data_window_variance(s))); +} + + /* ********************************************************************************* */ + /* Compute entropy on the last sliding window values */ |