Quantization.cpp 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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. return bin / (double)(numBins-1);
  28. }
  29. uint Quantization::quantize (double value) const
  30. {
  31. if ( value <= 0.0 ) return 0;
  32. else if ( value >= 1.0 ) return numBins-1;
  33. else return (uint)( value * (numBins-1) + 0.5 );
  34. }
  35. // ---------------------- STORE AND RESTORE FUNCTIONS ----------------------
  36. void Quantization::restore ( std::istream & is, int format )
  37. {
  38. if (is.good())
  39. {
  40. std::string tmp;
  41. bool b_endOfBlock ( false ) ;
  42. while ( !b_endOfBlock )
  43. {
  44. is >> tmp; // start of block
  45. if ( this->isEndTag( tmp, "Quantization" ) )
  46. {
  47. b_endOfBlock = true;
  48. continue;
  49. }
  50. tmp = this->removeStartTag ( tmp );
  51. if ( tmp.compare("numBins") == 0 )
  52. {
  53. is >> numBins;
  54. }
  55. else
  56. {
  57. std::cerr << "WARNING -- unexpected Quantization object -- " << tmp << " -- for restoration... aborting" << std::endl;
  58. throw;
  59. }
  60. is >> tmp; // end of block
  61. tmp = this->removeEndTag ( tmp );
  62. }
  63. }
  64. else
  65. {
  66. std::cerr << "Quantization::restore -- InStream not initialized - restoring not possible!" << std::endl;
  67. }
  68. }
  69. void Quantization::store ( std::ostream & os, int format ) const
  70. {
  71. // show starting point
  72. os << this->createStartTag( "Quantization" ) << std::endl;
  73. os << this->createStartTag( "numBins" ) << std::endl;
  74. os << numBins << std::endl;
  75. os << this->createEndTag( "numBins" ) << std::endl;
  76. // done
  77. os << this->createEndTag( "Quantization" ) << std::endl;
  78. }