9 #include <boost/shared_ptr.hpp> 12 template <
class T,
class FT>
20 void fprintf_value (FILE* fp,
const double _time,
const T& _input,
const T& _output);
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]));
32 FILE* gp_pos =
popen(
"gnuplot",
"w");
33 fprintf(gp_pos,
"set xlabel 'Time [s]'\n");
34 fprintf(gp_pos,
"set ylabel 'var []'\n");
54 std::cerr <<
"test0 : test" << std::endl;
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);
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());
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;
85 fprintf(fp,
"%f %f %f\n", _time, _input, _output);
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");
97 fprintf(fp,
"%f %f %f %f %f %f %f\n", _time, _input[0], _output[0], _input[1], _output[1], _input[2], _output[2]);
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");
113 #if 0 // use obsolated method 115 std::vector<double> fb_coeffs, ff_coeffs;
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;
129 std::vector<double>
A(dim+1);
130 std::vector<double>
B(dim+1);
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);
151 }
else if (
arg_strs[
i]==
"--cutoff-freq" ) {
154 }
else if (
arg_strs[
i]==
"--input-freq" ) {
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;
163 fprintf(fp,
"%f %f %f\n", _time, _input, _output);
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");
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;
181 int main(
int argc,
char* argv[])
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]));
190 if (std::string(argv[2]) ==
"--test0") {
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]));
201 if (std::string(argv[2]) ==
"--test0") {
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]));
212 if (std::string(argv[2]) ==
"--test0") {
void gen_pattern_and_plot(const std::vector< double > &time_vec, const std::vector< T > &input_vec)
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)
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)
T test0_input_value(const size_t i)
void fprintf_plot(FILE *gp_pos)
std::vector< std::string > arg_strs