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 static void denoise_median_reset(struct sensor_info_t* info)
92 struct filter_median* f_data = (struct filter_median*) info->filter;
101 void denoise_median_release(int s)
103 if (!sensor_info[s].filter)
106 free(((struct filter_median*)sensor_info[s].filter)->buff);
107 free(sensor_info[s].filter);
108 sensor_info[s].filter = NULL;
111 void denoise_median(struct sensor_info_t* info, struct sensors_event_t* data,
112 unsigned int num_fields)
116 unsigned int field, offset;
118 struct filter_median* f_data = (struct filter_median*) info->filter;
122 /* If we are at event count 1 reset the indexes */
123 if (info->event_count == 1)
124 denoise_median_reset(info);
126 if (f_data->count < f_data->sample_size)
129 for (field = 0; field < num_fields; field++) {
130 offset = f_data->sample_size * field;
131 f_data->buff[offset + f_data->idx] = data->data[field];
133 data->data[field] = median(f_data->buff + offset, f_data->count);
136 f_data->idx = (f_data->idx + 1) % f_data->sample_size;
140 #define GLOBAL_HISTORY_SIZE 100
142 struct recorded_sample_t
146 sensors_event_t data;
150 * This is a circular buffer holding the last GLOBAL_HISTORY_SIZE events,
151 * covering the entire sensor collection. It is intended as a way to correlate
152 * data coming from active sensors, no matter the sensor type, over a recent
153 * window of time. The array is not sorted ; we simply evict the oldest cell
154 * (by insertion time) and replace its contents. Timestamps don't necessarily
155 * grow monotonically as they tell the data acquisition type, and that there can
156 * be a delay between acquisition and insertion into this table.
159 static struct recorded_sample_t global_history[GLOBAL_HISTORY_SIZE];
161 static int initialized_entries; /* How many of these are initialized */
162 static int insertion_index; /* Index of sample to evict next time */
165 void record_sample (int s, const struct sensors_event_t* event)
167 struct recorded_sample_t *cell;
170 /* Don't record duplicate samples, as they are not useful for filters */
171 if (sensor_info[s].report_pending == DATA_DUPLICATE)
174 if (initialized_entries == GLOBAL_HISTORY_SIZE) {
176 insertion_index = (insertion_index+1) % GLOBAL_HISTORY_SIZE;
178 i = initialized_entries;
179 initialized_entries++;
182 cell = &global_history[i];
186 cell->motion_trigger = (sensor_info[s].selected_trigger ==
187 sensor_info[s].motion_trigger_name);
189 memcpy(&cell->data, event, sizeof(sensors_event_t));