aboutsummaryrefslogtreecommitdiff
path: root/src/lib/ndpi_analyze.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/lib/ndpi_analyze.c')
-rw-r--r--src/lib/ndpi_analyze.c35
1 files changed, 33 insertions, 2 deletions
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));
+}