RTS API Documentation  1.10.11
Data Structures | Typedefs | Functions
switch_estimators.h File Reference
#include <switch.h>
+ Include dependency graph for switch_estimators.h:
+ This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Data Structures

struct  kalman_estimator_s
 
struct  cusum_kalman_detector_s
 

Typedefs

typedef struct kalman_estimator_s kalman_estimator_t
 
typedef struct cusum_kalman_detector_s cusum_kalman_detector_t
 

Functions

void switch_kalman_init (kalman_estimator_t *est, float Q, float R)
 
switch_bool_t switch_kalman_cusum_init (cusum_kalman_detector_t *detect_change, float epsilon, float h)
 
switch_bool_t switch_kalman_estimate (kalman_estimator_t *est, float measurement, int system_model)
 
switch_bool_t switch_kalman_cusum_detect_change (cusum_kalman_detector_t *detector, float measurement, float rtt_avg)
 
switch_bool_t switch_kalman_is_slow_link (kalman_estimator_t *est_loss, kalman_estimator_t *est_rtt)
 

Typedef Documentation

◆ cusum_kalman_detector_t

Definition at line 80 of file switch_estimators.h.

◆ kalman_estimator_t

Definition at line 79 of file switch_estimators.h.

Function Documentation

◆ switch_kalman_cusum_detect_change()

switch_bool_t switch_kalman_cusum_detect_change ( cusum_kalman_detector_t detector,
float  measurement,
float  rtt_avg 
)

Definition at line 142 of file switch_estimators.c.

References cusum_kalman_detector_s::delta, cusum_kalman_detector_s::epsilon, FALSE, cusum_kalman_detector_s::g_last, cusum_kalman_detector_s::h, if(), cusum_kalman_detector_s::last_average, cusum_kalman_detector_s::measurement_noise_e, cusum_kalman_detector_s::N, cusum_kalman_detector_s::P_last, TRUE, cusum_kalman_detector_s::val_desired_last, cusum_kalman_detector_s::variance_Re, and cusum_kalman_detector_s::variance_Rv.

Referenced by process_rtcp_report().

143 {
144  float K=0;
145  float P=0;
146  float g=0;
147  float desired_val;
148  float current_average;
149  float current_q;
150  float sample_variance_Re = 0;
151 
152  /*variance*/
153 
154  detector->N++;
155  current_average = detector->last_average + (measurement - detector->last_average)/detector->N ;
156  if (avg > current_average) {
157  current_average = avg;
158  }
159  current_q = detector-> last_q + (measurement - detector->last_average) * (measurement - current_average);
160  if (detector->N != 0)
161  sample_variance_Re = sqrt(current_q/detector->N);
162 
163  detector->variance_Re = sample_variance_Re;
164  detector->variance_Rv = sample_variance_Re;
165 
166  if (sample_variance_Re != 0) {
167  K = detector->P_last / (detector->P_last + detector->variance_Re);
168  desired_val = detector->val_desired_last + K * (measurement - detector->variance_Re);
169  P = (1 - K) * detector->P_last + detector->delta * detector->variance_Rv;
170  detector->measurement_noise_e = measurement - desired_val;
171  g = detector->g_last + detector->measurement_noise_e - detector->epsilon;
172  if (g > detector->h) {
173  detector->delta = 1;
174  g = 0;
175  } else {
176  detector->delta = 0;
177  }
178 
179  /* update last vals for calculating variance */
180  detector->last_average = current_average;
181  /* update lasts (cusum)*/
182  detector -> g_last = g;
183  detector -> P_last = P;
184  detector -> val_desired_last = desired_val;
185  }
186  if (detector->delta == 1) {
187  return TRUE;
188  }
189  return FALSE;
190 }
if((uint32_t)(unpack->cur - unpack->buf) > unpack->buflen)
#define FALSE
#define TRUE

◆ switch_kalman_cusum_init()

switch_bool_t switch_kalman_cusum_init ( cusum_kalman_detector_t detect_change,
float  epsilon,
float  h 
)

Definition at line 113 of file switch_estimators.c.

References FALSE, and TRUE.

Referenced by rtcp_stats_init().

114 {
115  cusum_kalman_detector_t *detector_change = detect_change;
116 
117 
118  if (epsilon < 0 || h < 0) {
119  return FALSE;
120  }
121 
122  detector_change -> val_estimate_last = 0;
123  detector_change -> val_desired_last = 0;
124  detector_change -> P_last = 0;
125  detector_change -> K_last = 0;
126  detector_change -> delta = 0;
127  detector_change -> measurement_noise_e = 0;
128  detector_change -> variance_Re = 0;
129  detector_change -> measurement_noise_v = 0;
130  detector_change -> variance_Rv = 0;
131  detector_change -> g_last = 0;
132  /*per system model*/
133  detector_change -> epsilon = epsilon;
134  detector_change -> h = h;
135  /*variance*/
136  detector_change -> last_average = 0;
137  detector_change -> last_q = 0;
138  detector_change -> N = 0;
139  return TRUE;
140 }
#define FALSE
#define TRUE

◆ switch_kalman_estimate()

switch_bool_t switch_kalman_estimate ( kalman_estimator_t est,
float  measurement,
int  system_model 
)

Definition at line 195 of file switch_estimators.c.

References EST_JITTER, EST_LOSS, EST_RTT, kalman_estimator_s::K, KALMAN_SYSTEM_MODELS, kalman_estimator_s::P, kalman_estimator_s::P_last, kalman_estimator_s::Q, kalman_estimator_s::R, SWITCH_FALSE, SWITCH_TRUE, kalman_estimator_s::val_estimate_last, and kalman_estimator_s::val_measured.

Referenced by process_rtcp_report().

196 {
197  /*system model can be about: loss, jitter, rtt*/
198  float val_estimate;
199  float val_temp_est = est->val_estimate_last;
200  float P_temp = est->P_last + est->Q;
201 
202  if (system_model >= KALMAN_SYSTEM_MODELS) {
203  return SWITCH_FALSE ;
204  }
205 
206  /*sanitize input a little bit, just in case */
207  if (system_model == EST_LOSS ) {
208  if ((measurement > 100) || (measurement < 0)) {
209  return SWITCH_FALSE ;
210  }
211  }
212 
213  if (system_model == EST_JITTER) {
214  if ((measurement > 10000) || (measurement < 0)) {
215  return SWITCH_FALSE;
216  }
217  }
218 
219  if (system_model == EST_RTT) {
220  if ((measurement > 2 ) || (measurement < 0)) {
221  return SWITCH_FALSE;
222  }
223  }
224 
225  /* calculate the Kalman gain */
226  est->K = P_temp * (1.0/(P_temp + est->R));
227  /* real life measurement */
228  est->val_measured = measurement ;
229  val_estimate = val_temp_est + est->K * (est->val_measured - val_temp_est);
230  est->P = (1 - est->K) * P_temp;
231  /*update lasts*/
232  est->P_last = est->P;
233  /* save the estimated value (future) */
234  est->val_estimate_last = val_estimate;
235  return SWITCH_TRUE;
236 }
#define KALMAN_SYSTEM_MODELS
#define EST_LOSS
#define EST_RTT
#define EST_JITTER

◆ switch_kalman_init()

void switch_kalman_init ( kalman_estimator_t est,
float  Q,
float  R 
)

Definition at line 70 of file switch_estimators.c.

Referenced by rtcp_stats_init().

71 {
72  est -> val_estimate_last = 0 ;
73  est -> P_last = 0;
74  est -> Q = Q; /*accuracy of system model */ /* SYSTEM MODEL: TO BE DEDUCTED */
75  est -> R = R; /*accuracy of measurement*/ /* SYSTEM MODEL: TO BE DEDUCTED */
76  est -> K = 0;
77  est -> val_estimate = 0 ;
78  est -> val_measured = 0 ; // [0-100 %] or [0-5000] or [0-2sec]
79 }

◆ switch_kalman_is_slow_link()

switch_bool_t switch_kalman_is_slow_link ( kalman_estimator_t est_loss,
kalman_estimator_t est_rtt 
)

Definition at line 238 of file switch_estimators.c.

References SWITCH_FALSE, SWITCH_TRUE, and kalman_estimator_s::val_estimate_last.

Referenced by process_rtcp_report().

239 {
240  float thresh_packet_loss = 5; /* % */
241  float thresh_rtt = 0.8 ; /*seconds*/
242 
243  if ((est_loss->val_estimate_last > thresh_packet_loss) &&
244  (est_rtt->val_estimate_last > thresh_rtt )) {
245  return SWITCH_TRUE;
246  }
247 
248  return SWITCH_FALSE;
249 }