You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

141 lines
3.5KB

  1. #include <assert.h>
  2. #include <iostream>
  3. #include "LookupTable.h"
  4. #include "AudioMath.h"
  5. using namespace std;
  6. // test that we can call all the functions
  7. template<typename T>
  8. static void test0()
  9. {
  10. LookupTableParams<T> p;
  11. const int tableSize = 512;
  12. std::function<double(double)> f = [](double d) {
  13. return 0;
  14. };
  15. LookupTable<T>::init(p, tableSize, 0, 1, f);
  16. LookupTable<T>::lookup(p, 0);
  17. }
  18. // test that simple lookup works
  19. template<typename T>
  20. static void test1()
  21. {
  22. LookupTableParams<T> p;
  23. const int tableSize = 512;
  24. std::function<double(double)> f = [](double d) {
  25. return 100;
  26. };
  27. LookupTable<T>::init(p, tableSize, 0, 1, f);
  28. assert(LookupTable<T>::lookup(p, 0) == 100);
  29. assert(LookupTable<T>::lookup(p, 1) == 100);
  30. assert(LookupTable<T>::lookup(p, T(.342)) == 100);
  31. }
  32. // test that sin works
  33. template<typename T>
  34. static void test2()
  35. {
  36. LookupTableParams<T> p;
  37. const int tableSize = 512;
  38. std::function<double(double)> f = [](double d) {
  39. return std::sin(d);
  40. };
  41. LookupTable<T>::init(p, tableSize, 0, 1, f);
  42. const T tolerance = T(0.000001);
  43. for (double d = 0; d < 1; d += .0001) {
  44. T output = LookupTable<T>::lookup(p, T(d));
  45. const bool t = AudioMath::closeTo(output, std::sin(d), tolerance);
  46. if (!t) {
  47. cout << "failing with expected=" << std::sin(d) << " actual=" << output << " delta=" << std::abs(output - std::sin(d));
  48. assert(false);
  49. }
  50. }
  51. }
  52. // test that sin works on domain 10..32
  53. template<typename T>
  54. static void test3()
  55. {
  56. LookupTableParams<T> p;
  57. const int tableSize = 16;
  58. std::function<double(double)> f = [](double d) {
  59. const double s = (d - 10) / 3;
  60. return std::sin(s);
  61. };
  62. LookupTable<T>::init(p, tableSize, 10, 13, f);
  63. const T tolerance = T(0.01);
  64. for (double d = 10; d < 13; d += .0001) {
  65. const T output = LookupTable<T>::lookup(p, T(d));
  66. const T expected = (T) std::sin((d - 10.0) / 3);
  67. const bool t = AudioMath::closeTo(output, expected, tolerance);
  68. if (!t) {
  69. cout << "failing with d=" << d << " expected=" << expected << " actual=" << output << " delta=" << std::abs(output - std::sin(d));
  70. assert(false);
  71. }
  72. }
  73. }
  74. // test that sin at extremes works
  75. template<typename T>
  76. static void test4()
  77. {
  78. LookupTableParams<T> exponential;
  79. const T xMin = -5;
  80. const T xMax = 5;
  81. std::function<double(double)> expFunc = AudioMath::makeFunc_Exp(-5, 5, 2, 2000);
  82. LookupTable<T>::init(exponential, 128, -5, 5, expFunc);
  83. // Had to loosen tolerance to pass with windows gcc. Is there a problem
  84. // with precision, or is this expected with fast math?
  85. const T tolerance = T(0.0003);
  86. T outputLow = LookupTable<T>::lookup(exponential, xMin);
  87. T outputHigh = LookupTable<T>::lookup(exponential, xMax);
  88. bool t = AudioMath::closeTo(outputLow, 2, tolerance);
  89. if (!t) {
  90. cout << "failing l with expected=" << 2 << " actual=" << outputLow << " delta=" << std::abs(outputLow - 2) << std::endl;
  91. assert(false);
  92. }
  93. t = AudioMath::closeTo(outputHigh, 2000, tolerance);
  94. if (!t) {
  95. cout << "failing h with expected=" << 2000 << " actual=" << outputHigh << " delta=" << std::abs(outputHigh - 2000) << std::endl;
  96. assert(false);
  97. }
  98. }
  99. template<typename T>
  100. static void test()
  101. {
  102. test0<T>();
  103. test1<T>();
  104. test2<T>();
  105. test3<T>();
  106. test4<T>();
  107. }
  108. void testLookupTable()
  109. {
  110. test<float>();
  111. test<double>();
  112. }