1 #include <twr_data_stream.h>
3 static int _twr_data_stream_compare_float(
const void * a,
const void * b);
4 static int _twr_data_stream_compare_int(
const void * a,
const void * b);
8 memset(
self, 0,
sizeof(*
self));
9 self->_buffer = buffer;
11 self->_feed_head =
self->_buffer->number_of_samples - 1;
12 self->_min_number_of_samples = min_number_of_samples;
23 if (++self->_feed_head == self->_buffer->number_of_samples)
28 switch (self->_buffer->type)
30 case TWR_DATA_STREAM_TYPE_FLOAT:
32 if (isnan(*(
float *) data) || isinf(*(
float *) data))
39 *((
float *) self->_buffer->feed + self->_feed_head) = *(
float *) data;
43 case TWR_DATA_STREAM_TYPE_INT:
45 *((
int *) self->_buffer->feed + self->_feed_head) = *(
int *) data;
61 self->_feed_head =
self->_buffer->number_of_samples - 1;
66 return self->_counter;
71 return self->_counter >
self->_buffer->number_of_samples ?
self->_buffer->number_of_samples :
self->_counter;
76 return self->_buffer->type;
81 return self->_buffer->number_of_samples;
86 if (self->_counter < self->_min_number_of_samples)
91 int length =
self->_counter >
self->_buffer->number_of_samples ?
self->_buffer->number_of_samples :
self->_counter;
93 switch (self->_buffer->type)
95 case TWR_DATA_STREAM_TYPE_FLOAT:
98 float *buffer = (
float *) self->_buffer->feed;
101 for (
int i = 0; i < length; i ++)
106 *(
float *) result = sum / length;
109 case TWR_DATA_STREAM_TYPE_INT:
112 int *buffer = (
int *) self->_buffer->feed;
114 for (
int i = 0; i < length; i ++)
119 *(
int *) result = sum / length;
133 if (self->_counter < self->_min_number_of_samples)
138 int length =
self->_counter >
self->_buffer->number_of_samples ?
self->_buffer->number_of_samples :
self->_counter;
140 switch (self->_buffer->type)
142 case TWR_DATA_STREAM_TYPE_FLOAT:
144 memcpy(self->_buffer->sort, self->_buffer->feed, length *
sizeof(
float));
145 qsort(self->_buffer->sort, length,
sizeof(
float), _twr_data_stream_compare_float);
147 float *buffer = (
float *) self->_buffer->sort;
151 *(
float *) result = (buffer[(length - 2) / 2] + buffer[length / 2]) / 2;
155 *(
float *) result = buffer[(length - 1) / 2];
159 case TWR_DATA_STREAM_TYPE_INT:
161 memcpy(self->_buffer->sort, self->_buffer->feed, length *
sizeof(
int));
162 qsort(self->_buffer->sort, length,
sizeof(
int), _twr_data_stream_compare_int);
164 int *buffer = (
int *) self->_buffer->sort;
168 *(
int *) result = (buffer[(length - 2) / 2] + buffer[length / 2]) / 2;
172 *(
int *) result = buffer[(length - 1) / 2];
187 if (self->_counter == 0)
192 int position =
self->_counter <
self->_buffer->number_of_samples ? 0 :
self->_feed_head + 1;
194 if (position == self->_buffer->number_of_samples)
199 switch (self->_buffer->type)
201 case TWR_DATA_STREAM_TYPE_FLOAT:
203 *(
float *) result = *((
float *)
self->_buffer->feed + position);
206 case TWR_DATA_STREAM_TYPE_INT:
208 *(
int *) result = *((
int *)
self->_buffer->feed + position);
222 if (self->_counter == 0)
227 switch (self->_buffer->type)
229 case TWR_DATA_STREAM_TYPE_FLOAT:
231 *(
float *) result = *((
float *)
self->_buffer->feed +
self->_feed_head);
234 case TWR_DATA_STREAM_TYPE_INT:
236 *(
int *) result = *((
int *)
self->_buffer->feed +
self->_feed_head);
254 if (self->_counter + n < 0)
259 if (self->_buffer->number_of_samples + n < 0)
264 position =
self->_counter <
self->_buffer->number_of_samples ?
self->_counter :
self->_feed_head + 1 +
self->_buffer->number_of_samples;
268 if (self->_counter < n)
273 if (self->_buffer->number_of_samples > n)
278 position =
self->_counter <
self->_buffer->number_of_samples ? 0 :
self->_feed_head + 1;
281 position = (position + n) % self->_buffer->number_of_samples;
283 switch (self->_buffer->type)
285 case TWR_DATA_STREAM_TYPE_FLOAT:
287 *(
float *) result = *((
float *)
self->_buffer->feed + position);
290 case TWR_DATA_STREAM_TYPE_INT:
292 *(
int *) result = *((
int *)
self->_buffer->feed + position);
306 if (self->_counter < self->_min_number_of_samples)
313 switch (self->_buffer->type)
315 case TWR_DATA_STREAM_TYPE_FLOAT:
317 float *buffer = (
float *) self->_buffer->feed;
319 float max = buffer[0];
321 for (
int i = 1; i < length; i ++)
329 *(
float *) result = max;
333 case TWR_DATA_STREAM_TYPE_INT:
335 int *buffer = (
int *) self->_buffer->feed;
339 for (
int i = 1; i < length; i ++)
347 *(
int *) result = max;
362 if (self->_counter < self->_min_number_of_samples)
369 switch (self->_buffer->type)
371 case TWR_DATA_STREAM_TYPE_FLOAT:
373 float *buffer = (
float *) self->_buffer->feed;
375 float min = buffer[0];
377 for (
int i = 1; i < length; i ++)
385 *(
float *) result = min;
389 case TWR_DATA_STREAM_TYPE_INT:
391 int *buffer = (
int *) self->_buffer->feed;
395 for (
int i = 1; i < length; i ++)
403 *(
int *) result = min;
416 static int _twr_data_stream_compare_float(
const void *a,
const void *b)
418 return *(
float *) a - *(
float *) b;
421 static int _twr_data_stream_compare_int(
const void *a,
const void *b)
423 return *(
int *) a - *(
int *) b;
bool twr_data_stream_get_nth(twr_data_stream_t *self, int n, void *result)
Get nth value in data stream.
int twr_data_stream_get_number_of_samples(twr_data_stream_t *self)
Get buffer number_of_samples.
bool twr_data_stream_get_max(twr_data_stream_t *self, void *result)
Get max value.
struct twr_data_stream_t twr_data_stream_t
Data stream instance.
bool twr_data_stream_get_min(twr_data_stream_t *self, void *result)
Get min value.
twr_data_stream_type_t twr_data_stream_get_type(twr_data_stream_t *self)
Get type.
void twr_data_stream_reset(twr_data_stream_t *self)
Reset data stream.
bool twr_data_stream_get_first(twr_data_stream_t *self, void *result)
Get first value in data stream.
void twr_data_stream_init(twr_data_stream_t *self, int min_number_of_samples, twr_data_stream_buffer_t *buffer)
Initialize data stream instance.
int twr_data_stream_get_counter(twr_data_stream_t *self)
Get counter.
int twr_data_stream_get_length(twr_data_stream_t *self)
Get length.
twr_data_stream_type_t
Data stream type.
bool twr_data_stream_get_median(twr_data_stream_t *self, void *result)
Get median value of data stream.
bool twr_data_stream_get_average(twr_data_stream_t *self, void *result)
Get average value of data stream.
bool twr_data_stream_get_last(twr_data_stream_t *self, void *result)
Get last value in data stream.
void twr_data_stream_feed(twr_data_stream_t *self, void *data)
Feed data into stream instance.