Franky 0.12.0
A High-Level Motion API for Franka
Loading...
Searching...
No Matches
median_filter.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <Eigen/Dense>
4#include <algorithm>
5
6namespace franky {
7
8template <size_t dims>
10 public:
11 explicit MedianFilter(size_t window_size)
12 : window_size_(window_size),
13 current_index_(0),
14 current_size_(0),
15 window_(Eigen::Matrix<double, dims, Eigen::Dynamic>::Zero(dims, window_size)) {
16 if (window_size <= 0) throw std::invalid_argument("Window size must be positive.");
17 }
18
19 MedianFilter(const MedianFilter&) = default;
20
21 void add(const Eigen::Vector<double, dims>& value) {
22 window_.col(current_index_) = value;
23 current_index_ = (current_index_ + 1) % window_size_;
24 if (current_size_ < window_size_) ++current_size_;
25 }
26
27 Eigen::Vector<double, dims> current_value() const {
28 Eigen::Vector<double, dims> median;
29
30 for (size_t d = 0; d < dims; ++d) {
31 Eigen::VectorXd values = window_.row(d).head(current_size_).transpose();
32 std::nth_element(values.data(), values.data() + current_size_ / 2, values.data() + current_size_);
33 median(d) = values(current_size_ / 2);
34 }
35
36 return median;
37 }
38
39 Eigen::Vector<double, dims> operator()(const Eigen::Vector<double, dims>& value) {
40 add(value);
41 return current_value();
42 }
43
44 private:
45 size_t window_size_;
46 size_t current_index_;
47 size_t current_size_;
48 Eigen::Matrix<double, dims, Eigen::Dynamic> window_;
49};
50
51} // namespace franky
Definition median_filter.hpp:9
MedianFilter(size_t window_size)
Definition median_filter.hpp:11
Eigen::Vector< double, dims > operator()(const Eigen::Vector< double, dims > &value)
Definition median_filter.hpp:39
void add(const Eigen::Vector< double, dims > &value)
Definition median_filter.hpp:21
Eigen::Vector< double, dims > current_value() const
Definition median_filter.hpp:27
MedianFilter(const MedianFilter &)=default
Definition dynamics_limit.cpp:8