+
+static void denoise_average (sensor_info_t* si, sensors_event_t* data)
+{
+ /*
+ * Smooth out incoming data using a moving average over a number of
+ * samples. We accumulate one second worth of samples, or max_samples,
+ * depending on which is lower.
+ */
+
+ int f;
+ int sampling_rate = (int) si->sampling_rate;
+ int history_size;
+ int history_full = 0;
+ filter_average_t* filter;
+
+ /* Don't denoise anything if we have less than two samples per second */
+ if (sampling_rate < 2)
+ return;
+
+ filter = (filter_average_t*) si->filter;
+
+ if (!filter)
+ return;
+
+ /* Restrict window size to the min of sampling_rate and max_samples */
+ if (sampling_rate > filter->max_samples)
+ history_size = filter->max_samples;
+ else
+ history_size = sampling_rate;
+
+ /* Reset history if we're operating on an incorrect window size */
+ if (filter->history_size != history_size) {
+ filter->history_size = history_size;
+ filter->history_entries = 0;
+ filter->history_index = 0;
+ filter->history = (float*) realloc(filter->history, filter->history_size * filter->num_fields * sizeof(float));
+ if (filter->history) {
+ filter->history_sum = (float*) realloc(filter->history_sum, filter->num_fields * sizeof(float));
+ if (filter->history_sum)
+ memset(filter->history_sum, 0, filter->num_fields * sizeof(float));
+ }
+ }
+
+ if (!filter->history || !filter->history_sum)
+ return; /* Unlikely, but still... */
+
+ /* Update initialized samples count */
+ if (filter->history_entries < filter->history_size)
+ filter->history_entries++;
+ else
+ history_full = 1;
+
+ /* Record new sample and calculate the moving sum */
+ for (f=0; f < filter->num_fields; f++) {
+ /** A field is going to be overwritten if history is full, so decrease the history sum */
+ if (history_full)
+ filter->history_sum[f] -= filter->history[filter->history_index * filter->num_fields + f];
+
+ filter->history[filter->history_index * filter->num_fields + f] = data->data[f];
+ filter->history_sum[f] += data->data[f];
+
+ /* For now simply compute a mobile mean for each field and output filtered data */
+ data->data[f] = filter->history_sum[f] / filter->history_entries;
+ }
+
+ /* Update our rolling index (next evicted cell) */
+ filter->history_index = (filter->history_index + 1) % filter->history_size;
+}
+
+
+void setup_noise_filtering (int s)
+{
+ char filter_buf[MAX_NAME_SIZE];
+ int num_fields;
+ char* cursor;
+ int window_size = 0;
+
+ /* By default, don't apply filtering */
+ sensor[s].filter_type = FILTER_TYPE_NONE;
+
+ /* Restrict filtering to a few sensor types for now */
+ switch (sensor[s].type) {
+ case SENSOR_TYPE_ACCELEROMETER:
+ case SENSOR_TYPE_GYROSCOPE:
+ case SENSOR_TYPE_MAGNETIC_FIELD:
+ num_fields = 3 /* x,y,z */;
+ break;
+
+ default:
+ return; /* No filtering */
+ }
+
+ /* If noisy, start with default filter for sensor type */
+ if (sensor[s].quirks & QUIRK_NOISY)
+ switch (sensor[s].type) {
+ case SENSOR_TYPE_GYROSCOPE:
+ sensor[s].filter_type = FILTER_TYPE_MEDIAN;
+ break;
+
+ case SENSOR_TYPE_MAGNETIC_FIELD:
+ sensor[s].filter_type = FILTER_TYPE_MOVING_AVERAGE;
+ break;
+ }
+
+ /* Use whatever was specified if there's an explicit configuration choice for this sensor */
+
+ filter_buf[0] = '\0';
+ sensor_get_st_prop(s, "filter", filter_buf);
+
+ cursor = strstr(filter_buf, "median");
+ if (cursor)
+ sensor[s].filter_type = FILTER_TYPE_MEDIAN;
+ else {
+ cursor = strstr(filter_buf, "average");
+ if (cursor)
+ sensor[s].filter_type = FILTER_TYPE_MOVING_AVERAGE;
+ }
+
+ /* Check if an integer is part of the string, and use it as window size */
+ if (cursor) {
+ while (*cursor && !isdigit(*cursor))
+ cursor++;
+
+ if (*cursor)
+ window_size = atoi(cursor);
+ }
+
+ switch (sensor[s].filter_type) {
+
+ case FILTER_TYPE_MEDIAN:
+ denoise_median_init(s, num_fields, window_size ? window_size : 5);
+ break;
+
+ case FILTER_TYPE_MOVING_AVERAGE:
+ denoise_average_init(s, num_fields, window_size ? window_size: 20);
+ break;
+ }
+}
+
+
+void denoise (int s, sensors_event_t* data)
+{
+ switch (sensor[s].filter_type) {
+
+ case FILTER_TYPE_MEDIAN:
+ denoise_median(&sensor[s], data, 3);
+ break;
+
+ case FILTER_TYPE_MOVING_AVERAGE:
+ denoise_average(&sensor[s], data);
+ break;
+ }
+}
+
+
+void release_noise_filtering_data (int s)
+{
+ void *buf;
+
+ if (!sensor[s].filter)
+ return;
+
+ switch (sensor[s].filter_type) {
+
+ case FILTER_TYPE_MEDIAN:
+ buf = ((filter_median_t*) sensor[s].filter)->buff;
+ if (buf)
+ free(buf);
+ break;
+
+ case FILTER_TYPE_MOVING_AVERAGE:
+ buf = ((filter_average_t*) sensor[s].filter)->history;
+ if (buf)
+ free(buf);
+
+ buf = ((filter_average_t*) sensor[s].filter)->history_sum;
+ if (buf)
+ free(buf);
+ break;
+ }
+
+ free(sensor[s].filter);
+ sensor[s].filter = NULL;
+}
+
+
+#define GLOBAL_HISTORY_SIZE 100
+
+typedef struct
+{
+ int sensor;
+ int motion_trigger;
+ sensors_event_t data;
+}
+recorded_sample_t;
+
+/*
+ * 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 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 sensors_event_t* event)
+{
+ recorded_sample_t *cell;
+ int i;
+
+ /* Don't record duplicate samples, as they are not useful for filters */
+ if (sensor[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[s].selected_trigger == sensor[s].motion_trigger_name);
+
+ memcpy(&cell->data, event, sizeof(sensors_event_t));
+}