lut.hpp 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. /******************************************************************************
  2. from url: http://www.mochima.com/articles/LUT/LUT.html
  3. File: LUT.H
  4. Author: Carlos Moreno
  5. Date: March 1999
  6. Description:
  7. Template class definitions for Look-Up Table objects (LUT_number to
  8. encapsulate LUT'd floating-point multiplications, and LUT_function
  9. to encapsulate LUT'd functions evaluation)
  10. ******************************************************************************/
  11. #ifndef __LUT_H__
  12. #define __LUT_H__
  13. /*****************************************************************************
  14. LUT_number definition
  15. Template parameters:
  16. TResult:
  17. type parameter indicating the data type of the result of the multiplication
  18. LBound:
  19. Lower bound for the integer operand (multiplier)
  20. UBound:
  21. Upper bound for the integer operand (multiplier)
  22. ScaleFactor:
  23. The result of the multiplication is scaled by this factor (in case you want
  24. to provide the result as an integer, you can reduce the effect of the
  25. rounding error by specifying a high scale factor)
  26. ******************************************************************************/
  27. template <int LBound = -128, int UBound = 127, class TResult = double,
  28. int ScaleFactor = 1>
  29. class LUT_number
  30. {
  31. public:
  32. explicit LUT_number (double val = 0)
  33. {
  34. lut = lut_array - LBound;
  35. for (int i = LBound; i <= UBound; i++)
  36. {
  37. lut[i] = i * ScaleFactor * val;
  38. }
  39. }
  40. const TResult & operator* (int val) const
  41. {
  42. return lut[val];
  43. }
  44. private:
  45. TResult lut_array[UBound - LBound + 1];
  46. TResult * lut;
  47. };
  48. template <int LBound, int UBound, class TResult, int ScaleFactor>
  49. inline const TResult & operator* (int val, const LUT_number<LBound,
  50. UBound, TResult, ScaleFactor> & coeff)
  51. {
  52. return coeff * val;
  53. }
  54. /*******************************************************************************
  55. LUT_function definition
  56. Template parameters:
  57. TResult:
  58. Type parameter indicating the data type of the return value of the function
  59. LBound:
  60. Lower bound for the integer argument
  61. UBound:
  62. Upper bound for the integer argument
  63. TArg:
  64. Type parameter indicating the data type of the parameter
  65. (argument) of the function used to "initialize" the LUT
  66. ******************************************************************************/
  67. template <int LBound = -128, int UBound = 127, class TResult = double,
  68. class TArg = double>
  69. class LUT_function
  70. {
  71. public:
  72. explicit LUT_function (TResult (*f) (TArg), double coeff = 1)
  73. {
  74. lut = lut_array - LBound;
  75. for (int i = LBound; i <= UBound; i++)
  76. {
  77. lut[i] = f(coeff * i);
  78. }
  79. }
  80. const TResult & operator()(int i) const
  81. {
  82. if (i<UBound) {
  83. if (i>LBound) {
  84. return lut[i];
  85. } else return lut[LBound];
  86. } else return lut[UBound];
  87. }
  88. private:
  89. TResult lut_array[UBound - LBound + 1];
  90. TResult * lut;
  91. };
  92. #endif