Home | History | Annotate | Download | only in common
      1 /*
      2  * Copyright (C) 2016 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 //////////////////////////////////////////////////////////////////////////////
     18 /*
     19  * This function implements a diversity checker and stores diverse vectors into
     20  * a memory. We assume that the data is located on a sphere, and we use the
     21  * norm of the difference of two vectors to decide if the vectors are diverse
     22  * enough:
     23  *
     24  * k = norm( v1 - v2 )^2 < Threshold
     25  *
     26  * Hence when k < Threshold the data is not stored, because the vectors are too
     27  * similar. We store diverse vectors in memory and all new incoming vectors
     28  * are checked against the already stored data points.
     29  *
     30  * Furthermore we also check if k > max_distance, since that data is most likely
     31  * not located on a sphere anymore and indicates a disturbance. Finally we give
     32  * a "data is full" flag to indicate once the memory is full.
     33  * The diverse data can be used to improve sphere fit calibrations, ensuring
     34  * that the sphere is populated enough resulting in better fits.
     35  *
     36  * Memory is stored in an array initialized to length of
     37  * [THREE_AXIS_DATA_DIM * NUM_DIVERSE_VECTORS], this has been done to be
     38  * compatible with the full sphere fit algorithm.
     39  *
     40  * Notice, this function stops to check if data is diverse, once the memory is
     41  * full. This has been done in order to save processing power.
     42  */
     43 
     44 #ifndef LOCATION_LBS_CONTEXTHUB_NANOAPPS_CALIBRATION_COMMON_DIVERSITY_CHECKER_H_
     45 #define LOCATION_LBS_CONTEXTHUB_NANOAPPS_CALIBRATION_COMMON_DIVERSITY_CHECKER_H_
     46 
     47 #include <stdbool.h>
     48 #include <stddef.h>
     49 #include <stdint.h>
     50 
     51 #ifdef __cplusplus
     52 extern "C" {
     53 #endif
     54 
     55 #define THREE_AXIS_DATA_DIM (3)   // data is three-dimensional.
     56 #define NUM_DIVERSE_VECTORS (20)  // Storing 20 data points.
     57 
     58 // Main data struct.
     59 struct DiversityChecker {
     60   // Data memory.
     61   float diverse_data[THREE_AXIS_DATA_DIM * NUM_DIVERSE_VECTORS];
     62 
     63   // Number of data points in the memory.
     64   size_t num_points;
     65 
     66   // Number of data points that violated the max_distance condition.
     67   size_t num_max_dist_violations;
     68 
     69   // Threshold value that is used to check k against.
     70   float threshold;
     71 
     72   // Threshold tuning paramter used to calculate threshold (k_algo):
     73   // threshold = threshold_tuning_param_sq * (local_field)^2.
     74   float threshold_tuning_param_sq;
     75 
     76   // Maximum distance value.
     77   float max_distance;
     78 
     79   // Max Distance tuning parameter:
     80   // max_distance = max_distance_tuning_param_sq * (local_field)^2.
     81   float max_distance_tuning_param_sq;
     82 
     83   // Data full bit.
     84   bool data_full;
     85 
     86   // Setup variables for NormQuality check.
     87 
     88   size_t min_num_diverse_vectors;
     89   size_t max_num_max_distance;
     90   float var_threshold;
     91   float max_min_threshold;
     92 };
     93 
     94 // Initialization of the function/struct, input:
     95 // min_num_diverse_vectors -> sets the gate for a minimum number of data points
     96 //                           in the memory
     97 // max_num_max_distance -> sets the value for a max distance violation number
     98 //                         gate.
     99 // var_threshold -> is a threshold value for a Norm variance gate.
    100 // max_min_threshold -> is a value for a gate that rejects Norm variations
    101 //                      that are larger than this number.
    102 // local_field -> is the assumed local_field (radius of the sphere).
    103 // threshold_tuning_param ->  threshold tuning parameter used to calculate
    104 //                            threshold (k_algo).
    105 // max_distance_tuning_param -> Max distance tuning parameter used to calculate
    106 //                             max_distance.
    107 void diversityCheckerInit(struct DiversityChecker* diverse_data,
    108                           size_t min_num_diverse_vectors,
    109                           size_t max_num_max_distance, float var_threshold,
    110                           float max_min_threshold, float local_field,
    111                           float threshold_tuning_param,
    112                           float max_distance_tuning_param);
    113 
    114 // Resetting the memory and the counters, leaves threshold and max_distance
    115 // as well as the setup variables for NormQuality check untouched.
    116 void diversityCheckerReset(struct DiversityChecker* diverse_data);
    117 
    118 // Main function. Tests the data (x,y,z) against the memory if diverse and
    119 // stores it, if so.
    120 void diversityCheckerUpdate(struct DiversityChecker* diverse_data, float x,
    121                             float y, float z);
    122 
    123 // Removing a constant bias from the diverse_data and check if the norm is
    124 // within a defined bound:
    125 // implemented 4 gates
    126 // -> needs a minimum number of data points in the memory
    127 //    (controlled by min_num_divers_vectors).
    128 // -> will return false if maximum number of max_distance is reached
    129 //    (controlled by max_num_max_distance).
    130 // -> norm must be within a var window.
    131 // -> norm must be within a MAX/MIN window.
    132 // Returned value will only be true if all 4 gates are passed.
    133 bool diversityCheckerNormQuality(struct DiversityChecker* diverse_data,
    134                                  float x_bias,
    135                                  float y_bias,
    136                                  float z_bias);
    137 
    138 // This function updates the threshold value and max distance value based on the
    139 // local field. This ensures a local field independent operation of the
    140 // diversity checker.
    141 //
    142 // threshold = (threshold_tuning_param * local_field)^2
    143 // max_distance = (max_distance_tuning_param * local_field)^2
    144 void diversityCheckerLocalFieldUpdate(struct DiversityChecker* diverse_data,
    145                                       float local_field);
    146 #ifdef __cplusplus
    147 }
    148 #endif
    149 
    150 #endif  // LOCATION_LBS_CONTEXTHUB_NANOAPPS_CALIBRATION_COMMON_DIVERSITY_CHECKER_H_
    151