X-Git-Url: http://git.osdn.net/view?a=blobdiff_plain;f=transform.c;h=8b525d177122f5efd7d92c73e735a8fbedda8176;hb=b24d6584ce3c44cdb5ba9ebe6aa525ff7ecd477c;hp=be4a8f122e19007921b50276583869219cfbef5c;hpb=85c42117b76d73d435cea6980ca03228129439dd;p=android-x86%2Fhardware-intel-libsensors.git diff --git a/transform.c b/transform.c index be4a8f1..8b525d1 100644 --- a/transform.c +++ b/transform.c @@ -12,6 +12,7 @@ #include "description.h" #include "transform.h" #include "utils.h" +#include "filtering.h" /*----------------------------------------------------------------------------*/ @@ -198,10 +199,10 @@ static void denoise (struct sensor_info_t* si, struct sensors_event_t* data, */ int i; - float total; int f; int sampling_rate = (int) si->sampling_rate; int history_size; + int history_full = 0; /* Don't denoise anything if we have less than two samples per second */ if (sampling_rate < 2) @@ -220,32 +221,43 @@ static void denoise (struct sensor_info_t* si, struct sensors_event_t* data, si->history_index = 0; si->history = (float*) realloc(si->history, si->history_size * num_fields * sizeof(float)); + if (si->history) { + si->history_sum = (float*) realloc(si->history_sum, + num_fields * sizeof(float)); + if (si->history_sum) + memset(si->history_sum, 0, num_fields * sizeof(float)); + } } - if (!si->history) + if (!si->history || !si->history_sum) return; /* Unlikely, but still... */ /* Update initialized samples count */ if (si->history_entries < si->history_size) si->history_entries++; + else + history_full = 1; - /* Record new sample */ - for (f=0; f < num_fields; f++) - si->history[si->history_index * num_fields + f] = data->data[f]; - - /* Update our rolling index (next evicted cell) */ - si->history_index = (si->history_index + 1) % si->history_size; - - /* For now simply compute a mobile mean for each field */ + /* Record new sample and calculate the moving sum */ for (f=0; f < num_fields; f++) { - total = 0; + /** + * A field is going to be overwritten if + * history is full, so decrease the history sum + */ + if (history_full) + si->history_sum[f] -= + si->history[si->history_index * num_fields + f]; - for (i=0; i < si->history_entries; i++) - total += si->history[i * num_fields + f]; + si->history[si->history_index * num_fields + f] = data->data[f]; + si->history_sum[f] += data->data[f]; - /* Output filtered data */ - data->data[f] = total / si->history_entries; + /* For now simply compute a mobile mean for each field */ + /* and output filtered data */ + data->data[f] = si->history_sum[f] / si->history_entries; } + + /* Update our rolling index (next evicted cell) */ + si->history_index = (si->history_index + 1) % si->history_size; } @@ -260,6 +272,8 @@ static int finalize_sample_default(int s, struct sensors_event_t* data) switch (sensor_type) { case SENSOR_TYPE_ACCELEROMETER: + /* Always consider the accelerometer accurate */ + data->acceleration.status = SENSOR_STATUS_ACCURACY_HIGH; if (sensor_info[s].quirks & QUIRK_NOISY) denoise(&sensor_info[s], data, 3, 20); break; @@ -272,9 +286,10 @@ static int finalize_sample_default(int s, struct sensors_event_t* data) case SENSOR_TYPE_GYROSCOPE: case SENSOR_TYPE_GYROSCOPE_UNCALIBRATED: - calibrate_gyro(data, &sensor_info[s]); + if (!(sensor_info[s].quirks & QUIRK_TERSE_DRIVER)) + calibrate_gyro(data, &sensor_info[s]); if (sensor_info[s].quirks & QUIRK_NOISY) - denoise(&sensor_info[s], data, 3, 20); + denoise_median(data, &sensor_info[s]); break; case SENSOR_TYPE_LIGHT: