QuantizationNDAequiDist0ToMax.cpp 3.9 KB

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