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. this->v_upperBounds = *_upperBounds;
  21. }
  22. QuantizationNDAequiDist0ToMax::~QuantizationNDAequiDist0ToMax()
  23. {
  24. }
  25. double QuantizationNDAequiDist0ToMax::getPrototype ( uint _bin,
  26. const uint & _dim
  27. ) const
  28. {
  29. return (this->v_upperBounds[_dim]*_bin) / (double)(this->ui_numBins-1);
  30. }
  31. uint QuantizationNDAequiDist0ToMax::quantize ( double _value,
  32. const uint & _dim
  33. ) const
  34. {
  35. if ( _value <= 0.0 )
  36. return 0;
  37. else if ( _value >= this->v_upperBounds[_dim] )
  38. return this->ui_numBins-1;
  39. else
  40. return (uint)( _value/this->v_upperBounds[_dim] * (this->ui_numBins-1) + 0.5 );
  41. }
  42. virtual void Quantization1DAequiDist0ToMax::computeParametersFromData ( const NICE::FeatureMatrix * _fm )
  43. {
  44. double vmax = _fm->getLargestValue();
  45. this->upperBound.resize ( 1 );
  46. this->upperBound ( 0 ) = vmax;
  47. double d_quantile ( 0.99 );
  48. this->upperBound = this->fmk->getLargestValuePerDimension( d_quantile );
  49. }
  50. // ---------------------- STORE AND RESTORE FUNCTIONS ----------------------
  51. void QuantizationNDAequiDist0ToMax::restore ( std::istream & _is,
  52. int _format
  53. )
  54. {
  55. if ( _is.good() )
  56. {
  57. std::string tmp;
  58. _is >> tmp; //class name
  59. if ( ! this->isStartTag( tmp, "Quantization1DAequiDist0ToMax" ) )
  60. {
  61. std::cerr << " WARNING - attempt to restore Quantization1DAequiDist0ToMax, but start flag " << tmp << " does not match! Aborting... " << std::endl;
  62. throw;
  63. }
  64. bool b_endOfBlock ( false ) ;
  65. while ( !b_endOfBlock )
  66. {
  67. _is >> tmp; // start of block
  68. if ( this->isEndTag( tmp, "QuantizationNDAequiDist0ToMax" ) )
  69. {
  70. b_endOfBlock = true;
  71. continue;
  72. }
  73. tmp = this->removeStartTag ( tmp );
  74. if ( tmp.compare("Quantization") == 0 )
  75. {
  76. // restore parent object
  77. Quantization::restore( _is );
  78. }
  79. else
  80. {
  81. std::cerr << "WARNING -- unexpected QuantizationNDAequiDist0ToMax object -- " << tmp << " -- for restoration... aborting" << std::endl;
  82. throw;
  83. }
  84. //FIXME also store and restore the upper bounds
  85. // _is >> tmp; // end of block
  86. // tmp = this->removeEndTag ( tmp );
  87. }
  88. }
  89. else
  90. {
  91. std::cerr << "QuantizationNDAequiDist0ToMax::restore -- InStream not initialized - restoring not possible!" << std::endl;
  92. }
  93. }
  94. void QuantizationNDAequiDist0ToMax::store ( std::ostream & _os,
  95. int _format
  96. ) const
  97. {
  98. // show starting point
  99. _os << this->createStartTag( "QuantizationNDAequiDist0ToMax" ) << std::endl;
  100. // store parent object
  101. Quantization::store( _os );
  102. // done
  103. _os << this->createEndTag( "QuantizationNDAequiDist0ToMax" ) << std::endl;
  104. }