Quantization.cpp 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. /**
  2. * @file Quantization.cpp
  3. * @brief Quantization of one-dimensional signals with a standard range of [0,1] (Implementation)
  4. * @author Erik Rodner, Alexander Freytag
  5. * @date 01/09/2012
  6. */
  7. #include <iostream>
  8. #include "Quantization.h"
  9. using namespace NICE;
  10. Quantization::Quantization( )
  11. {
  12. this->numBins = 1;
  13. }
  14. Quantization::Quantization( uint numBins )
  15. {
  16. this->numBins = numBins;
  17. }
  18. Quantization::~Quantization()
  19. {
  20. }
  21. uint Quantization::size() const
  22. {
  23. return numBins;
  24. }
  25. double Quantization::getPrototype (uint bin) const
  26. {
  27. // std::cerr << bin / (double)(numBins-1) << std::endl;
  28. return bin / (double)(numBins-1);
  29. }
  30. uint Quantization::quantize (double value) const
  31. {
  32. if ( value <= 0.0 ) return 0;
  33. else if ( value >= 1.0 ) return numBins-1;
  34. else return (uint)( value * (numBins-1) + 0.5 );
  35. }
  36. // ---------------------- STORE AND RESTORE FUNCTIONS ----------------------
  37. void Quantization::restore ( std::istream & is, int format )
  38. {
  39. if (is.good())
  40. {
  41. std::string tmp;
  42. bool b_endOfBlock ( false ) ;
  43. while ( !b_endOfBlock )
  44. {
  45. is >> tmp; // start of block
  46. if ( this->isEndTag( tmp, "Quantization" ) )
  47. {
  48. b_endOfBlock = true;
  49. continue;
  50. }
  51. tmp = this->removeStartTag ( tmp );
  52. if ( tmp.compare("numBins") == 0 )
  53. {
  54. is >> numBins;
  55. }
  56. else
  57. {
  58. std::cerr << "WARNING -- unexpected Quantization object -- " << tmp << " -- for restoration... aborting" << std::endl;
  59. throw;
  60. }
  61. is >> tmp; // end of block
  62. tmp = this->removeEndTag ( tmp );
  63. }
  64. }
  65. else
  66. {
  67. std::cerr << "Quantization::restore -- InStream not initialized - restoring not possible!" << std::endl;
  68. }
  69. }
  70. void Quantization::store ( std::ostream & os, int format ) const
  71. {
  72. // show starting point
  73. os << this->createStartTag( "Quantization" ) << std::endl;
  74. os << this->createStartTag( "numBins" ) << std::endl;
  75. os << numBins << std::endl;
  76. os << this->createEndTag( "numBins" ) << std::endl;
  77. // done
  78. os << this->createEndTag( "Quantization" ) << std::endl;
  79. }