RSI enhancements
This commit is contained in:
@@ -3587,7 +3587,7 @@ void analyzeUnitTest() {
|
|||||||
ndpi_data_min(s), ndpi_data_max(s));
|
ndpi_data_min(s), ndpi_data_max(s));
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
ndpi_free_data_analysis(s);
|
ndpi_free_data_analysis(s, 1);
|
||||||
|
|
||||||
#ifdef RUN_DATA_ANALYSIS_THEN_QUIT
|
#ifdef RUN_DATA_ANALYSIS_THEN_QUIT
|
||||||
exit(0);
|
exit(0);
|
||||||
@@ -3675,7 +3675,7 @@ void analysisUnitTest() {
|
|||||||
printf("Min/Max: %u/%u\n", ndpi_data_min(s), ndpi_data_max(s));
|
printf("Min/Max: %u/%u\n", ndpi_data_min(s), ndpi_data_max(s));
|
||||||
}
|
}
|
||||||
|
|
||||||
ndpi_free_data_analysis(s);
|
ndpi_free_data_analysis(s, 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* *********************************************** */
|
/* *********************************************** */
|
||||||
@@ -3704,7 +3704,7 @@ void rsiUnitTest() {
|
|||||||
for(i=0; i<n; i++) {
|
for(i=0; i<n; i++) {
|
||||||
float rsi = ndpi_rsi_add_value(&s, v[i]);
|
float rsi = ndpi_rsi_add_value(&s, v[i]);
|
||||||
|
|
||||||
#if 0
|
#if DEBUG
|
||||||
printf("%2d) RSI = %f\n", i, rsi);
|
printf("%2d) RSI = %f\n", i, rsi);
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -493,13 +493,13 @@ static void ndpi_free_flow_tls_data(struct ndpi_flow_info *flow) {
|
|||||||
/* ***************************************************** */
|
/* ***************************************************** */
|
||||||
|
|
||||||
static void ndpi_free_flow_data_analysis(struct ndpi_flow_info *flow) {
|
static void ndpi_free_flow_data_analysis(struct ndpi_flow_info *flow) {
|
||||||
if(flow->iat_c_to_s) ndpi_free_data_analysis(flow->iat_c_to_s);
|
if(flow->iat_c_to_s) ndpi_free_data_analysis(flow->iat_c_to_s, 1);
|
||||||
if(flow->iat_s_to_c) ndpi_free_data_analysis(flow->iat_s_to_c);
|
if(flow->iat_s_to_c) ndpi_free_data_analysis(flow->iat_s_to_c, 1);
|
||||||
|
|
||||||
if(flow->pktlen_c_to_s) ndpi_free_data_analysis(flow->pktlen_c_to_s);
|
if(flow->pktlen_c_to_s) ndpi_free_data_analysis(flow->pktlen_c_to_s, 1);
|
||||||
if(flow->pktlen_s_to_c) ndpi_free_data_analysis(flow->pktlen_s_to_c);
|
if(flow->pktlen_s_to_c) ndpi_free_data_analysis(flow->pktlen_s_to_c, 1);
|
||||||
|
|
||||||
if(flow->iat_flow) ndpi_free_data_analysis(flow->iat_flow);
|
if(flow->iat_flow) ndpi_free_data_analysis(flow->iat_flow, 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* ***************************************************** */
|
/* ***************************************************** */
|
||||||
|
|||||||
@@ -1370,7 +1370,7 @@ extern "C" {
|
|||||||
/* Data analysis */
|
/* Data analysis */
|
||||||
struct ndpi_analyze_struct* ndpi_alloc_data_analysis(u_int16_t _max_series_len);
|
struct ndpi_analyze_struct* ndpi_alloc_data_analysis(u_int16_t _max_series_len);
|
||||||
void ndpi_init_data_analysis(struct ndpi_analyze_struct *s, u_int16_t _max_series_len);
|
void ndpi_init_data_analysis(struct ndpi_analyze_struct *s, u_int16_t _max_series_len);
|
||||||
void ndpi_free_data_analysis(struct ndpi_analyze_struct *d);
|
void ndpi_free_data_analysis(struct ndpi_analyze_struct *d, u_int8_t free_pointer);
|
||||||
void ndpi_reset_data_analysis(struct ndpi_analyze_struct *d);
|
void ndpi_reset_data_analysis(struct ndpi_analyze_struct *d);
|
||||||
void ndpi_data_add_value(struct ndpi_analyze_struct *s, const u_int32_t value);
|
void ndpi_data_add_value(struct ndpi_analyze_struct *s, const u_int32_t value);
|
||||||
|
|
||||||
|
|||||||
@@ -1545,7 +1545,7 @@ struct ndpi_analyze_struct {
|
|||||||
/* **************************************** */
|
/* **************************************** */
|
||||||
|
|
||||||
struct ndpi_rsi_struct {
|
struct ndpi_rsi_struct {
|
||||||
struct ndpi_analyze_struct *gains, *losses;
|
struct ndpi_analyze_struct gains, losses;
|
||||||
u_int32_t last_value;
|
u_int32_t last_value;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
+15
-11
@@ -68,9 +68,9 @@ struct ndpi_analyze_struct* ndpi_alloc_data_analysis(u_int16_t _max_series_len)
|
|||||||
|
|
||||||
/* ********************************************************************************* */
|
/* ********************************************************************************* */
|
||||||
|
|
||||||
void ndpi_free_data_analysis(struct ndpi_analyze_struct *d) {
|
void ndpi_free_data_analysis(struct ndpi_analyze_struct *d, u_int8_t free_pointer) {
|
||||||
if(d->values) ndpi_free(d->values);
|
if(d->values) ndpi_free(d->values);
|
||||||
ndpi_free(d);
|
if(free_pointer) ndpi_free(d);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* ********************************************************************************* */
|
/* ********************************************************************************* */
|
||||||
@@ -827,29 +827,33 @@ int ndpi_cluster_bins(struct ndpi_bin *bins, u_int16_t num_bins,
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
void ndpi_init_rsi(struct ndpi_rsi_struct *s, u_int16_t num_learning_values) {
|
void ndpi_init_rsi(struct ndpi_rsi_struct *s, u_int16_t num_learning_values) {
|
||||||
s->gains = ndpi_alloc_data_analysis(num_learning_values);
|
ndpi_init_data_analysis(&s->gains, num_learning_values);
|
||||||
s->losses = ndpi_alloc_data_analysis(num_learning_values);
|
ndpi_init_data_analysis(&s->losses, num_learning_values);
|
||||||
s->last_value = 0;
|
s->last_value = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
void ndpi_free_rsi(struct ndpi_rsi_struct *s) {
|
void ndpi_free_rsi(struct ndpi_rsi_struct *s) {
|
||||||
ndpi_free_data_analysis(s->gains), ndpi_free_data_analysis(s->losses);
|
ndpi_free_data_analysis(&s->gains, 0), ndpi_free_data_analysis(&s->losses, 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
This function adds a new value and returns the computed RSI, or -1
|
||||||
|
if there are too many points (< num_learning_values)
|
||||||
|
*/
|
||||||
float ndpi_rsi_add_value(struct ndpi_rsi_struct *s, const u_int32_t value) {
|
float ndpi_rsi_add_value(struct ndpi_rsi_struct *s, const u_int32_t value) {
|
||||||
if(s->gains->num_data_entries == 0)
|
if(s->gains.num_data_entries == 0)
|
||||||
ndpi_data_add_value(s->gains, 0), ndpi_data_add_value(s->losses, 0);
|
ndpi_data_add_value(&s->gains, 0), ndpi_data_add_value(&s->losses, 0);
|
||||||
else {
|
else {
|
||||||
if(value > s->last_value)
|
if(value > s->last_value)
|
||||||
ndpi_data_add_value(s->gains, value - s->last_value), ndpi_data_add_value(s->losses, 0);
|
ndpi_data_add_value(&s->gains, value - s->last_value), ndpi_data_add_value(&s->losses, 0);
|
||||||
else
|
else
|
||||||
ndpi_data_add_value(s->losses, s->last_value - value), ndpi_data_add_value(s->gains, 0);
|
ndpi_data_add_value(&s->losses, s->last_value - value), ndpi_data_add_value(&s->gains, 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
s->last_value = value;
|
s->last_value = value;
|
||||||
|
|
||||||
if(s->gains->num_data_entries >= s->gains->num_values_array_len) {
|
if(s->gains.num_data_entries >= s->gains.num_values_array_len) {
|
||||||
float relative_strength = ndpi_data_average(s->gains) / ndpi_data_average(s->losses);
|
float relative_strength = ndpi_data_window_average(&s->gains) / ndpi_data_window_average(&s->losses);
|
||||||
|
|
||||||
/* printf("RSI: %f\n", relative_strength); */
|
/* printf("RSI: %f\n", relative_strength); */
|
||||||
return(100. - (100. / (1. + relative_strength)));
|
return(100. - (100. / (1. + relative_strength)));
|
||||||
|
|||||||
Reference in New Issue
Block a user