sensord: enable samsung pedometer sensor for fused location
[platform/core/system/sensord.git] / src / sensor / pedometer / savitzky_golay_filter15.cpp
1 /*
2  *  Copyright (c) 2016-2017 Samsung Electronics Co., Ltd.
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 #include "savitzky_golay_filter15.h"
18
19 #include <stdlib.h>
20
21 /* length of filter. changing it requires changing coef_array! */
22 #define ARRAY_SIZE 15
23
24 /* sum of numerators of elements in coef_array. */
25 #define SUM_COEF 1105.0
26
27 /* array with coefficients for savitzky-golay filter. must be length of n! */
28 static double coef_array[] = {
29                 -78 / SUM_COEF, -13 / SUM_COEF, 42 / SUM_COEF, 87 / SUM_COEF,
30                 122 / SUM_COEF, 147 / SUM_COEF, 162 / SUM_COEF, 167 / SUM_COEF,
31                 162 / SUM_COEF, 147 / SUM_COEF, 122 / SUM_COEF, 87 / SUM_COEF,
32                 42 / SUM_COEF, -13 / SUM_COEF, -78 / SUM_COEF };
33
34 savitzky_golay_filter15::savitzky_golay_filter15()
35 : m_empty(true)
36 {
37         m_array = (double *)calloc(ARRAY_SIZE, sizeof(double));
38 }
39
40 savitzky_golay_filter15::~savitzky_golay_filter15()
41 {
42         if (m_array == NULL)
43                 return;
44
45         free(m_array);
46         m_array = NULL;
47 }
48
49 double savitzky_golay_filter15::filter(double value)
50 {
51         if (m_empty) {
52                 for (int i = 0; i < ARRAY_SIZE; i++)
53                         m_array[i] = value;
54                 m_empty = false;
55                 return value;
56         }
57
58         for (int i = 1; i < ARRAY_SIZE; i++)
59                 m_array[i - 1] = m_array[i];
60         m_array[ARRAY_SIZE - 1] = value;
61
62         double avrg = 0;
63         for (int i = 0; i < ARRAY_SIZE; i++)
64                 avrg += m_array[i] * coef_array[i];
65         return avrg;
66 }
67
68 void savitzky_golay_filter15::reset(void)
69 {
70         m_empty = true;
71 }