1 /******************************************************************
3 * Copyright 2014 Samsung Electronics All Rights Reserved.
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 ******************************************************************/
22 #include "proximity.h"
25 // Proximity code start
26 float CalculateExponentialAverage(int numberOfSamples, int *array, int startindex, int flag)
29 float denominator = 0;
33 if (flag < arraysize / RSSI_EA) // first loop buffer full
35 for (int i = 0; i < startindex; i++)
39 if (startindex == 0) {}
42 average = average / startindex;
44 // Serial.print("exp: ");
45 Serial.print("average1 : ");
46 Serial.println(average);
50 for (int i = 0; i < arraysize; i++)
55 for (int i = startindex; i < startindex + RSSI_EA; i++)
59 average = average / (arraysize - numberOfSamples);
60 // Serial.print("exp: ");
61 Serial.print("average2 : ");
62 Serial.println(average);
64 //exponential moving average
66 //CHANGE THIS FOR DIFFERENT SMOOTHING EFFECT
68 for (i = startindex + numberOfSamples - 1; i >= startindex; i--)
70 numerator += array[i] * pow(beta, startindex + numberOfSamples - i - 1);
71 denominator += pow(beta, startindex + numberOfSamples - i - 1);
77 numerator += average * pow(beta, offset + numberOfSamples);
78 denominator += pow(beta, offset + numberOfSamples);
80 return numerator / denominator;
84 float calculateDistance(float avgRSSI, float txPower)
91 float ratio = avgRSSI * 1.0 / txPower;
94 return pow(ratio, 10);
98 float distance = (0.7) * pow(ratio, 10) + 0.024;
103 // proximity code end