QuantizationNDAequiDist0ToMax.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. /**
  2. * @file QuantizationNDAequiDist0ToMax.cpp
  3. * @brief QuantizationNDAequiDist0ToMax of one-dimensional signals with selectable interval [0, vMax] (Implementation)
  4. * @author Alexander Freytag
  5. * @date 13-10-2015 ( dd-mm-yyyy )
  6. */
  7. #include <iostream>
  8. #include "QuantizationNDAequiDist0ToMax.h"
  9. using namespace NICE;
  10. QuantizationNDAequiDist0ToMax::QuantizationNDAequiDist0ToMax( )
  11. {
  12. this->ui_numBins = 1;
  13. }
  14. QuantizationNDAequiDist0ToMax::QuantizationNDAequiDist0ToMax(
  15. uint _numBins,
  16. NICE::Vector * _upperBounds
  17. )
  18. {
  19. this->ui_numBins = _numBins;
  20. if ( (_upperBounds != NULL) && (_upperBounds->size() > 0) )
  21. this->v_upperBounds = (*_upperBounds);
  22. else
  23. this->v_upperBounds = NICE::Vector( 1 );
  24. }
  25. QuantizationNDAequiDist0ToMax::~QuantizationNDAequiDist0ToMax()
  26. {
  27. }
  28. double QuantizationNDAequiDist0ToMax::getPrototype ( uint _bin,
  29. const uint & _dim
  30. ) const
  31. {
  32. return (this->v_upperBounds[_dim]*_bin) / (double)(this->ui_numBins-1);
  33. }
  34. uint QuantizationNDAequiDist0ToMax::quantize ( double _value,
  35. const uint & _dim
  36. ) const
  37. {
  38. if ( _value <= 0.0 )
  39. return 0;
  40. else if ( _value >= this->v_upperBounds[_dim] )
  41. return this->ui_numBins-1;
  42. else
  43. return (uint)( _value/this->v_upperBounds[_dim] * (this->ui_numBins-1) + 0.5 );
  44. }
  45. void QuantizationNDAequiDist0ToMax::computeParametersFromData ( const NICE::FeatureMatrix * _fm )
  46. {
  47. // 100% quantile...
  48. double d_quantile ( 1.00 );
  49. std::cerr << "QuantizationNDAequiDist0ToMax::computeParametersFromData -- d_quantile: " << d_quantile << std::endl;
  50. this->v_upperBounds = _fm->getLargestValuePerDimension( d_quantile );
  51. std::cerr << " computed upper bounds: " << this->v_upperBounds << std::endl;
  52. }
  53. // ---------------------- STORE AND RESTORE FUNCTIONS ----------------------
  54. void QuantizationNDAequiDist0ToMax::restore ( std::istream & _is,
  55. int _format
  56. )
  57. {
  58. if ( _is.good() )
  59. {
  60. std::string tmp;
  61. _is >> tmp; //class name
  62. if ( ! this->isStartTag( tmp, "Quantization1DAequiDist0ToMax" ) )
  63. {
  64. std::cerr << " WARNING - attempt to restore Quantization1DAequiDist0ToMax, but start flag " << tmp << " does not match! Aborting... " << std::endl;
  65. throw;
  66. }
  67. bool b_endOfBlock ( false ) ;
  68. while ( !b_endOfBlock )
  69. {
  70. _is >> tmp; // start of block
  71. if ( this->isEndTag( tmp, "QuantizationNDAequiDist0ToMax" ) )
  72. {
  73. b_endOfBlock = true;
  74. continue;
  75. }
  76. tmp = this->removeStartTag ( tmp );
  77. if ( tmp.compare("Quantization") == 0 )
  78. {
  79. // restore parent object
  80. Quantization::restore( _is );
  81. }
  82. else
  83. {
  84. std::cerr << "WARNING -- unexpected QuantizationNDAequiDist0ToMax object -- " << tmp << " -- for restoration... aborting" << std::endl;
  85. throw;
  86. }
  87. //FIXME also store and restore the upper bounds
  88. // _is >> tmp; // end of block
  89. // tmp = this->removeEndTag ( tmp );
  90. }
  91. }
  92. else
  93. {
  94. std::cerr << "QuantizationNDAequiDist0ToMax::restore -- InStream not initialized - restoring not possible!" << std::endl;
  95. }
  96. }
  97. void QuantizationNDAequiDist0ToMax::store ( std::ostream & _os,
  98. int _format
  99. ) const
  100. {
  101. // show starting point
  102. _os << this->createStartTag( "QuantizationNDAequiDist0ToMax" ) << std::endl;
  103. // store parent object
  104. Quantization::store( _os );
  105. // done
  106. _os << this->createEndTag( "QuantizationNDAequiDist0ToMax" ) << std::endl;
  107. }