TimeSeriesUtils.cpp
Go to the documentation of this file.
1 /*
2  * This file is part of ArmarX.
3  *
4  * Copyright (C) 2012-2016, High Performance Humanoid Technologies (H2T),
5  * Karlsruhe Institute of Technology (KIT), all rights reserved.
6  *
7  * ArmarX is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License version 2 as
9  * published by the Free Software Foundation.
10  *
11  * ArmarX is distributed in the hope that it will be useful, but
12  * WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program. If not, see <http://www.gnu.org/licenses/>.
18  *
19  * @author ()
20  * @copyright http://www.gnu.org/licenses/gpl-2.0.txt
21  * GNU General Public License
22  */
23 
24 #include "TimeSeriesUtils.h"
25 
27 
28 #include "MathUtils.h"
29 
30 namespace armarx::math
31 {
33  {
34  }
35 
36  std::vector<float>
37  TimeSeriesUtils::Resample(const std::vector<float>& timestamps,
38  const std::vector<float>& data,
39  const std::vector<float>& newTimestamps)
40  {
41  ARMARX_CHECK_EQUAL(data.size(), timestamps.size());
42  ARMARX_CHECK_EQUAL(data.size(), newTimestamps.size());
43  std::vector<float> result;
44  result.reserve(data.size());
45 
46  if (data.size() == 0)
47  {
48  return result;
49  }
50  if (data.size() == 1)
51  {
52  result.push_back(data.at(0));
53  return result;
54  }
55 
56  size_t i = 0;
57  size_t j = 0;
58 
59  while (j < data.size())
60  {
61  while (newTimestamps.at(j) > timestamps.at(i + 1) && i < data.size() - 2)
62  {
63  i++;
64  }
65  float f =
66  math::MathUtils::ILerp(timestamps.at(i), timestamps.at(i + 1), newTimestamps.at(j));
67  result.push_back(math::MathUtils::LerpClamp(data.at(i), data.at(i + 1), f));
68  j++;
69  }
70 
71  return result;
72  }
73 
74  std::vector<float>
75  TimeSeriesUtils::ApplyFilter(const std::vector<float>& data,
76  const std::vector<float>& filter,
77  BorderMode mode)
78  {
79  std::vector<float> result;
80  size_t start = filter.size() / 2;
81  for (size_t i = start; i < data.size() + start; i++)
82  {
83  float y = 0;
84  float w = 0;
85  for (size_t j = 0; j < filter.size(); j++)
86  {
87  int k = (int)i - (int)j;
88  if (k < 0)
89  {
90  k = 0;
91  }
92  if (k >= (int)data.size())
93  {
94  k = data.size() - 1;
95  }
96  y += data.at(k) * filter.at(j);
97  w += filter.at(j);
98  }
99  result.push_back(w == 0 ? 0 : y / w);
100  }
101  return result;
102  }
103 
104  std::vector<float>
105  TimeSeriesUtils::ApplyGaussianFilter(const std::vector<float>& data,
106  float sigma,
107  float sampleTime,
108  BorderMode mode)
109  {
110  std::vector<float> filter = CreateGaussianFilter(sigma, sampleTime);
111  return ApplyFilter(data, filter, mode);
112  }
113 
114  std::vector<float>
115  TimeSeriesUtils::CreateGaussianFilter(const float sigma, float sampleTime, float truncate)
116  {
117  std::vector<float> filter;
118  int range = (int)(truncate * sigma / sampleTime);
119  for (int i = -range; i <= range; i++)
120  {
121  float x = i * sampleTime;
122  filter.push_back(exp(-x * x / (2 * sigma * sigma) / (sigma * sqrt(2 * M_PI))));
123  }
124  return filter;
125  }
126 
127  std::vector<float>
128  TimeSeriesUtils::MakeTimestamps(float start, float end, size_t count)
129  {
130  std::vector<float> result;
131  for (size_t i = 0; i < count; i++)
132  {
133  result.push_back(MathUtils::Lerp(start, end, (float)i / (float)(count - 1)));
134  }
135  return result;
136  }
137 } // namespace armarx::math
armarx::math::MathUtils::ILerp
static float ILerp(float a, float b, float f)
Definition: MathUtils.h:197
MathUtils.h
armarx::math::TimeSeriesUtils::ApplyFilter
static std::vector< float > ApplyFilter(const std::vector< float > &data, const std::vector< float > &filter, BorderMode mode)
Definition: TimeSeriesUtils.cpp:75
armarx::math::TimeSeriesUtils::TimeSeriesUtils
TimeSeriesUtils()
Definition: TimeSeriesUtils.cpp:32
armarx::math::TimeSeriesUtils::ApplyGaussianFilter
static std::vector< float > ApplyGaussianFilter(const std::vector< float > &data, float sigma, float sampleTime, BorderMode mode)
Definition: TimeSeriesUtils.cpp:105
M_PI
#define M_PI
Definition: MathTools.h:17
data
uint8_t data[1]
Definition: EtherCATFrame.h:68
armarx::math::TimeSeriesUtils::MakeTimestamps
static std::vector< float > MakeTimestamps(float start, float end, size_t count)
Definition: TimeSeriesUtils.cpp:128
armarx::math::TimeSeriesUtils::BorderMode
BorderMode
Definition: TimeSeriesUtils.h:37
ExpressionException.h
GfxTL::sqrt
VectorXD< D, T > sqrt(const VectorXD< D, T > &a)
Definition: VectorXD.h:704
armarx::math::MathUtils::LerpClamp
static float LerpClamp(float a, float b, float f)
Definition: MathUtils.h:191
armarx::math
Definition: LinearizeAngularTrajectory.cpp:28
armarx::math::TimeSeriesUtils::Resample
static std::vector< float > Resample(const std::vector< float > &timestamps, const std::vector< float > &data, const std::vector< float > &newTimestamps)
Definition: TimeSeriesUtils.cpp:37
TimeSeriesUtils.h
ARMARX_CHECK_EQUAL
#define ARMARX_CHECK_EQUAL(lhs, rhs)
This macro evaluates whether lhs is equal (==) rhs and if it turns out to be false it will throw an E...
Definition: ExpressionException.h:130
armarx::math::MathUtils::Lerp
static float Lerp(float a, float b, float f)
Definition: MathUtils.h:185
armarx::math::TimeSeriesUtils::CreateGaussianFilter
static std::vector< float > CreateGaussianFilter(const float sigma, float sampleTime, float truncate=4)
Definition: TimeSeriesUtils.cpp:115