2 // After looking at how pulseaudio is doing things,
3 // i have came to the conclusion, that forgetting
4 // about statistics is a bad idea ;)
6 // i am loosely basing this on pulsecore/time-smoother.c
8 #include "time_smoother.h"
12 time_smoother
*time_smoother_new( int history_size
)
14 time_smoother
*retval
= malloc( sizeof( time_smoother
) );
18 retval
->x
= malloc( sizeof(jack_nframes_t
) * history_size
);
25 retval
->y
= malloc( sizeof(jack_nframes_t
) * history_size
);
32 retval
->history_size
= history_size
;
33 retval
->num_valid
= 0;
37 void time_smoother_free( time_smoother
*ts
)
44 // put a time measurement into the smoother.
45 // assume monotonically increasing x.
47 void time_smoother_put ( time_smoother
*ts
, jack_nframes_t x
, jack_nframes_t y
)
51 jack_nframes_t oldest_diff
;
53 if( ts
->num_valid
< ts
->history_size
) {
54 ts
->x
[ts
->num_valid
] = x
;
55 ts
->y
[ts
->num_valid
] = y
;
60 // Its full. find oldest value and replace.
63 for( i
=0; i
<ts
->history_size
; i
++ ) {
64 if( (x
- ts
->x
[i
]) > oldest_diff
) {
65 oldest_diff
= (x
- ts
->x
[i
]);
69 assert( oldest_index
!= -1 );
71 ts
->x
[oldest_index
] = x
;
72 ts
->y
[oldest_index
] = y
;
75 // return a and b for the linear regression line,
77 void time_smoother_get_linear_params( time_smoother
*ts
,
80 jack_nframes_t history
,
81 double *a
, double *b
)
84 jack_nframes_t sum_x
= 0;
85 jack_nframes_t sum_y
= 0;
87 double mean_x
, mean_y
;
92 for( i
=0; i
<ts
->num_valid
; i
++ ) {
93 if( (now_x
- ts
->x
[i
]) < history
) {
94 sum_x
+= (now_x
- ts
->x
[i
]);
95 sum_y
+= (now_y
- ts
->y
[i
]);
100 // Give some valid values if we dont have
102 if( num_values
< 10 ) {
109 mean_x
= (double) sum_x
/ (double) num_values
;
110 mean_y
= (double) sum_y
/ (double) num_values
;
111 //printf( "mean: %f %f\n", (float) mean_x, (float) mean_y );
113 for( i
=0; i
<ts
->num_valid
; i
++ ) {
114 if( (now_x
- ts
->x
[i
]) < history
) {
115 sxx
+= ((double)(now_x
-ts
->x
[i
])-mean_x
) * ((double)(now_x
-ts
->x
[i
])-mean_x
);
116 sxy
+= ((double)(now_x
-ts
->x
[i
])-mean_x
) * ((double)(now_y
-ts
->y
[i
])-mean_y
);
121 val_a
= mean_y
- val_b
*mean_x
;