OSDN Git Service

GMINL-2659: Keep recent events history for fusion-like processing
[android-x86/hardware-intel-libsensors.git] / filtering.c
1 #include <stdlib.h>
2 #include <hardware/sensors.h>
3 #include <math.h>
4 #include <pthread.h>
5 #include <utils/Log.h>
6 #include "common.h"
7 #include "filtering.h"
8
9
10 struct filter_median
11 {
12         float* buff;
13         unsigned int idx;
14         unsigned int count;
15         unsigned int sample_size;
16 };
17
18 static unsigned int partition(float* list, unsigned int left,
19         unsigned int right, unsigned int pivot_index)
20 {
21         unsigned int i;
22         unsigned int store_index = left;
23         float aux;
24         float pivot_value = list[pivot_index];
25
26         // swap list[pivotIndex] and list[right]
27         aux = list[pivot_index];
28         list[pivot_index] = list[right];
29         list[right] = aux;
30
31         for (i = left; i < right; i++)
32         {
33                 if (list[i] < pivot_value)
34                 {
35                         // swap list[store_index] and list[i]
36                         aux = list[store_index];
37                         list[store_index] = list[i];
38                         list[i] = aux;
39                         store_index++;
40                 }
41         }
42         //swap list[right] and list[store_index]
43         aux = list[right];
44         list[right] = list[store_index];
45         list[store_index] = aux;
46         return store_index;
47 }
48
49 /* http://en.wikipedia.org/wiki/Quickselect */
50 float median(float* queue, unsigned int size)
51 {
52         unsigned int left = 0;
53         unsigned int right = size - 1;
54         unsigned int pivot_index;
55         unsigned int median_index = (right / 2);
56         float temp[size];
57
58         memcpy(temp, queue, size * sizeof(float));
59
60         /* If the list has only one element return it */
61         if (left == right)
62                 return temp[left];
63
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;
71                 else
72                         left = pivot_index + 1;
73         }
74
75         return temp[left];
76 }
77
78 void denoise_median_init(int s, unsigned int num_fields,
79         unsigned int max_samples)
80 {
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;
85         f_data->count = 0;
86         f_data->idx = 0;
87         sensor_info[s].filter = f_data;
88 }
89
90 void denoise_median_release(int s)
91 {
92         if (!sensor_info[s].filter)
93                 return;
94
95         free(((struct filter_median*)sensor_info[s].filter)->buff);
96         free(sensor_info[s].filter);
97         sensor_info[s].filter = NULL;
98 }
99
100 void denoise_median(struct sensor_info_t* info, struct sensors_event_t* data,
101                                         unsigned int num_fields)
102 {
103         float x, y, z;
104         float scale;
105         unsigned int field, offset;
106
107         struct filter_median* f_data = (struct filter_median*) info->filter;
108         if (!f_data)
109                 return;
110
111
112         if (f_data->count < f_data->sample_size)
113                 f_data->count++;
114
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];
118
119                 data->data[field] = median(f_data->buff + offset, f_data->count);
120         }
121
122         f_data->idx = (f_data->idx + 1) % f_data->sample_size;
123 }
124
125
126 #define GLOBAL_HISTORY_SIZE 100
127
128 struct recorded_sample_t
129 {
130         int sensor;
131         int motion_trigger;
132         sensors_event_t data;
133 };
134
135 /*
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.
143  */
144
145 static struct recorded_sample_t global_history[GLOBAL_HISTORY_SIZE];
146
147 static int initialized_entries; /* How many of these are initialized          */
148 static int insertion_index;     /* Index of sample to evict next time         */
149
150
151 void record_sample (int s, const struct sensors_event_t* event)
152 {
153         struct recorded_sample_t *cell;
154         int i;
155
156         /* Don't record duplicate samples, as they are not useful for filters */
157         if (sensor_info[s].report_pending == DATA_DUPLICATE)
158                 return;
159
160         if (initialized_entries == GLOBAL_HISTORY_SIZE) {
161                 i = insertion_index;
162                 insertion_index = (insertion_index+1) % GLOBAL_HISTORY_SIZE;
163         } else {
164                 i = initialized_entries;
165                 initialized_entries++;
166         }
167
168         cell = &global_history[i];
169
170         cell->sensor            = s;
171
172         cell->motion_trigger    = (sensor_info[s].selected_trigger ==
173                                    sensor_info[s].motion_trigger_name);
174
175         memcpy(&cell->data, event, sizeof(sensors_event_t));
176 }