JASPL  0.2
Just Another Signal Processing Library
jplot.h
1 #ifndef JPLOT_H
2 #define JPLOT_H
3 
4 //C System-Headers
5 #include <cstdarg>
6 //C++ System headers
7 #include <utility>
8 //OpenCL Headers
9 
10 //Boost Headers
11 
12 //GnuPlot Headers
13 #include "gnuplot-iostream.h"
14 //Project specific headers
15 #include "../JASPL/jTypeTraits/jtypetraits.h"
16 
17 namespace jaspl {
18 
19 template < typename T >
20 class Plotter {
21 
22  typedef std::pair< T, std::string > series_pair;
23 
24  public:
25  Plotter() {
26  static_assert(is_stdlib_container< T >::value,
27  "Can only plot container-like objects.");
28  static_assert(std::is_arithmetic< typename T::value_type >::value,
29  "Can only plot arithmetic types.");
30  }
31 
32  ~Plotter();
33 
34  void Plot() {
35  //
36  }
37 
38  void SetXAxisLabel( std::string x_label ) {
39  //
40  }
41 
42  void SetYAxisLabel( std::string y_label ) {
43 
44  }
45 
46  void SetSavePath( std::string file_path ) {
47 
48  }
49 
50  void AddSeries( T series, std::string title ) {
51  series_list.push_back( series_pair( series, title ) );
52  }
53 
54  void SetMaxNumPoints( uint max_points ) {
55  //
56  }
57 
58  private:
59 
60  T decimate( const T& series, uint desired_points ) {
61  uint pivot = static_cast<uint> (series.size() / desired_points);
62 
63  std::vector<uint> pivot_indices;
64 
65  for( uint i = 0 ; i < series.size() ; i ++ ) {
66  if ( i%pivot == 0 ) {
67  pivot_indices.push_back(i);
68  }
69  }
70 
71  T y_vals;
72 
73  for ( const auto& idx : pivot_indices ) {
74  y_vals.push_back( series.at(idx) );
75  }
76  }
77 
78  std::string save_file_path;
79  std::string x_label;
80  std::string y_label;
81  uint max_num_points;
82 
83  std::list< series_pair > series_list;
84  Gnuplot gp;
85 };
86 
87 template <typename T> void plot( T signal ) {
88 
89  static_assert(is_stdlib_container< T >::value,
90  "Can only plot container-like objects.");
91  static_assert(std::is_arithmetic< typename T::value_type >::value,
92  "Can only plot arithmetic types.");
93 
94  Gnuplot gp;
95 
96  gp << "plot '-' with lines notitle\n";
97  gp.send1d(signal);
98 }
99 
100 template <class T> void plot ( T signal, uint num_plot_points ) {
101 
102  static_assert(is_stdlib_container< T >::value,
103  "Can only plot container-like objects.");
104  static_assert(std::is_arithmetic< typename T::value_type >::value,
105  "Can only plot arithmetic types.");
106 
107  Gnuplot gp;
108 
109  uint pivot = static_cast<uint> (signal.size() / num_plot_points);
110 
111  std::vector<uint> pivot_indices;
112 
113  for( uint i = 0 ; i < signal.size() ; i ++ ) {
114  if ( i%pivot == 0 ) {
115  pivot_indices.push_back(i);
116  }
117  }
118 
119  T y_vals;
120 
121  for ( const auto& idx : pivot_indices ) {
122  y_vals.push_back( signal.at(idx) );
123  }
124 
125  gp << "plot '-' with lines notitle\n";
126  gp.send1d(y_vals);
127 }
128 
129 
130 //friend function of JVector
131 template <class T> void plot ( T& signal, std::string plot_title ) {
132 
133  static_assert(is_stdlib_container< T >::value,
134  "Can only plot container-like objects.");
135  static_assert(std::is_arithmetic< typename T::value_type >::value,
136  "Can only plot arithmetic types.");
137 
138  Gnuplot gp;
139 
140  gp << "set title '"+plot_title+"'\n";
141  gp << "plot '-' with lines notitle\n";
142  gp.send1d(signal);
143 }
144 
145 template <class T>
146 void plot_to_disk ( T& signal_a,
147  std::string signal_a_title,
148  std::string x_axis_label,
149  std::string y_axis_label,
150  std::string plot_title,
151  std::string save_file_path ) {
152 
153  static_assert(is_stdlib_container< T >::value,
154  "Can only plot container-like objects.");
155  static_assert(std::is_arithmetic< typename T::value_type >::value,
156  "Can only plot arithmetic types.");
157 
158  Gnuplot gp;
159 
160  std::string out_file_path = save_file_path + plot_title + ".png";
161 
162  gp << "set term png transparent truecolor size 1280,720\n";
163  gp << " set border lw 3 lc rgb 'yellow' " << std::endl;
164  gp << " set key font ', 20' textcolor rgb 'yellow' " << std::endl;
165  gp << "set output '"+out_file_path+"'\n";
166  gp << "set xlabel font ', 20' " << std::endl;
167  gp << "set ylabel font ', 20' " << std::endl;
168  gp << "set xlabel '" + x_axis_label + "' tc rgb 'yellow' " << std::endl;
169  gp << "set ylabel '" + y_axis_label + "' tc rgb 'yellow' " << std::endl;
170  gp << "set xtics font ', 20' tc rgb 'yellow'" << std::endl;
171  gp << "set ytics font ', 20' tc rgb 'yellow'" << std::endl;
172  gp << "set title '"+plot_title+"' font 'arial,30' tc rgb 'yellow' \n";
173  gp << "plot" << gp.file1d( signal_a ) << "with lines linewidth 5 title '" + signal_a_title + "'" << std::endl;
174 
175 }
176 
177 template <class T>
178 void plot_to_disk ( T& signal_a,
179  T& signal_b,
180  std::string signal_a_title,
181  std::string signal_b_title,
182  std::string x_axis_label,
183  std::string y_axis_label,
184  std::string plot_title,
185  std::string save_file_path ) {
186 
187  static_assert(is_stdlib_container< T >::value,
188  "Can only plot container-like objects.");
189  static_assert(std::is_arithmetic< typename T::value_type >::value,
190  "Can only plot arithmetic types.");
191 
192  Gnuplot gp;
193 
194  std::string out_file_path = save_file_path + plot_title + ".png";
195 
196  gp << "set term png transparent truecolor size 1280,720\n";
197  gp << " set border lw 3 lc rgb 'yellow' " << std::endl;
198  gp << " set key font ', 20' textcolor rgb 'yellow' " << std::endl;
199  gp << "set output '"+out_file_path+"'\n";
200  gp << "set xlabel font ', 20' " << std::endl;
201  gp << "set ylabel font ', 20' " << std::endl;
202  gp << "set xlabel '" + x_axis_label + "' tc rgb 'yellow' " << std::endl;
203  gp << "set ylabel '" + y_axis_label + "' tc rgb 'yellow' " << std::endl;
204  gp << "set xtics font ', 20' tc rgb 'yellow'" << std::endl;
205  gp << "set ytics font ', 20' tc rgb 'yellow'" << std::endl;
206  gp << "set title '"+plot_title+"' font 'arial,30' tc rgb 'yellow' \n";
207  gp << "plot" << gp.file1d( signal_a ) << "with lines linewidth 5 title '" + signal_a_title + "',";
208  gp << gp.file1d( signal_b ) << "with lines linewidth 5 title '" + signal_b_title +"'" << std::endl;
209 
210 }
211 
212 
213 template <class T> void plot ( T& signal, std::string plot_title, uint num_plot_points ) {
214 
215  static_assert(is_stdlib_container< T >::value,
216  "Can only plot container-like objects.");
217  static_assert(std::is_arithmetic< typename T::value_type >::value,
218  "Can only plot arithmetic types.");
219 
220  Gnuplot gp;
221 
222  uint pivot = static_cast<uint> (signal.size() / num_plot_points);
223 
224  std::vector<uint> pivot_indices;
225 
226  for( uint i = 0 ; i < signal.size() ; i ++ ) {
227  if ( i%pivot == 0 ) {
228  pivot_indices.push_back(i);
229  }
230  }
231 
232  T y_vals;
233 
234  for ( const auto& idx : pivot_indices ) {
235  y_vals.push_back( signal.at(idx) );
236  }
237 
238  gp << "set title '"+plot_title+"'\n";
239  gp << "plot '-' with lines notitle\n";
240  gp.send1d(y_vals);
241 }
242 
243 }
244 
245 #endif // JPLOT_H