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 "MathUtils.h"
25 #include "TimeSeriesUtils.h"
27 
28 namespace armarx::math
29 {
31  {
32  }
33 
34  std::vector<float> TimeSeriesUtils::Resample(const std::vector<float>& timestamps, const std::vector<float>& data, const std::vector<float>& newTimestamps)
35  {
36  ARMARX_CHECK_EQUAL(data.size(), timestamps.size());
37  ARMARX_CHECK_EQUAL(data.size(), newTimestamps.size());
38  std::vector<float> result;
39  result.reserve(data.size());
40 
41  if (data.size() == 0)
42  {
43  return result;
44  }
45  if (data.size() == 1)
46  {
47  result.push_back(data.at(0));
48  return result;
49  }
50 
51  size_t i = 0;
52  size_t j = 0;
53 
54  while (j < data.size())
55  {
56  while (newTimestamps.at(j) > timestamps.at(i + 1) && i < data.size() - 2)
57  {
58  i++;
59  }
60  float f = math::MathUtils::ILerp(timestamps.at(i), timestamps.at(i + 1), newTimestamps.at(j));
61  result.push_back(math::MathUtils::LerpClamp(data.at(i), data.at(i + 1), f));
62  j++;
63  }
64 
65  return result;
66  }
67 
68  std::vector<float> TimeSeriesUtils::ApplyFilter(const std::vector<float>& data, const std::vector<float>& filter, BorderMode mode)
69  {
70  std::vector<float> result;
71  size_t start = filter.size() / 2;
72  for (size_t i = start; i < data.size() + start; i++)
73  {
74  float y = 0;
75  float w = 0;
76  for (size_t j = 0; j < filter.size(); j++)
77  {
78  int k = (int)i - (int)j;
79  if (k < 0)
80  {
81  k = 0;
82  }
83  if (k >= (int)data.size())
84  {
85  k = data.size() - 1;
86  }
87  y += data.at(k) * filter.at(j);
88  w += filter.at(j);
89  }
90  result.push_back(w == 0 ? 0 : y / w);
91  }
92  return result;
93  }
94 
95 
96  std::vector<float> TimeSeriesUtils::ApplyGaussianFilter(const std::vector<float>& data, float sigma, float sampleTime, BorderMode mode)
97  {
98  std::vector<float> filter = CreateGaussianFilter(sigma, sampleTime);
99  return ApplyFilter(data, filter, mode);
100  }
101 
102  std::vector<float> TimeSeriesUtils::CreateGaussianFilter(const float sigma, float sampleTime, float truncate)
103  {
104  std::vector<float> filter;
105  int range = (int)(truncate * sigma / sampleTime);
106  for (int i = -range; i <= range; i++)
107  {
108  float x = i * sampleTime;
109  filter.push_back(exp(-x * x / (2 * sigma * sigma) / (sigma * sqrt(2 * M_PI))));
110  }
111  return filter;
112  }
113 
114  std::vector<float> TimeSeriesUtils::MakeTimestamps(float start, float end, size_t count)
115  {
116  std::vector<float> result;
117  for (size_t i = 0; i < count; i++)
118  {
119  result.push_back(MathUtils::Lerp(start, end, (float)i / (float)(count - 1)));
120  }
121  return result;
122  }
123 }
GfxTL::sqrt
VectorXD< D, T > sqrt(const VectorXD< D, T > &a)
Definition: VectorXD.h:662
armarx::math::MathUtils::ILerp
static float ILerp(float a, float b, float f)
Definition: MathUtils.h:160
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:68
armarx::math::TimeSeriesUtils::TimeSeriesUtils
TimeSeriesUtils()
Definition: TimeSeriesUtils.cpp:30
armarx::math::TimeSeriesUtils::ApplyGaussianFilter
static std::vector< float > ApplyGaussianFilter(const std::vector< float > &data, float sigma, float sampleTime, BorderMode mode)
Definition: TimeSeriesUtils.cpp:96
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:114
armarx::math::TimeSeriesUtils::BorderMode
BorderMode
Definition: TimeSeriesUtils.h:37
ExpressionException.h
armarx::math::MathUtils::LerpClamp
static float LerpClamp(float a, float b, float f)
Definition: MathUtils.h:155
armarx::math
Definition: LinearizeAngularTrajectory.cpp:27
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:34
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:150
armarx::math::TimeSeriesUtils::CreateGaussianFilter
static std::vector< float > CreateGaussianFilter(const float sigma, float sampleTime, float truncate=4)
Definition: TimeSeriesUtils.cpp:102