lpc.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. /*
  2. Copyright 1992, 1993, 1994 by Jutta Degener and Carsten Bormann,
  3. Technische Universitaet Berlin
  4. Any use of this software is permitted provided that this notice is not
  5. removed and that neither the authors nor the Technische Universitaet Berlin
  6. are deemed to have made any representations as to the suitability of this
  7. software for any purpose nor are held responsible for any defects of
  8. this software. THERE IS ABSOLUTELY NO WARRANTY FOR THIS SOFTWARE.
  9. As a matter of courtesy, the authors request to be informed about uses
  10. this software has found, about bugs in this software, and about any
  11. improvements that may be of general interest.
  12. Berlin, 28.11.1994
  13. Jutta Degener
  14. Carsten Bormann
  15. Code modified by Jean-Marc Valin
  16. Speex License:
  17. Redistribution and use in source and binary forms, with or without
  18. modification, are permitted provided that the following conditions
  19. are met:
  20. - Redistributions of source code must retain the above copyright
  21. notice, this list of conditions and the following disclaimer.
  22. - Redistributions in binary form must reproduce the above copyright
  23. notice, this list of conditions and the following disclaimer in the
  24. documentation and/or other materials provided with the distribution.
  25. - Neither the name of the Xiph.org Foundation nor the names of its
  26. contributors may be used to endorse or promote products derived from
  27. this software without specific prior written permission.
  28. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  29. ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  30. LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  31. A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR
  32. CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  33. EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  34. PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  35. PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  36. LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  37. NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  38. SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  39. */
  40. #ifdef HAVE_CONFIG_H
  41. #include "config.h"
  42. #endif
  43. #ifndef DISABLE_ENCODER
  44. #include "lpc.h"
  45. #ifdef BFIN_ASM
  46. #include "lpc_bfin.h"
  47. #endif
  48. /* LPC analysis
  49. *
  50. * The next two functions calculate linear prediction coefficients
  51. * and/or the related reflection coefficients from the first P_MAX+1
  52. * values of the autocorrelation function.
  53. */
  54. /* Invented by N. Levinson in 1947, modified by J. Durbin in 1959.
  55. */
  56. /* returns minimum mean square error */
  57. spx_word32_t _spx_lpc(
  58. spx_coef_t *lpc, /* out: [0...p-1] LPC coefficients */
  59. const spx_word16_t *ac, /* in: [0...p] autocorrelation values */
  60. int p
  61. )
  62. {
  63. int i, j;
  64. spx_word16_t r;
  65. spx_word16_t error = ac[0];
  66. for (i = 0; i < p; i++) {
  67. /* Sum up this iteration's reflection coefficient */
  68. spx_word32_t rr = NEG32(SHL32(EXTEND32(ac[i + 1]),13));
  69. for (j = 0; j < i; j++)
  70. rr = SUB32(rr,MULT16_16(lpc[j],ac[i - j]));
  71. #ifdef FIXED_POINT
  72. r = DIV32_16(rr+PSHR32(error,1),ADD16(error,8));
  73. #else
  74. r = rr/(error+.003*ac[0]);
  75. #endif
  76. /* Update LPC coefficients and total error */
  77. lpc[i] = r;
  78. for (j = 0; j < (i+1)>>1; j++)
  79. {
  80. spx_word16_t tmp1, tmp2;
  81. /* It could be that j == i-1-j, in which case, we're updating the same value twice, which is OK */
  82. tmp1 = lpc[j];
  83. tmp2 = lpc[i-1-j];
  84. lpc[j] = MAC16_16_P13(tmp1,r,tmp2);
  85. lpc[i-1-j] = MAC16_16_P13(tmp2,r,tmp1);
  86. }
  87. error = SUB16(error,MULT16_16_Q13(r,MULT16_16_Q13(error,r)));
  88. }
  89. return error;
  90. }
  91. #ifdef FIXED_POINT
  92. /* Compute the autocorrelation
  93. * ,--,
  94. * ac(i) = > x(n) * x(n-i) for all n
  95. * `--'
  96. * for lags between 0 and lag-1, and x == 0 outside 0...n-1
  97. */
  98. #ifndef OVERRIDE_SPEEX_AUTOCORR
  99. void _spx_autocorr(
  100. const spx_word16_t *x, /* in: [0...n-1] samples x */
  101. spx_word16_t *ac, /* out: [0...lag-1] ac values */
  102. int lag,
  103. int n
  104. )
  105. {
  106. spx_word32_t d;
  107. int i, j;
  108. spx_word32_t ac0=1;
  109. int shift, ac_shift;
  110. for (j=0;j<n;j++)
  111. ac0 = ADD32(ac0,SHR32(MULT16_16(x[j],x[j]),8));
  112. ac0 = ADD32(ac0,n);
  113. shift = 8;
  114. while (shift && ac0<0x40000000)
  115. {
  116. shift--;
  117. ac0 <<= 1;
  118. }
  119. ac_shift = 18;
  120. while (ac_shift && ac0<0x40000000)
  121. {
  122. ac_shift--;
  123. ac0 <<= 1;
  124. }
  125. for (i=0;i<lag;i++)
  126. {
  127. d=0;
  128. for (j=i;j<n;j++)
  129. {
  130. d = ADD32(d,SHR32(MULT16_16(x[j],x[j-i]), shift));
  131. }
  132. ac[i] = SHR32(d, ac_shift);
  133. }
  134. }
  135. #endif
  136. #else
  137. /* Compute the autocorrelation
  138. * ,--,
  139. * ac(i) = > x(n) * x(n-i) for all n
  140. * `--'
  141. * for lags between 0 and lag-1, and x == 0 outside 0...n-1
  142. */
  143. void _spx_autocorr(
  144. const spx_word16_t *x, /* in: [0...n-1] samples x */
  145. float *ac, /* out: [0...lag-1] ac values */
  146. int lag,
  147. int n
  148. )
  149. {
  150. float d;
  151. int i;
  152. while (lag--)
  153. {
  154. for (i = lag, d = 0; i < n; i++)
  155. d += x[i] * x[i-lag];
  156. ac[lag] = d;
  157. }
  158. ac[0] += 10;
  159. }
  160. #endif
  161. #endif /* DISABLE_ENCODER */