OSDN Git Service

45e846d4a9dad260e244197eacbebf58c4a0cea8
[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 void denoise_median(struct sensor_info_t* info, struct sensors_event_t* data,
100                                         unsigned int num_fields)
101 {
102         float x, y, z;
103         float scale;
104         unsigned int field, offset;
105
106         struct filter_median* f_data = (struct filter_median*) info->filter;
107         if (!f_data)
108                 return;
109
110
111         if (f_data->count < f_data->sample_size)
112                 f_data->count++;
113
114         for (field = 0; field < num_fields; field++) {
115                 offset = f_data->sample_size * field;
116                 f_data->buff[offset + f_data->idx] = data->data[field];
117
118                 data->data[field] = median(f_data->buff + offset, f_data->count);
119         }
120
121         f_data->idx = (f_data->idx + 1) % f_data->sample_size;
122 }
123