2 * Copyright (C) 2014 Intel Corporation.
5 #include <hardware/sensors.h>
16 unsigned int sample_size;
21 static unsigned int partition (float* list, unsigned int left, unsigned int right, unsigned int pivot_index)
24 unsigned int store_index = left;
26 float pivot_value = list[pivot_index];
28 /* Swap list[pivotIndex] and list[right] */
29 aux = list[pivot_index];
30 list[pivot_index] = list[right];
33 for (i = left; i < right; i++)
35 if (list[i] < pivot_value)
37 /* Swap list[store_index] and list[i] */
38 aux = list[store_index];
39 list[store_index] = list[i];
45 /* Swap list[right] and list[store_index] */
47 list[right] = list[store_index];
48 list[store_index] = aux;
53 static float median (float* queue, unsigned int size)
55 /* http://en.wikipedia.org/wiki/Quickselect */
57 unsigned int left = 0;
58 unsigned int right = size - 1;
59 unsigned int pivot_index;
60 unsigned int median_index = (right / 2);
63 memcpy(temp, queue, size * sizeof(float));
65 /* If the list has only one element return it */
69 while (left < right) {
70 pivot_index = (left + right) / 2;
71 pivot_index = partition(temp, left, right, pivot_index);
72 if (pivot_index == median_index)
73 return temp[median_index];
74 else if (pivot_index > median_index)
75 right = pivot_index - 1;
77 left = pivot_index + 1;
84 static void denoise_median_init (int s, unsigned int num_fields, unsigned int max_samples)
86 filter_median_t* f_data = (filter_median_t*) malloc(sizeof(filter_median_t));
88 f_data->buff = (float*) calloc(max_samples, sizeof(float) * num_fields);
89 f_data->sample_size = max_samples;
92 sensor[s].filter = f_data;
96 static void denoise_median_reset (sensor_info_t* info)
98 filter_median_t* f_data = (filter_median_t*) info->filter;
108 static void denoise_median (sensor_info_t* info, sensors_event_t* data, unsigned int num_fields)
112 unsigned int field, offset;
114 filter_median_t* f_data = (filter_median_t*) info->filter;
118 /* If we are at event count 1 reset the indices */
119 if (info->event_count == 1)
120 denoise_median_reset(info);
122 if (f_data->count < f_data->sample_size)
125 for (field = 0; field < num_fields; field++) {
126 offset = f_data->sample_size * field;
127 f_data->buff[offset + f_data->idx] = data->data[field];
129 data->data[field] = median(f_data->buff + offset, f_data->count);
132 f_data->idx = (f_data->idx + 1) % f_data->sample_size;
136 static void denoise_average (sensor_info_t* si, sensors_event_t* data, int num_fields, int max_samples)
139 * Smooth out incoming data using a moving average over a number of
140 * samples. We accumulate one second worth of samples, or max_samples,
141 * depending on which is lower.
146 int sampling_rate = (int) si->sampling_rate;
148 int history_full = 0;
150 /* Don't denoise anything if we have less than two samples per second */
151 if (sampling_rate < 2)
154 /* Restrict window size to the min of sampling_rate and max_samples */
155 if (sampling_rate > max_samples)
156 history_size = max_samples;
158 history_size = sampling_rate;
160 /* Reset history if we're operating on an incorrect window size */
161 if (si->history_size != history_size) {
162 si->history_size = history_size;
163 si->history_entries = 0;
164 si->history_index = 0;
165 si->history = (float*) realloc(si->history, si->history_size * num_fields * sizeof(float));
167 si->history_sum = (float*) realloc(si->history_sum, num_fields * sizeof(float));
169 memset(si->history_sum, 0, num_fields * sizeof(float));
173 if (!si->history || !si->history_sum)
174 return; /* Unlikely, but still... */
176 /* Update initialized samples count */
177 if (si->history_entries < si->history_size)
178 si->history_entries++;
182 /* Record new sample and calculate the moving sum */
183 for (f=0; f < num_fields; f++) {
184 /** A field is going to be overwritten if history is full, so decrease the history sum */
186 si->history_sum[f] -=
187 si->history[si->history_index * num_fields + f];
189 si->history[si->history_index * num_fields + f] = data->data[f];
190 si->history_sum[f] += data->data[f];
192 /* For now simply compute a mobile mean for each field and output filtered data */
193 data->data[f] = si->history_sum[f] / si->history_entries;
196 /* Update our rolling index (next evicted cell) */
197 si->history_index = (si->history_index + 1) % si->history_size;
201 void setup_noise_filtering (int s)
203 switch (sensor[s].type) {
204 case SENSOR_TYPE_GYROSCOPE:
205 denoise_median_init(s, 3, 5);
211 void denoise (int s, sensors_event_t* data)
213 switch (sensor[s].type) {
214 case SENSOR_TYPE_GYROSCOPE:
215 denoise_median(&sensor[s], data, 3);
218 case SENSOR_TYPE_MAGNETIC_FIELD:
219 denoise_average(&sensor[s], data, 3 , 20);
225 void release_noise_filtering_data (int s)
229 /* Delete moving average structures */
230 if (sensor[s].history) {
231 free(sensor[s].history);
232 sensor[s].history = NULL;
233 sensor[s].history_size = 0;
234 if (sensor[s].history_sum) {
235 free(sensor[s].history_sum);
236 sensor[s].history_sum = NULL;
240 /* Delete median filter structures */
241 if (sensor[s].filter) {
242 buff = ((filter_median_t*) sensor[s].filter)->buff;
247 free(sensor[s].filter);
248 sensor[s].filter = NULL;
253 #define GLOBAL_HISTORY_SIZE 100
259 sensors_event_t data;
264 * This is a circular buffer holding the last GLOBAL_HISTORY_SIZE events, covering the entire sensor collection. It is intended as a way to correlate
265 * data coming from active sensors, no matter the sensor type, over a recent window of time. The array is not sorted ; we simply evict the oldest cell
266 * (by insertion time) and replace its contents. Timestamps don't necessarily grow monotonically as they tell the data acquisition type, and that there
267 * can be a delay between acquisition and insertion into this table.
270 static recorded_sample_t global_history[GLOBAL_HISTORY_SIZE];
272 static int initialized_entries; /* How many of these are initialized */
273 static int insertion_index; /* Index of sample to evict next time */
276 void record_sample (int s, const sensors_event_t* event)
278 recorded_sample_t *cell;
281 /* Don't record duplicate samples, as they are not useful for filters */
282 if (sensor[s].report_pending == DATA_DUPLICATE)
285 if (initialized_entries == GLOBAL_HISTORY_SIZE) {
287 insertion_index = (insertion_index+1) % GLOBAL_HISTORY_SIZE;
289 i = initialized_entries;
290 initialized_entries++;
293 cell = &global_history[i];
297 cell->motion_trigger = (sensor[s].selected_trigger == sensor[s].motion_trigger_name);
299 memcpy(&cell->data, event, sizeof(sensors_event_t));