TorqueFilter/IIRFilter.h
Go to the documentation of this file.
1 // -*- C++ -*-
2 
11 #ifndef IIR_FILTER_H
12 #define IIR_FILTER_H
13 
14 #include <vector>
15 #include <deque>
16 #include <string>
17 #include <cmath>
18 #include <iostream>
19 
24 class IIRFilter
25 {
26 public:
30  IIRFilter(const std::string& error_prefix = "");
37  IIRFilter(unsigned int dim, std::vector<double>& fb_coeffs, std::vector<double>& ff_coeffs, const std::string& error_prefix = "");
41  ~IIRFilter() {};
42 
52  bool setParameter(int dim, std::vector<double>& A, std::vector<double>& B);
53 
60  bool setParameterAsBiquad(const double f_cutoff, const double Q, const double hz);
61 
64  void getParameter(int &dim, std::vector<double>&A, std::vector<double>& B);
65 
68  void reset(double initial_input = 0.0);
69 
73  double executeFilter(double input) {
74  // std::cerr << "executeFilter will be obsolated." << std::endl;
75  return passFilter(input);
76  };
80  double passFilter(double input);
81  // double getCurrentValue () const { return m_prev_output; };
82 private:
83  // Configuration variable declaration
84  // <rtc-template block="config_declare">
86  std::vector<double> m_fb_coefficients; // fb parameters (dim must be m_dimension + 1, m_fb_coefficients[0] would be 1.0)
87  std::vector<double> m_ff_coefficients; // ff parameters (dim must be m_dimension + 1)
88  std::deque<double> m_previous_values;
89  // double m_prev_output;
91  std::string m_error_prefix;
92 };
93 
97 template <class T> class FirstOrderLowPassFilter
98 {
99 private:
101  double cutoff_freq, dt, const_param;
102 public:
103  FirstOrderLowPassFilter (const double _cutoff_freq, const double _dt, const T init_value) : prev_value(init_value), dt(_dt)
104  {
105  setCutOffFreq(_cutoff_freq);
106  };
108  {
109  };
110  T passFilter (const T& value)
111  {
112  prev_value = 1.0/(1+const_param) * prev_value + const_param/(1+const_param) * value;
113  return prev_value;
114  };
115  void reset (const T& value) { prev_value = value; };
116  void setCutOffFreq (const double f)
117  {
118  cutoff_freq = f;
119  const_param = 2 * M_PI * cutoff_freq * dt;
120  };
121  double getCutOffFreq () const { return cutoff_freq; };
122  T getCurrentValue () const { return prev_value; };
123 };
124 
125 #endif // IIRFilter_H
bool setParameterAsBiquad(const double f_cutoff, const double Q, const double hz)
Simple user interface of setParameter.
void setCutOffFreq(const double f)
std::vector< double > m_ff_coefficients
double executeFilter(double input)
Execute filtering, this method will be obsolated.
std::string m_error_prefix
double passFilter(double input)
passFilter
bool setParameter(int dim, std::vector< double > &A, std::vector< double > &B)
Set parameters Y[n] = B[0] * X[n] + B[1] * X[n-1] + ... + B[dim] * X[n-dim] - A[1] * Y[n-1] ...
~IIRFilter()
Destructor.
void reset(double initial_input=0.0)
FirstOrderLowPassFilter(const double _cutoff_freq, const double _dt, const T init_value)
std::vector< double > m_fb_coefficients
void getParameter(int &dim, std::vector< double > &A, std::vector< double > &B)
#define M_PI
IIRFilter(const std::string &error_prefix="")
Constructor.
std::deque< double > m_previous_values


hrpsys
Author(s): AIST, Fumio Kanehiro
autogenerated on Thu May 6 2021 02:41:50