2 #include <hardware/sensors.h>
15 unsigned int sample_size;
18 static unsigned int partition(float* list, unsigned int left,
19 unsigned int right, unsigned int pivot_index)
22 unsigned int store_index = left;
24 float pivot_value = list[pivot_index];
26 // swap list[pivotIndex] and list[right]
27 aux = list[pivot_index];
28 list[pivot_index] = list[right];
31 for (i = left; i < right; i++)
33 if (list[i] < pivot_value)
35 // swap list[store_index] and list[i]
36 aux = list[store_index];
37 list[store_index] = list[i];
42 //swap list[right] and list[store_index]
44 list[right] = list[store_index];
45 list[store_index] = aux;
49 /* http://en.wikipedia.org/wiki/Quickselect */
50 float median(float* queue, unsigned int size)
52 unsigned int left = 0;
53 unsigned int right = size - 1;
54 unsigned int pivot_index;
55 unsigned int median_index = (right / 2);
58 memcpy(temp, queue, size * sizeof(float));
60 /* If the list has only one element return it */
64 while (left < right) {
65 pivot_index = (left + right) / 2;
66 pivot_index = partition(temp, left, right, pivot_index);
67 if (pivot_index == median_index)
68 return temp[median_index];
69 else if (pivot_index > median_index)
70 right = pivot_index - 1;
72 left = pivot_index + 1;
78 void denoise_median_init(int s, unsigned int num_fields,
79 unsigned int max_samples)
81 struct filter_median* f_data = (struct filter_median*) calloc(1, sizeof(struct filter_median));
82 f_data->buff = (float*)calloc(max_samples,
83 sizeof(float) * num_fields);
84 f_data->sample_size = max_samples;
87 sensor_info[s].filter = f_data;
90 void denoise_median_release(int s)
92 if (!sensor_info[s].filter)
95 free(((struct filter_median*)sensor_info[s].filter)->buff);
96 free(sensor_info[s].filter);
97 sensor_info[s].filter = NULL;
100 void denoise_median(struct sensor_info_t* info, struct sensors_event_t* data,
101 unsigned int num_fields)
105 unsigned int field, offset;
107 struct filter_median* f_data = (struct filter_median*) info->filter;
112 if (f_data->count < f_data->sample_size)
115 for (field = 0; field < num_fields; field++) {
116 offset = f_data->sample_size * field;
117 f_data->buff[offset + f_data->idx] = data->data[field];
119 data->data[field] = median(f_data->buff + offset, f_data->count);
122 f_data->idx = (f_data->idx + 1) % f_data->sample_size;
126 #define GLOBAL_HISTORY_SIZE 100
128 struct recorded_sample_t
132 sensors_event_t data;
136 * This is a circular buffer holding the last GLOBAL_HISTORY_SIZE events,
137 * covering the entire sensor collection. It is intended as a way to correlate
138 * data coming from active sensors, no matter the sensor type, over a recent
139 * window of time. The array is not sorted ; we simply evict the oldest cell
140 * (by insertion time) and replace its contents. Timestamps don't necessarily
141 * grow monotonically as they tell the data acquisition type, and that there can
142 * be a delay between acquisition and insertion into this table.
145 static struct recorded_sample_t global_history[GLOBAL_HISTORY_SIZE];
147 static int initialized_entries; /* How many of these are initialized */
148 static int insertion_index; /* Index of sample to evict next time */
151 void record_sample (int s, const struct sensors_event_t* event)
153 struct recorded_sample_t *cell;
156 /* Don't record duplicate samples, as they are not useful for filters */
157 if (sensor_info[s].report_pending == DATA_DUPLICATE)
160 if (initialized_entries == GLOBAL_HISTORY_SIZE) {
162 insertion_index = (insertion_index+1) % GLOBAL_HISTORY_SIZE;
164 i = initialized_entries;
165 initialized_entries++;
168 cell = &global_history[i];
172 cell->motion_trigger = (sensor_info[s].selected_trigger ==
173 sensor_info[s].motion_trigger_name);
175 memcpy(&cell->data, event, sizeof(sensors_event_t));