jack2 codebase
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

148 lines
4.1KB

  1. /*
  2. Copyright (C) 2006-2008 Grame
  3. This program is free software; you can redistribute it and/or modify
  4. it under the terms of the GNU Lesser General Public License as published by
  5. the Free Software Foundation; either version 2.1 of the License, or
  6. (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU Lesser General Public License for more details.
  11. You should have received a copy of the GNU Lesser General Public License
  12. along with this program; if not, write to the Free Software
  13. Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  14. */
  15. #include "JackGnuPlotMonitor.h"
  16. #include "JackError.h"
  17. using namespace std;
  18. namespace Jack {
  19. template <class T>
  20. JackGnuPlotMonitor<T>::JackGnuPlotMonitor(uint32_t measure_cnt, uint32_t measure_points, std::string name)
  21. {
  22. jack_log ( "JackGnuPlotMonitor::JackGnuPlotMonitor %u measure points - %u measures", measure_points, measure_cnt );
  23. fMeasureCnt = measure_cnt;
  24. fMeasurePoints = measure_points;
  25. fTablePos = 0;
  26. fName = name;
  27. fCurrentMeasure = new T[fMeasurePoints];
  28. fMeasureTable = new T*[fMeasureCnt];
  29. for ( uint32_t cnt = 0; cnt < fMeasureCnt; cnt++ )
  30. {
  31. fMeasureTable[cnt] = new T[fMeasurePoints];
  32. fill_n ( fMeasureTable[cnt], fMeasurePoints, 0 );
  33. }
  34. }
  35. template <class T>
  36. JackGnuPlotMonitor<T>::~JackGnuPlotMonitor()
  37. {
  38. jack_log ( "JackGnuPlotMonitor::~JackGnuPlotMonitor" );
  39. for ( uint32_t cnt = 0; cnt < fMeasureCnt; cnt++ )
  40. delete[] fMeasureTable[cnt];
  41. delete[] fMeasureTable;
  42. delete[] fCurrentMeasure;
  43. }
  44. template <class T>
  45. T JackGnuPlotMonitor<T>::AddNew(T measure_point)
  46. {
  47. fMeasureId = 0;
  48. return fCurrentMeasure[fMeasureId++] = measure_point;
  49. }
  50. template <class T>
  51. uint32_t JackGnuPlotMonitor<T>::New()
  52. {
  53. return fMeasureId = 0;
  54. }
  55. template <class T>
  56. T JackGnuPlotMonitor<T>::Add(T measure_point)
  57. {
  58. return fCurrentMeasure[fMeasureId++] = measure_point;
  59. }
  60. template <class T>
  61. uint32_t JackGnuPlotMonitor<T>::AddLast(T measure_point)
  62. {
  63. fCurrentMeasure[fMeasureId] = measure_point;
  64. fMeasureId = 0;
  65. return Write();
  66. }
  67. template <class T>
  68. uint32_t JackGnuPlotMonitor<T>::Write()
  69. {
  70. for ( uint32_t point = 0; point < fMeasurePoints; point++ )
  71. fMeasureTable[fTablePos][point] = fCurrentMeasure[point];
  72. if ( ++fTablePos == fMeasureCnt )
  73. fTablePos = 0;
  74. return fTablePos;
  75. }
  76. template <class T>
  77. int JackGnuPlotMonitor<T>::Save(std::string name)
  78. {
  79. std::string filename = ( name.empty() ) ? fName : name;
  80. filename += ".log";
  81. jack_log ( "JackGnuPlotMonitor::Save filename %s", filename.c_str() );
  82. std::ofstream file ( filename.c_str() );
  83. for ( uint32_t cnt = 0; cnt < fMeasureCnt; cnt++ )
  84. {
  85. for ( uint32_t point = 0; point < fMeasurePoints; point++ )
  86. file << fMeasureTable[cnt][point] << " \t";
  87. file << std::endl;
  88. }
  89. file.close();
  90. return 0;
  91. }
  92. template <class T>
  93. int JackGnuPlotMonitor<T>::SetPlotFile(std::string* options_list, uint32_t options_number,
  94. std::string* field_names, uint32_t field_number,
  95. std::string name)
  96. {
  97. std::string title = ( name.empty() ) ? fName : name;
  98. std::string plot_filename = title + ".plt";
  99. std::string data_filename = title + ".log";
  100. std::ofstream file ( plot_filename.c_str() );
  101. file << "set multiplot" << std::endl;
  102. file << "set grid" << std::endl;
  103. file << "set title \"" << title << "\"" << std::endl;
  104. for ( uint32_t i = 0; i < options_number; i++ )
  105. file << options_list[i] << std::endl;
  106. file << "plot ";
  107. for ( uint32_t row = 1; row <= field_number; row++ )
  108. {
  109. file << "\"" << data_filename << "\" using " << row << " title \"" << field_names[row-1] << "\" with lines";
  110. file << ( ( row < field_number ) ? ", " : "\n" );
  111. }
  112. jack_log ( "JackGnuPlotMonitor::SetPlotFile - Save GnuPlot file to '%s'", plot_filename.c_str() );
  113. file.close();
  114. return 0;
  115. }
  116. } // end of namespace