OSDN Git Service

GMINL-3234: Refactor a little bit our filtering code
authorPatrick Porlan <patrick.porlan@intel.com>
Fri, 14 Nov 2014 14:43:44 +0000 (15:43 +0100)
committerGerrit Code Review <gerrit@mcg-psi-gcr1.jf.intel.com>
Fri, 14 Nov 2014 16:34:11 +0000 (08:34 -0800)
filtering.h has a simpler interface now, and filtering structures
are now uniformly allocated/cleaned on enable/disable, for sensors
marked as noisy.

This paves the way for per-sensor filter type and parameters.

Change-Id: I1e867570e79c3a56bac0734307a023c1101bb826
Tracked-On: https://jira01.devtools.intel.com/browse/GMINL-3234
Signed-off-by: Patrick Porlan <patrick.porlan@intel.com>
control.c
enumeration.c
filtering.c
filtering.h
transform.c

index 9565f46..3e3e8fb 100644 (file)
--- a/control.c
+++ b/control.c
@@ -17,6 +17,7 @@
 #include "transform.h"
 #include "calibration.h"
 #include "description.h"
+#include "filtering.h"
 
 /* Currently active sensors count, per device */
 static int poll_sensors_per_dev[MAX_DEVICES];  /* poll-mode sensors */
@@ -561,6 +562,10 @@ int sensor_activate(int s, int enabled)
        sensor_info[s].event_count = 0;
        sensor_info[s].meta_data_pending = 0;
 
+       if (enabled && (sensor_info[s].quirks & QUIRK_NOISY))
+               /* Initialize filtering data if required */
+               setup_noise_filtering(s);
+
        if (!is_poll_sensor) {
 
                /* Stop sampling */
@@ -600,16 +605,8 @@ int sensor_activate(int s, int enabled)
                                device_fd[dev_num] = -1;
                        }
 
-               /* If we recorded a trail of samples for filtering, delete it */
-               if (sensor_info[s].history) {
-                       free(sensor_info[s].history);
-                       sensor_info[s].history = NULL;
-                       sensor_info[s].history_size = 0;
-                       if (sensor_info[s].history_sum) {
-                               free(sensor_info[s].history_sum);
-                               sensor_info[s].history_sum = NULL;
-                       }
-               }
+               /* Release any filtering data we may have accumulated */
+               release_noise_filtering_data(s);
 
                return 0;
        }
index 91c156b..5e53841 100644 (file)
@@ -14,7 +14,6 @@
 #include "description.h"
 #include "control.h"
 #include "calibration.h"
-#include "filtering.h"
 
 /*
  * This table maps syfs entries in scan_elements directories to sensor types,
@@ -379,7 +378,6 @@ static void add_sensor (int dev_num, int catalog_index, int use_polling)
                sensor_type == SENSOR_TYPE_GYROSCOPE_UNCALIBRATED) {
                struct gyro_cal* calibration_data = calloc(1, sizeof(struct gyro_cal));
                sensor_info[s].cal_data = calibration_data;
-               denoise_median_init(s, 3, 7);
        }
 
        if (sensor_type == SENSOR_TYPE_MAGNETIC_FIELD) {
@@ -801,6 +799,7 @@ void delete_enumeration_data (void)
                                sensor_info[i].cal_level = 0;
                        }
                        break;
+
                case SENSOR_TYPE_GYROSCOPE_UNCALIBRATED:
                case SENSOR_TYPE_GYROSCOPE:
                        if (sensor_info[i].cal_data != NULL) {
@@ -809,9 +808,7 @@ void delete_enumeration_data (void)
                                sensor_info[i].cal_level = 0;
                        }
                        break;
-                       if (sensor_info[i].filter != NULL) {
-                               denoise_median_release(i);
-                       }
+
                default:
                        break;
        }
index adabb30..741a85d 100644 (file)
@@ -15,15 +15,16 @@ struct filter_median
        unsigned int sample_size;
 };
 
-static unsigned int partition(float* list, unsigned int left,
-       unsigned int right, unsigned int pivot_index)
+
+static unsigned int partition (        float* list, unsigned int left,
+                               unsigned int right, unsigned int pivot_index)
 {
        unsigned int i;
        unsigned int store_index = left;
        float aux;
        float pivot_value = list[pivot_index];
 
-       // swap list[pivotIndex] and list[right]
+       /* Swap list[pivotIndex] and list[right] */
        aux = list[pivot_index];
        list[pivot_index] = list[right];
        list[right] = aux;
@@ -32,23 +33,26 @@ static unsigned int partition(float* list, unsigned int left,
        {
                if (list[i] < pivot_value)
                {
-                       // swap list[store_index] and list[i]
+                       /* Swap list[store_index] and list[i] */
                        aux = list[store_index];
                        list[store_index] = list[i];
                        list[i] = aux;
                        store_index++;
                }
        }
-       //swap list[right] and list[store_index]
+
+       /* Swap list[right] and list[store_index] */
        aux = list[right];
        list[right] = list[store_index];
        list[store_index] = aux;
        return store_index;
 }
 
-/* http://en.wikipedia.org/wiki/Quickselect */
-float median(float* queue, unsigned int size)
+
+static float median (float* queue, unsigned int size)
 {
+       /* http://en.wikipedia.org/wiki/Quickselect */
+
        unsigned int left = 0;
        unsigned int right = size - 1;
        unsigned int pivot_index;
@@ -75,10 +79,13 @@ float median(float* queue, unsigned int size)
        return temp[left];
 }
 
-void denoise_median_init(int s, unsigned int num_fields,
-       unsigned int max_samples)
+
+static void denoise_median_init(int s, unsigned int num_fields,
+                               unsigned int max_samples)
 {
-       struct filter_median* f_data = (struct filter_median*) calloc(1, sizeof(struct filter_median));
+       struct filter_median* f_data = (struct filter_median*) calloc(1,
+                                               sizeof(struct filter_median));
+
        f_data->buff = (float*)calloc(max_samples,
                sizeof(float) * num_fields);
        f_data->sample_size = max_samples;
@@ -87,7 +94,8 @@ void denoise_median_init(int s, unsigned int num_fields,
        sensor_info[s].filter = f_data;
 }
 
-static void denoise_median_reset(struct sensor_info_t* info)
+
+static void denoise_median_reset (struct sensor_info_t* info)
 {
        struct filter_median* f_data = (struct filter_median*) info->filter;
 
@@ -98,18 +106,10 @@ static void denoise_median_reset(struct sensor_info_t* info)
        f_data->idx = 0;
 }
 
-void denoise_median_release(int s)
-{
-       if (!sensor_info[s].filter)
-               return;
-
-       free(((struct filter_median*)sensor_info[s].filter)->buff);
-       free(sensor_info[s].filter);
-       sensor_info[s].filter = NULL;
-}
 
-void denoise_median(struct sensor_info_t* info, struct sensors_event_t* data,
-                                       unsigned int num_fields)
+static void denoise_median (   struct sensor_info_t* info,
+                               struct sensors_event_t* data,
+                               unsigned int num_fields)
 {
        float x, y, z;
        float scale;
@@ -119,7 +119,7 @@ void denoise_median(struct sensor_info_t* info, struct sensors_event_t* data,
        if (!f_data)
                return;
 
-       /* If we are at event count 1 reset the indexes */
+       /* If we are at event count 1 reset the indices */
        if (info->event_count == 1)
                denoise_median_reset(info);
 
@@ -137,61 +137,9 @@ void denoise_median(struct sensor_info_t* info, struct sensors_event_t* data,
 }
 
 
-#define GLOBAL_HISTORY_SIZE 100
-
-struct recorded_sample_t
-{
-       int sensor;
-       int motion_trigger;
-       sensors_event_t data;
-};
-
-/*
- * 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
- * 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
- * (by insertion time) and replace its contents. Timestamps don't necessarily
- * grow monotonically as they tell the data acquisition type, and that there can
- * be a delay between acquisition and insertion into this table.
- */
-
-static struct recorded_sample_t global_history[GLOBAL_HISTORY_SIZE];
-
-static int initialized_entries;        /* How many of these are initialized          */
-static int insertion_index;    /* Index of sample to evict next time         */
-
-
-void record_sample (int s, const struct sensors_event_t* event)
-{
-       struct recorded_sample_t *cell;
-       int i;
-
-       /* Don't record duplicate samples, as they are not useful for filters */
-       if (sensor_info[s].report_pending == DATA_DUPLICATE)
-               return;
-
-       if (initialized_entries == GLOBAL_HISTORY_SIZE) {
-               i = insertion_index;
-               insertion_index = (insertion_index+1) % GLOBAL_HISTORY_SIZE;
-       } else {
-               i = initialized_entries;
-               initialized_entries++;
-       }
-
-       cell = &global_history[i];
-
-       cell->sensor            = s;
-
-       cell->motion_trigger    = (sensor_info[s].selected_trigger ==
-                                  sensor_info[s].motion_trigger_name);
-
-       memcpy(&cell->data, event, sizeof(sensors_event_t));
-}
-
-
-void denoise_average ( struct sensor_info_t* si, struct sensors_event_t* data,
-                       int num_fields, int max_samples)
+static void denoise_average (  struct sensor_info_t* si,
+                               struct sensors_event_t* data,
+                               int num_fields, int max_samples)
 {
        /*
         * Smooth out incoming data using a moving average over a number of
@@ -260,3 +208,110 @@ void denoise_average (    struct sensor_info_t* si, struct sensors_event_t* data,
        /* Update our rolling index (next evicted cell) */
        si->history_index = (si->history_index + 1) % si->history_size;
 }
+
+
+void setup_noise_filtering (int s)
+{
+       switch (sensor_info[s].type) {
+               case SENSOR_TYPE_GYROSCOPE:
+               case SENSOR_TYPE_GYROSCOPE_UNCALIBRATED:
+                       denoise_median_init(s, 3, 7);
+                       break;
+       }
+}
+
+
+void denoise (int s, struct sensors_event_t* data)
+{
+       switch (sensor_info[s].type) {
+               case SENSOR_TYPE_GYROSCOPE:
+               case SENSOR_TYPE_GYROSCOPE_UNCALIBRATED:
+                       denoise_median(&sensor_info[s], data, 3);
+                       break;
+
+               case SENSOR_TYPE_MAGNETIC_FIELD:
+                       denoise_average(&sensor_info[s], data, 3 , 30);
+                       break;
+       }
+}
+
+
+void release_noise_filtering_data (int s)
+{
+       void *buff;
+
+       /* Delete moving average structures */
+       if (sensor_info[s].history) {
+               free(sensor_info[s].history);
+               sensor_info[s].history = NULL;
+               sensor_info[s].history_size = 0;
+               if (sensor_info[s].history_sum) {
+                       free(sensor_info[s].history_sum);
+                       sensor_info[s].history_sum = NULL;
+               }
+       }
+
+       /* Delete median filter structures */
+       if (sensor_info[s].filter) {
+               buff = ((struct filter_median*)sensor_info[s].filter)->buff;
+
+               if (buff)
+                       free(buff);
+
+               free(sensor_info[s].filter);
+               sensor_info[s].filter = NULL;
+       }
+}
+
+
+#define GLOBAL_HISTORY_SIZE 100
+
+struct recorded_sample_t
+{
+       int sensor;
+       int motion_trigger;
+       sensors_event_t data;
+};
+
+/*
+ * 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
+ * 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
+ * (by insertion time) and replace its contents. Timestamps don't necessarily
+ * grow monotonically as they tell the data acquisition type, and that there can
+ * be a delay between acquisition and insertion into this table.
+ */
+
+static struct recorded_sample_t global_history[GLOBAL_HISTORY_SIZE];
+
+static int initialized_entries;        /* How many of these are initialized          */
+static int insertion_index;    /* Index of sample to evict next time         */
+
+
+void record_sample (int s, const struct sensors_event_t* event)
+{
+       struct recorded_sample_t *cell;
+       int i;
+
+       /* Don't record duplicate samples, as they are not useful for filters */
+       if (sensor_info[s].report_pending == DATA_DUPLICATE)
+               return;
+
+       if (initialized_entries == GLOBAL_HISTORY_SIZE) {
+               i = insertion_index;
+               insertion_index = (insertion_index+1) % GLOBAL_HISTORY_SIZE;
+       } else {
+               i = initialized_entries;
+               initialized_entries++;
+       }
+
+       cell = &global_history[i];
+
+       cell->sensor            = s;
+
+       cell->motion_trigger    = (sensor_info[s].selected_trigger ==
+                                  sensor_info[s].motion_trigger_name);
+
+       memcpy(&cell->data, event, sizeof(sensors_event_t));
+}
index 14ee1e9..fda84e6 100644 (file)
@@ -1,14 +1,9 @@
 #ifndef FILTERING_H
 #define FILTERING_H
 
-void denoise_median(struct sensor_info_t* info, struct sensors_event_t* event,
-       unsigned int num_fields);
-void denoise_median_init(int s, unsigned int num_fields,
-       unsigned int max_samples);
-void denoise_median_release(int s);
+void setup_noise_filtering             (int s);
+void release_noise_filtering_data      (int s);
+void denoise                           (int s, struct sensors_event_t* event);
+void record_sample                     (int s, const sensors_event_t* data);
 
-void denoise_average ( struct sensor_info_t* si, struct sensors_event_t* data,
-                       int num_fields, int max_samples);
-
-void record_sample(int s, const sensors_event_t* data);
 #endif
index ac90c94..cc53f39 100644 (file)
@@ -257,13 +257,13 @@ static int finalize_sample_default (int s, struct sensors_event_t* data)
                        /* Always consider the accelerometer accurate */
                        data->acceleration.status = SENSOR_STATUS_ACCURACY_HIGH;
                        if (sensor_info[s].quirks & QUIRK_NOISY)
-                               denoise_average(&sensor_info[s], data, 3, 20);
+                               denoise(s, data);
                        break;
 
                case SENSOR_TYPE_MAGNETIC_FIELD:
                        calibrate_compass (data, &sensor_info[s], get_timestamp());
                        if (sensor_info[s].quirks & QUIRK_NOISY)
-                               denoise_average(&sensor_info[s], data, 3, 30);
+                               denoise(s, data);
                        break;
 
                case SENSOR_TYPE_GYROSCOPE:
@@ -295,11 +295,12 @@ static int finalize_sample_default (int s, struct sensors_event_t* data)
                         * events that can disturb our mean or stddev.
                         */
                        if (sensor_info[s].quirks & QUIRK_NOISY) {
-                               denoise_median(&sensor_info[s], data, 3);
                                if((sensor_info[s].selected_trigger !=
                                        sensor_info[s].motion_trigger_name) &&
                                        sensor_info[s].event_count < MIN_SAMPLES)
                                                return 0;
+
+                               denoise(s, data);
                        }
 
                        /* Clamp near zero moves to (0,0,0) if appropriate */
@@ -326,9 +327,6 @@ static int finalize_sample_default (int s, struct sensors_event_t* data)
                        break;
        }
 
-       /* Add this event to our global records, for filtering purposes */
-       record_sample(s, data);
-
        return 1; /* Return sample to Android */
 }