testIIRFilter.cpp
Go to the documentation of this file.
1 /* -*- coding:utf-8-unix; mode:c++; -*- */
2 
3 #include "IIRFilter.h"
4 /* samples */
5 #include <stdio.h>
6 #include <stdlib.h>
7 #include <iostream>
8 #include <vector>
9 #include <boost/shared_ptr.hpp>
10 #include <hrpUtil/Eigen3d.h>
11 
12 template <class T, class FT>
14 {
15 protected:
16  double dt; /* [s] */
17  double input_freq; /* [Hz] */
20  void fprintf_value (FILE* fp, const double _time, const T& _input, const T& _output);
21  void fprintf_plot (FILE* gp_pos);
22  void gen_pattern_and_plot (const std::vector<double>& time_vec, const std::vector<T>& input_vec)
23  {
24  std::string fname("/tmp/plot-iirfilter.dat");
25  FILE* fp = fopen(fname.c_str(), "w");
26  for (size_t i = 0; i < time_vec.size();i++) {
27  fprintf_value(fp, time_vec[i], input_vec[i], filter->passFilter(input_vec[i]));
28  }
29  fclose(fp);
30  if (use_gnuplot) {
31  // plot pos
32  FILE* gp_pos = popen("gnuplot", "w");
33  fprintf(gp_pos, "set xlabel 'Time [s]'\n");
34  fprintf(gp_pos, "set ylabel 'var []'\n");
35  fprintf_plot (gp_pos);
36  fflush(gp_pos);
37  double tmp;
38  std::cin >> tmp;
39  pclose(gp_pos);
40  }
41  };
42  T init_value ();
43  T test0_input_value (const size_t i);
44  double calc_sin_value (const size_t i) { return std::sin(2*M_PI*i*dt*input_freq); };
45 public:
46  std::vector<std::string> arg_strs;
47  testIIRFilter (const double _dt = 0.004) : dt(_dt), input_freq(1.0),
48  use_gnuplot(true) { initialize(); };
49  void initialize() {
50  filter = boost::shared_ptr<FT >(new FT (4.0, dt, init_value()));
51  }
52  void test0 ()
53  {
54  std::cerr << "test0 : test" << std::endl;
55  parse_params();
56  double tm = 0.0, total_tm = 4.0;
57  std::vector<double> time_vec;
58  std::vector<T> input_vec;
59  for (size_t i = 0; i < static_cast<size_t>(total_tm/dt);i++) {
60  time_vec.push_back(tm);
61  input_vec.push_back(test0_input_value(i));
62  tm += dt;
63  }
64  gen_pattern_and_plot (time_vec,input_vec);
65  };
66  void parse_params ()
67  {
68  for (int i = 0; i < arg_strs.size(); ++ i) {
69  if ( arg_strs[i]== "--use-gnuplot" ) {
70  if (++i < arg_strs.size()) use_gnuplot = (arg_strs[i]=="true");
71  } else if ( arg_strs[i]== "--cutoff-freq" ) {
72  if (++i < arg_strs.size()) filter->setCutOffFreq(atof(arg_strs[i].c_str()));
73  } else if ( arg_strs[i]== "--input-freq" ) {
74  if (++i < arg_strs.size()) input_freq = atof(arg_strs[i].c_str());
75  }
76  }
77  std::cerr << "[testIIRFilter] params" << std::endl;
78  std::cerr << "[testIIRFilter] dt = " << dt << "[s], cutoff-freq = " << filter->getCutOffFreq() << "[Hz], input-freq = " << input_freq << "[Hz]" << std::endl;
79  };
80 };
81 
82 // Specialization for double
83 template<> void testIIRFilter<double, FirstOrderLowPassFilter<double> >::fprintf_value (FILE* fp, const double _time, const double& _input, const double& _output)
84 {
85  fprintf(fp, "%f %f %f\n", _time, _input, _output);
86 };
90 {
91  fprintf(gp_pos, "plot '/tmp/plot-iirfilter.dat' using 1:2 with lines title 'input' lw 4, '/tmp/plot-iirfilter.dat' using 1:3 with lines title 'filtered' lw 3\n");
92 };
93 
94 // Specialization for hrp::Vector3
95 template<> void testIIRFilter<hrp::Vector3, FirstOrderLowPassFilter<hrp::Vector3> >::fprintf_value (FILE* fp, const double _time, const hrp::Vector3& _input, const hrp::Vector3& _output)
96 {
97  fprintf(fp, "%f %f %f %f %f %f %f\n", _time, _input[0], _output[0], _input[1], _output[1], _input[2], _output[2]);
98 };
101 {
102  double tmp = calc_sin_value(i);
103  return hrp::Vector3(tmp, 2*tmp, -0.5*tmp);
104 };
106 {
107  fprintf(gp_pos, "plot '/tmp/plot-iirfilter.dat' using 1:2 with lines title 'input (0)' lw 4, '/tmp/plot-iirfilter.dat' using 1:3 with lines title 'filtered (0)' lw 3,");
108  fprintf(gp_pos, "'/tmp/plot-iirfilter.dat' using 1:4 with lines title 'input (1)' lw 4, '/tmp/plot-iirfilter.dat' using 1:5 with lines title 'filtered (1)' lw 3,");
109  fprintf(gp_pos, "'/tmp/plot-iirfilter.dat' using 1:6 with lines title 'input (2)' lw 4, '/tmp/plot-iirfilter.dat' using 1:7 with lines title 'filtered (2)' lw 3\n");
110 };
113 #if 0 // use obsolated method
114  int filter_dim = 0;
115  std::vector<double> fb_coeffs, ff_coeffs;
116  filter_dim = 2;
117  fb_coeffs.resize(filter_dim+1);
118  fb_coeffs[0] = 1.00000;
119  fb_coeffs[1] = 1.88903;
120  fb_coeffs[2] =-0.89487;
121  ff_coeffs.resize(filter_dim+1);
122  ff_coeffs[0] = 0.0014603;
123  ff_coeffs[1] = 0.0029206;
124  ff_coeffs[2] = 0.0014603;
125  filter = boost::shared_ptr<IIRFilter >(new IIRFilter (filter_dim, fb_coeffs, ff_coeffs));
126 #else
128  int dim = 2;
129  std::vector<double> A(dim+1);
130  std::vector<double> B(dim+1);
131  // octave
132  // [B, A] = butter(2, 0.004 * 2 * 8) ;;; 2 * dt * cutoff_freq
133  //b =
134  //0.00882608666843131 0.01765217333686262 0.00882608666843131
135  //a =
136  //1.000000000000000 -1.717211834908084 0.752516181581809
137  A[0] = 1.000000000000000;
138  A[1] = -1.717211834908084;
139  A[2] = 0.752516181581809;
140  B[0] = 0.00882608666843131;
141  B[1] = 0.01765217333686262;
142  B[2] = 0.00882608666843131;
143  filter->setParameter(dim, A, B);
144 #endif
145 };
147  for (int i = 0; i < arg_strs.size(); ++ i) {
148  if ( arg_strs[i]== "--use-gnuplot" ) {
149  if (++i < arg_strs.size()) use_gnuplot = (arg_strs[i]=="true");
150 #if 0
151  } else if ( arg_strs[i]== "--cutoff-freq" ) {
152  if (++i < arg_strs.size()) filter->setCutOffFreq(atof(arg_strs[i].c_str()));
153 #endif
154  } else if ( arg_strs[i]== "--input-freq" ) {
155  if (++i < arg_strs.size()) input_freq = atof(arg_strs[i].c_str());
156  }
157  }
158  std::cerr << "[testIIRFilter] params" << std::endl;
159  std::cerr << "[testIIRFilter] dt = " << dt << "[s], cutoff-freq = " << 8 << "[Hz], input-freq = " << input_freq << "[Hz]" << std::endl;
160 };
161 template<> void testIIRFilter<double, IIRFilter >::fprintf_value (FILE* fp, const double _time, const double& _input, const double& _output)
162 {
163  fprintf(fp, "%f %f %f\n", _time, _input, _output);
164 };
165 template<> double testIIRFilter<double, IIRFilter >::init_value () { return 0;};
166 template<> double testIIRFilter<double, IIRFilter >::test0_input_value (const size_t i) { return calc_sin_value(i);};
168 {
169  fprintf(gp_pos, "plot '/tmp/plot-iirfilter.dat' using 1:2 with lines title 'input' lw 4, '/tmp/plot-iirfilter.dat' using 1:3 with lines title 'filtered' lw 3\n");
170 };
171 
172 void print_usage ()
173 {
174  std::cerr << "Usage : testIIRFilter [mode] [test-name] [option]" << std::endl;
175  std::cerr << " [mode] should be: --double, --vector3" << std::endl;
176  std::cerr << " [test-name] should be:" << std::endl;
177  std::cerr << " --test0 : test" << std::endl;
178  std::cerr << " [option] should be:" << std::endl;
179 };
180 
181 int main(int argc, char* argv[])
182 {
183  int ret = 0;
184  if (argc >= 3) {
185  if (std::string(argv[1]) == "--double") {
187  for (int i = 2; i < argc; ++ i) {
188  tiir.arg_strs.push_back(std::string(argv[i]));
189  }
190  if (std::string(argv[2]) == "--test0") {
191  tiir.test0();
192  } else {
193  print_usage();
194  ret = 1;
195  }
196  } else if (std::string(argv[1]) == "--vector3") {
198  for (int i = 2; i < argc; ++ i) {
199  tiir.arg_strs.push_back(std::string(argv[i]));
200  }
201  if (std::string(argv[2]) == "--test0") {
202  tiir.test0();
203  } else {
204  print_usage();
205  ret = 1;
206  }
207  } else if (std::string(argv[1]) == "--iir") {
209  for (int i = 2; i < argc; ++ i) {
210  tiir.arg_strs.push_back(std::string(argv[i]));
211  }
212  if (std::string(argv[2]) == "--test0") {
213  tiir.test0();
214  } else {
215  print_usage();
216  ret = 1;
217  }
218  } else {
219  print_usage();
220  ret = 1;
221  }
222  } else {
223  print_usage();
224  ret = 1;
225  }
226  return ret;
227 }
228 
void gen_pattern_and_plot(const std::vector< double > &time_vec, const std::vector< T > &input_vec)
void print_usage()
boost::shared_ptr< FT > filter
RTC::ReturnCode_t ret(RTC::Local::ReturnCode_t r)
void fprintf_value(FILE *fp, const double _time, const T &_input, const T &_output)
png_uint_32 i
Eigen::Vector3d Vector3
png_FILE_p fp
int main(int argc, char *argv[])
testIIRFilter(const double _dt=0.004)
FILE * popen(const char *cmd, const char *mode)
double calc_sin_value(const size_t i)
#define M_PI
T test0_input_value(const size_t i)
void fprintf_plot(FILE *gp_pos)
std::vector< std::string > arg_strs
void pclose(FILE *fd)


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