Imported Upstream version 0.9.2
[platform/upstream/iotivity.git] / service / soft-sensor-manager / SoftSensorPlugin / IndoorTrajectorySensor / src / lib_proximity.cpp
1 /******************************************************************
2 *
3 * Copyright 2014 Samsung Electronics All Rights Reserved.
4 *
5 *
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 *      http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 *
19 ******************************************************************/
20
21 #include <stdio.h>
22 #include "tizen_log.h"
23 #include "lib_proximity.h"
24 #include <math.h>
25
26 // Proximity code start
27 float CalculateExponentialAverage(int numberOfSamples, int *array, int startindex, int flag)
28 {
29     float numerator = 0;
30     float denominator = 0;
31
32     float average = 0.0;
33
34     if (flag < arraysize / RSSI_EA)   // first loop buffer full
35     {
36
37         for (int i = 0; i < startindex; i++)
38         {
39             average += array[i];
40         }
41
42         if (startindex == 0) {}
43         else
44         {
45             average = average / startindex;
46         }
47         //       DBG("exp: ");
48         DBG("average1 : %f", average);
49     }
50     else
51     {
52         for (int i = 0; i < arraysize; i++)
53         {
54             average += array[i];
55         }
56
57         for (int i = startindex; i < startindex + RSSI_EA; i++)
58         {
59             average -= array[i];
60         }
61
62         average = average / (arraysize - numberOfSamples);
63
64         //            DBG("exp: ");
65         DBG("average2 : %f", average);
66     }
67
68     //exponential moving average
69     int i = 0;
70     //CHANGE THIS FOR DIFFERENT SMOOTHING EFFECT
71     float beta = 0.8f;
72     for (i = startindex + numberOfSamples - 1; i >= startindex; i--)
73     {
74         double temp = pow(beta, startindex + numberOfSamples - i - 1);
75         numerator += array[i] * (float)temp;
76         denominator += (float)temp;
77     }
78
79     int offset = 3;
80     if (average != 0.0)
81     {
82         numerator += average * pow(beta, offset + numberOfSamples);
83         denominator += pow(beta, offset + numberOfSamples);
84     }
85
86     return numerator / denominator;
87 }
88
89
90 float calculateDistance(float avgRSSI, float txPower)
91 {
92     if (avgRSSI == 0)
93     {
94         return -1.0;
95     }
96
97     float ratio = avgRSSI * 1.0 / txPower;
98     if (ratio < 1.0)
99     {
100         return pow(ratio, 10);
101     }
102     else
103     {
104         float distance =  (0.7) * pow(ratio, 10) + 0.024;
105         return distance;
106     }
107 }
108
109 // proximity code end
110