RSI enhancements
This commit is contained in:
@@ -1370,7 +1370,7 @@ extern "C" {
|
||||
/* Data analysis */
|
||||
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_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_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_analyze_struct *gains, *losses;
|
||||
struct ndpi_analyze_struct gains, losses;
|
||||
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);
|
||||
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) {
|
||||
s->gains = ndpi_alloc_data_analysis(num_learning_values);
|
||||
s->losses = ndpi_alloc_data_analysis(num_learning_values);
|
||||
ndpi_init_data_analysis(&s->gains, num_learning_values);
|
||||
ndpi_init_data_analysis(&s->losses, num_learning_values);
|
||||
s->last_value = 0;
|
||||
}
|
||||
|
||||
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) {
|
||||
if(s->gains->num_data_entries == 0)
|
||||
ndpi_data_add_value(s->gains, 0), ndpi_data_add_value(s->losses, 0);
|
||||
if(s->gains.num_data_entries == 0)
|
||||
ndpi_data_add_value(&s->gains, 0), ndpi_data_add_value(&s->losses, 0);
|
||||
else {
|
||||
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
|
||||
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;
|
||||
|
||||
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);
|
||||
if(s->gains.num_data_entries >= s->gains.num_values_array_len) {
|
||||
float relative_strength = ndpi_data_window_average(&s->gains) / ndpi_data_window_average(&s->losses);
|
||||
|
||||
/* printf("RSI: %f\n", relative_strength); */
|
||||
return(100. - (100. / (1. + relative_strength)));
|
||||
|
||||
Reference in New Issue
Block a user