OSDN Git Service

cleanup: Remove handling for continous sensors when setting flags
[android-x86/hardware-intel-libsensors.git] / filtering.c
1 /*
2  * Copyright (C) 2014 Intel Corporation.
3  */
4
5 #include <hardware/sensors.h>
6 #include <utils/Log.h>
7 #include "common.h"
8 #include "filtering.h"
9
10
11 typedef struct
12 {
13         float* buff;
14         unsigned int idx;
15         unsigned int count;
16         unsigned int sample_size;
17 }
18 filter_median_t;
19
20
21 static unsigned int partition (float* list, unsigned int left, unsigned int right, unsigned int pivot_index)
22 {
23         unsigned int i;
24         unsigned int store_index = left;
25         float aux;
26         float pivot_value = list[pivot_index];
27
28         /* Swap list[pivotIndex] and list[right] */
29         aux = list[pivot_index];
30         list[pivot_index] = list[right];
31         list[right] = aux;
32
33         for (i = left; i < right; i++)
34         {
35                 if (list[i] < pivot_value)
36                 {
37                         /* Swap list[store_index] and list[i] */
38                         aux = list[store_index];
39                         list[store_index] = list[i];
40                         list[i] = aux;
41                         store_index++;
42                 }
43         }
44
45         /* Swap list[right] and list[store_index] */
46         aux = list[right];
47         list[right] = list[store_index];
48         list[store_index] = aux;
49         return store_index;
50 }
51
52
53 static float median (float* queue, unsigned int size)
54 {
55         /* http://en.wikipedia.org/wiki/Quickselect */
56
57         unsigned int left = 0;
58         unsigned int right = size - 1;
59         unsigned int pivot_index;
60         unsigned int median_index = (right / 2);
61         float temp[size];
62
63         memcpy(temp, queue, size * sizeof(float));
64
65         /* If the list has only one element return it */
66         if (left == right)
67                 return temp[left];
68
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;
76                 else
77                         left = pivot_index + 1;
78         }
79
80         return temp[left];
81 }
82
83
84 static void denoise_median_init (int s, unsigned int num_fields, unsigned int max_samples)
85 {
86         filter_median_t* f_data = (filter_median_t*) malloc(sizeof(filter_median_t));
87
88         f_data->buff = (float*) calloc(max_samples, sizeof(float) * num_fields);
89         f_data->sample_size = max_samples;
90         f_data->count = 0;
91         f_data->idx = 0;
92         sensor[s].filter = f_data;
93 }
94
95
96 static void denoise_median_reset (sensor_info_t* info)
97 {
98         filter_median_t* f_data = (filter_median_t*) info->filter;
99
100         if (!f_data)
101                 return;
102
103         f_data->count = 0;
104         f_data->idx = 0;
105 }
106
107
108 static void denoise_median (sensor_info_t* info, sensors_event_t* data, unsigned int num_fields)
109 {
110         float x, y, z;
111         float scale;
112         unsigned int field, offset;
113
114         filter_median_t* f_data = (filter_median_t*) info->filter;
115         if (!f_data)
116                 return;
117
118         /* If we are at event count 1 reset the indices */
119         if (info->event_count == 1)
120                 denoise_median_reset(info);
121
122         if (f_data->count < f_data->sample_size)
123                 f_data->count++;
124
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];
128
129                 data->data[field] = median(f_data->buff + offset, f_data->count);
130         }
131
132         f_data->idx = (f_data->idx + 1) % f_data->sample_size;
133 }
134
135
136 static void denoise_average (sensor_info_t* si, sensors_event_t* data, int num_fields, int max_samples)
137 {
138         /*
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.
142          */
143
144         int i;
145         int f;
146         int sampling_rate = (int) si->sampling_rate;
147         int history_size;
148         int history_full = 0;
149
150         /* Don't denoise anything if we have less than two samples per second */
151         if (sampling_rate < 2)
152                 return;
153
154         /* Restrict window size to the min of sampling_rate and max_samples */
155         if (sampling_rate > max_samples)
156                 history_size = max_samples;
157         else
158                 history_size = sampling_rate;
159
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));
166                 if (si->history) {
167                         si->history_sum = (float*) realloc(si->history_sum, num_fields * sizeof(float));
168                         if (si->history_sum)
169                                 memset(si->history_sum, 0, num_fields * sizeof(float));
170                 }
171         }
172
173         if (!si->history || !si->history_sum)
174                 return; /* Unlikely, but still... */
175
176         /* Update initialized samples count */
177         if (si->history_entries < si->history_size)
178                 si->history_entries++;
179         else
180                 history_full = 1;
181
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 */
185                 if (history_full)
186                         si->history_sum[f] -=
187                                 si->history[si->history_index * num_fields + f];
188
189                 si->history[si->history_index * num_fields + f] = data->data[f];
190                 si->history_sum[f] += data->data[f];
191
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;
194         }
195
196         /* Update our rolling index (next evicted cell) */
197         si->history_index = (si->history_index + 1) % si->history_size;
198 }
199
200
201 void setup_noise_filtering (int s)
202 {
203         switch (sensor[s].type) {
204                 case SENSOR_TYPE_GYROSCOPE:
205                         denoise_median_init(s, 3, 5);
206                         break;
207         }
208 }
209
210
211 void denoise (int s, sensors_event_t* data)
212 {
213         switch (sensor[s].type) {
214                 case SENSOR_TYPE_GYROSCOPE:
215                         denoise_median(&sensor[s], data, 3);
216                         break;
217
218                 case SENSOR_TYPE_MAGNETIC_FIELD:
219                         denoise_average(&sensor[s], data, 3 , 20);
220                         break;
221         }
222 }
223
224
225 void release_noise_filtering_data (int s)
226 {
227         void *buff;
228
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;
237                 }
238         }
239
240         /* Delete median filter structures */
241         if (sensor[s].filter) {
242                 buff = ((filter_median_t*) sensor[s].filter)->buff;
243
244                 if (buff)
245                         free(buff);
246
247                 free(sensor[s].filter);
248                 sensor[s].filter = NULL;
249         }
250 }
251
252
253 #define GLOBAL_HISTORY_SIZE 100
254
255 typedef struct
256 {
257         int sensor;
258         int motion_trigger;
259         sensors_event_t data;
260 }
261 recorded_sample_t;
262
263 /*
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.
268  */
269
270 static recorded_sample_t global_history[GLOBAL_HISTORY_SIZE];
271
272 static int initialized_entries; /* How many of these are initialized          */
273 static int insertion_index;     /* Index of sample to evict next time         */
274
275
276 void record_sample (int s, const sensors_event_t* event)
277 {
278         recorded_sample_t *cell;
279         int i;
280
281         /* Don't record duplicate samples, as they are not useful for filters */
282         if (sensor[s].report_pending == DATA_DUPLICATE)
283                 return;
284
285         if (initialized_entries == GLOBAL_HISTORY_SIZE) {
286                 i = insertion_index;
287                 insertion_index = (insertion_index+1) % GLOBAL_HISTORY_SIZE;
288         } else {
289                 i = initialized_entries;
290                 initialized_entries++;
291         }
292
293         cell = &global_history[i];
294
295         cell->sensor = s;
296
297         cell->motion_trigger = (sensor[s].selected_trigger == sensor[s].motion_trigger_name);
298
299         memcpy(&cell->data, event, sizeof(sensors_event_t));
300 }