mlp.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. /* Copyright (c) 2008-2011 Octasic Inc.
  2. Written by Jean-Marc Valin */
  3. /*
  4. Redistribution and use in source and binary forms, with or without
  5. modification, are permitted provided that the following conditions
  6. are met:
  7. - Redistributions of source code must retain the above copyright
  8. notice, this list of conditions and the following disclaimer.
  9. - Redistributions in binary form must reproduce the above copyright
  10. notice, this list of conditions and the following disclaimer in the
  11. documentation and/or other materials provided with the distribution.
  12. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  13. ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  14. LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  15. A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR
  16. CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  17. EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  18. PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  19. PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  20. LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  21. NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  22. SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  23. */
  24. #ifdef HAVE_CONFIG_H
  25. #include "config.h"
  26. #endif
  27. #include "opus_types.h"
  28. #include "opus_defines.h"
  29. #include <math.h>
  30. #include "mlp.h"
  31. #include "arch.h"
  32. #include "tansig_table.h"
  33. #define MAX_NEURONS 100
  34. #if 0
  35. static OPUS_INLINE opus_val16 tansig_approx(opus_val32 _x) /* Q19 */
  36. {
  37. int i;
  38. opus_val16 xx; /* Q11 */
  39. /*double x, y;*/
  40. opus_val16 dy, yy; /* Q14 */
  41. /*x = 1.9073e-06*_x;*/
  42. if (_x>=QCONST32(8,19))
  43. return QCONST32(1.,14);
  44. if (_x<=-QCONST32(8,19))
  45. return -QCONST32(1.,14);
  46. xx = EXTRACT16(SHR32(_x, 8));
  47. /*i = lrint(25*x);*/
  48. i = SHR32(ADD32(1024,MULT16_16(25, xx)),11);
  49. /*x -= .04*i;*/
  50. xx -= EXTRACT16(SHR32(MULT16_16(20972,i),8));
  51. /*x = xx*(1./2048);*/
  52. /*y = tansig_table[250+i];*/
  53. yy = tansig_table[250+i];
  54. /*y = yy*(1./16384);*/
  55. dy = 16384-MULT16_16_Q14(yy,yy);
  56. yy = yy + MULT16_16_Q14(MULT16_16_Q11(xx,dy),(16384 - MULT16_16_Q11(yy,xx)));
  57. return yy;
  58. }
  59. #else
  60. /*extern const float tansig_table[501];*/
  61. static OPUS_INLINE float tansig_approx(float x)
  62. {
  63. int i;
  64. float y, dy;
  65. float sign=1;
  66. /* Tests are reversed to catch NaNs */
  67. if (!(x<8))
  68. return 1;
  69. if (!(x>-8))
  70. return -1;
  71. #ifndef FIXED_POINT
  72. /* Another check in case of -ffast-math */
  73. if (celt_isnan(x))
  74. return 0;
  75. #endif
  76. if (x<0)
  77. {
  78. x=-x;
  79. sign=-1;
  80. }
  81. i = (int)floor(.5f+25*x);
  82. x -= .04f*i;
  83. y = tansig_table[i];
  84. dy = 1-y*y;
  85. y = y + x*dy*(1 - y*x);
  86. return sign*y;
  87. }
  88. #endif
  89. #if 0
  90. void mlp_process(const MLP *m, const opus_val16 *in, opus_val16 *out)
  91. {
  92. int j;
  93. opus_val16 hidden[MAX_NEURONS];
  94. const opus_val16 *W = m->weights;
  95. /* Copy to tmp_in */
  96. for (j=0;j<m->topo[1];j++)
  97. {
  98. int k;
  99. opus_val32 sum = SHL32(EXTEND32(*W++),8);
  100. for (k=0;k<m->topo[0];k++)
  101. sum = MAC16_16(sum, in[k],*W++);
  102. hidden[j] = tansig_approx(sum);
  103. }
  104. for (j=0;j<m->topo[2];j++)
  105. {
  106. int k;
  107. opus_val32 sum = SHL32(EXTEND32(*W++),14);
  108. for (k=0;k<m->topo[1];k++)
  109. sum = MAC16_16(sum, hidden[k], *W++);
  110. out[j] = tansig_approx(EXTRACT16(PSHR32(sum,17)));
  111. }
  112. }
  113. #else
  114. void mlp_process(const MLP *m, const float *in, float *out)
  115. {
  116. int j;
  117. float hidden[MAX_NEURONS];
  118. const float *W = m->weights;
  119. /* Copy to tmp_in */
  120. for (j=0;j<m->topo[1];j++)
  121. {
  122. int k;
  123. float sum = *W++;
  124. for (k=0;k<m->topo[0];k++)
  125. sum = sum + in[k]**W++;
  126. hidden[j] = tansig_approx(sum);
  127. }
  128. for (j=0;j<m->topo[2];j++)
  129. {
  130. int k;
  131. float sum = *W++;
  132. for (k=0;k<m->topo[1];k++)
  133. sum = sum + hidden[k]**W++;
  134. out[j] = tansig_approx(sum);
  135. }
  136. }
  137. #endif