Codebase list httping / 3aa6b8e
kalman filter in gui mode; value and graph folkert 11 years ago
5 changed file(s) with 146 addition(s) and 61 deletion(s). Raw diff Collapse all Expand all
5353 ARCHIVE=/bin/tar cf -
5454 COMPRESS=/bin/gzip -9
5555
56 OBJS=gen.o http.o io.o str.o error.o utils.o main.o tcp.o res.o socks5.o
56 OBJS=gen.o http.o io.o str.o error.o utils.o main.o tcp.o res.o socks5.o kalman.o
5757
5858 MANS=httping.1
5959
0 /* #define _TEST */
1
2 #include <math.h>
3 #ifdef _TEST
4 #include <stdio.h>
5 #include <stdlib.h>
6 #endif
7
8 double x_est_last = 0.0, P_last = 0.0, Q = 0.0, R = 0.0, K = 0.0, P = 0.0, P_temp = 0.0, x_temp_est = 0.0, x_est = 0.0, z_measured = 0.0, z_real = 0.0, sum_error_kalman = 0.0, sum_error_measure = 0.0;
9 char first = 1;
10
11 void kalman_init(double ideal_value)
12 {
13 /* initial values for the kalman filter */
14 x_est_last = 0;
15 P_last = 0;
16 /* the noise in the system (FIXME?) */
17 Q = 0.022;
18 R = 0.617;
19 z_real = ideal_value; /* 0.5; // the ideal value we wish to measure */
20
21 first = 1;
22 }
23
24 double kalman_do(double z_measured)
25 {
26 /* initialize with a measurement */
27 if (first)
28 {
29 first = 0;
30 x_est_last = z_measured;
31 }
32
33 /* do a prediction */
34 x_temp_est = x_est_last;
35 P_temp = P_last + Q;
36 /* calculate the Kalman gain */
37 K = P_temp * (1.0/(P_temp + R));
38 /* measure */
39 /*z_measured = z_real + frand()*0.09; //the real measurement plus noise*/
40 /* correct */
41 x_est = x_temp_est + K * (z_measured - x_temp_est);
42 P = (1- K) * P_temp;
43 /* we have our new system */
44
45 #ifdef _TEST
46 printf("Ideal position: %6.3f \n",z_real);
47 printf("Mesaured position: %6.3f [diff:%.3f]\n",z_measured,fabs(z_real-z_measured));
48 printf("Kalman position: %6.3f [diff:%.3f]\n",x_est,fabs(z_real - x_est));
49 #endif
50
51 sum_error_kalman += fabs(z_real - x_est);
52 sum_error_measure += fabs(z_real-z_measured);
53
54 /* update our last's */
55 P_last = P;
56 x_est_last = x_est;
57
58 #ifdef _TEST
59 printf("Total error if using raw measured: %f\n",sum_error_measure);
60 printf("Total error if using kalman filter: %f\n",sum_error_kalman);
61 printf("Reduction in error: %d%% \n",100-(int)((sum_error_kalman/sum_error_measure)*100));
62 #endif
63
64 return x_est;
65 }
66
67 #ifdef _TEST
68 int main(int argc, char *argv[])
69 {
70 kalman_init(0.0);
71
72 for(int loop=0; loop<25; loop++)
73 {
74 double v = drand48();
75 printf("%d] %f %f\n", loop + 1, v, kalman_do(v));
76 }
77
78 return 0;
79 }
80 #endif
0 // taken (and adapted) from http://www.dzone.com/snippets/simple-kalman-filter-c
1
2 void kalman_init(double ideal_value);
3 double kalman_do(double value);
+59
-59
nc.c less more
00 /* $Revision$ */
1 #include <math.h>
12 #include <poll.h>
23 #include <stdarg.h>
34 #include <stdio.h>
1011
1112 #include "error.h"
1213 #include "gen.h"
14 #include "kalman.h"
1315 #ifdef FW
1416 #include "fft.h"
1517 #endif
2628
2729 double graph_limit = 99999999.9;
2830 double hz = 1.0;
31
32 double graph_avg = 0.0, graph_sd = 0.0, graph_min = 999999999999.0, graph_max = -99999999999999999.0;
33 int graph_n = 0;
2934
3035 double *history = NULL, *history_temp = NULL, *history_fft = NULL;
3136 char *history_set = NULL;
222227 init_pair(C_GREEN, COLOR_GREEN, COLOR_BLACK);
223228 init_pair(C_RED, COLOR_RED, COLOR_BLACK);
224229
230 kalman_init(0.0);
231
225232 recreate_terminal();
226233 }
227234
456463 }
457464 #endif
458465
459 void draw_graph(void)
466 void draw_graph(double val)
460467 {
461468 int index = 0, n = min(max_x, history_n);
462 double sd = 0.0, avg = 0.0, mi = 0.0, ma = 0.0;
463 stats_t h_stats;
464
465 init_statst(&h_stats);
469 double avg = 0, sd = 0;
470 double mi = 0.0, ma = 0.0, diff = 0.0;
471
472 graph_min = min(val, graph_min);
473 graph_max = max(val, graph_max);
474
475 graph_avg += val;
476 graph_sd += val * val;
477 graph_n++;
478
479 avg = graph_avg / (double)graph_n;
480 sd = sqrt((graph_sd / (double)graph_n) - pow(avg, 2.0));
481
482 mi = max(graph_min, max(0.0, avg - sd));
483 ma = min(graph_max, avg + sd);
484 diff = ma - mi;
485
486 if (diff == 0.0)
487 diff = 1.0;
488
489 wattron(w_line1, A_REVERSE);
490 mvwprintw(w_line1, 0, 0, "graph range: %7.2fms - %7.2fms ", mi, ma);
491 wattroff(w_line1, A_REVERSE);
492 wnoutrefresh(w_line1);
493
494 /* fprintf(stderr, "%d| %f %f %f %f\n", h_stats.n, mi, avg, ma, sd); */
466495
467496 for(index=0; index<n; index++)
468497 {
469 if (history_set[index])
498 char overflow = 0, limitter = 0;
499 double val = 0, height = 0;
500 int i_h = 0;
501
502 if (history[index] < graph_limit)
503 val = history[index];
504 else
470505 {
471 double val = history[index] < graph_limit ? history[index] : graph_limit;
472
473 update_statst(&h_stats, val);
506 val = graph_limit;
507 limitter = 1;
474508 }
475 }
476
477 if (h_stats.n)
478 {
479 double diff = 0.0;
480
481 avg = h_stats.avg / (double)h_stats.n;
482 sd = calc_sd(&h_stats);
483
484 mi = max(h_stats.min, max(0.0, avg - sd));
485 ma = min(h_stats.max, avg + sd);
486 diff = ma - mi;
487
488 if (diff == 0.0)
489 diff = 1.0;
490
491 wattron(w_line1, A_REVERSE);
492 mvwprintw(w_line1, 0, 0, "graph range: %7.2fms - %7.2fms ", mi, ma);
493 wattroff(w_line1, A_REVERSE);
494 wnoutrefresh(w_line1);
495
496 /* fprintf(stderr, "%d| %f %f %f %f\n", h_stats.n, mi, avg, ma, sd); */
497
498 for(index=0; index<h_stats.n; index++)
509
510 height = (val - mi) / diff;
511
512 if (height > 1.0)
499513 {
500 char overflow = 0, limitter = 0;
501 double val = 0, height = 0;
502 int i_h = 0;
503
504 if (history[index] < graph_limit)
505 val = history[index];
506 else
507 {
508 val = graph_limit;
509 limitter = 1;
510 }
511
512 height = (val - mi) / (ma - mi);
513
514 if (height > 1.0)
515 {
516 height = 1.0;
517 overflow = 1;
518 }
519
520 i_h = (int)(height * stats_h);
521 /* fprintf(stderr, "%d %f %f %d %d\n", index, history[index], height, i_h, overflow); */
522
523 draw_column(w_stats, max_x - (1 + index), i_h, overflow, limitter);
514 height = 1.0;
515 overflow = 1;
524516 }
517
518 i_h = (int)(height * stats_h);
519 /* fprintf(stderr, "%d %f %f %d %d\n", index, history[index], height, i_h, overflow); */
520
521 draw_column(w_stats, max_x - (1 + index), i_h, overflow, limitter);
525522 }
526523 }
527524
528525 void update_stats(stats_t *resolve, stats_t *connect, stats_t *request, stats_t *total, stats_t *ssl_setup, int n_ok, int n_fail, const char *last_connect_str, const char *fp, char use_tfo, char dg, char use_ssl)
529526 {
527 double k = 0.0;
530528 char force_redraw = 0;
531529 struct pollfd p = { 0, POLLIN, 0 };
532530
555553 mvwprintw(w_stats, 4, 0, "total : %6.2f %6.2f %6.2f %6.2f %6.2f",
556554 total -> cur, total -> min, total -> avg / (double)total -> n, total -> max, calc_sd(total));
557555
558 mvwprintw(w_stats, 5, 0, "ok: %4d, fail: %4d%s, scc: %.3f", n_ok, n_fail, use_tfo ? ", with TFO" : "", get_cur_scc());
556 k = kalman_do(total -> cur);
557
558 mvwprintw(w_stats, 5, 0, "ok: %4d, fail: %4d%s, scc: %.3f, kalman: %.3f", n_ok, n_fail, use_tfo ? ", with TFO" : "", get_cur_scc(), k);
559559
560560 buflen = snprintf(buffer, sizeof buffer, "http result code: %s, SSL fingerprint: %s", last_connect_str, fp ? fp : "n/a");
561561
584584
585585 if (dg)
586586 {
587 draw_graph();
587 draw_graph(k);
588588 #ifdef FW
589589 draw_fft();
590590 #endif
0 VERSION=2.2.1
0 VERSION=2.3