QuantizationNDAequiDist0ToMax.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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 static_cast<uint> ( floor( _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. this->v_upperBounds = _fm->getLargestValuePerDimension( d_quantile );
  50. }
  51. // ---------------------- STORE AND RESTORE FUNCTIONS ----------------------
  52. void QuantizationNDAequiDist0ToMax::restore ( std::istream & _is,
  53. int _format
  54. )
  55. {
  56. if ( _is.good() )
  57. {
  58. std::string tmp;
  59. _is >> tmp; //class name
  60. if ( ! this->isStartTag( tmp, "Quantization1DAequiDist0ToMax" ) )
  61. {
  62. std::cerr << " WARNING - attempt to restore Quantization1DAequiDist0ToMax, but start flag " << tmp << " does not match! Aborting... " << std::endl;
  63. throw;
  64. }
  65. bool b_endOfBlock ( false ) ;
  66. while ( !b_endOfBlock )
  67. {
  68. _is >> tmp; // start of block
  69. if ( this->isEndTag( tmp, "QuantizationNDAequiDist0ToMax" ) )
  70. {
  71. b_endOfBlock = true;
  72. continue;
  73. }
  74. tmp = this->removeStartTag ( tmp );
  75. if ( tmp.compare("Quantization") == 0 )
  76. {
  77. // restore parent object
  78. Quantization::restore( _is );
  79. }
  80. else
  81. {
  82. std::cerr << "WARNING -- unexpected QuantizationNDAequiDist0ToMax object -- " << tmp << " -- for restoration... aborting" << std::endl;
  83. throw;
  84. }
  85. //FIXME also store and restore the upper bounds
  86. // _is >> tmp; // end of block
  87. // tmp = this->removeEndTag ( tmp );
  88. }
  89. }
  90. else
  91. {
  92. std::cerr << "QuantizationNDAequiDist0ToMax::restore -- InStream not initialized - restoring not possible!" << std::endl;
  93. }
  94. }
  95. void QuantizationNDAequiDist0ToMax::store ( std::ostream & _os,
  96. int _format
  97. ) const
  98. {
  99. // show starting point
  100. _os << this->createStartTag( "QuantizationNDAequiDist0ToMax" ) << std::endl;
  101. // store parent object
  102. Quantization::store( _os );
  103. // done
  104. _os << this->createEndTag( "QuantizationNDAequiDist0ToMax" ) << std::endl;
  105. }