lpc.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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. #include "lpc.h"
  44. #ifdef BFIN_ASM
  45. #include "lpc_bfin.h"
  46. #endif
  47. /* LPC analysis
  48. *
  49. * The next two functions calculate linear prediction coefficients
  50. * and/or the related reflection coefficients from the first P_MAX+1
  51. * values of the autocorrelation function.
  52. */
  53. /* Invented by N. Levinson in 1947, modified by J. Durbin in 1959.
  54. */
  55. /* returns minimum mean square error */
  56. spx_word32_t _spx_lpc(
  57. spx_coef_t *lpc, /* out: [0...p-1] LPC coefficients */
  58. const spx_word16_t *ac, /* in: [0...p] autocorrelation values */
  59. int p
  60. )
  61. {
  62. int i, j;
  63. spx_word16_t r;
  64. spx_word16_t error = ac[0];
  65. if (ac[0] == 0)
  66. {
  67. for (i = 0; i < p; i++)
  68. lpc[i] = 0;
  69. return 0;
  70. }
  71. for (i = 0; i < p; i++) {
  72. /* Sum up this iteration's reflection coefficient */
  73. spx_word32_t rr = NEG32(SHL32(EXTEND32(ac[i + 1]),13));
  74. for (j = 0; j < i; j++)
  75. rr = SUB32(rr,MULT16_16(lpc[j],ac[i - j]));
  76. #ifdef FIXED_POINT
  77. r = DIV32_16(rr+PSHR32(error,1),ADD16(error,8));
  78. #else
  79. r = rr/(error+.003*ac[0]);
  80. #endif
  81. /* Update LPC coefficients and total error */
  82. lpc[i] = r;
  83. for (j = 0; j < i>>1; j++)
  84. {
  85. spx_word16_t tmp = lpc[j];
  86. lpc[j] = MAC16_16_P13(lpc[j],r,lpc[i-1-j]);
  87. lpc[i-1-j] = MAC16_16_P13(lpc[i-1-j],r,tmp);
  88. }
  89. if (i & 1)
  90. lpc[j] = MAC16_16_P13(lpc[j],lpc[j],r);
  91. error = SUB16(error,MULT16_16_Q13(r,MULT16_16_Q13(error,r)));
  92. }
  93. return error;
  94. }
  95. #ifdef FIXED_POINT
  96. /* Compute the autocorrelation
  97. * ,--,
  98. * ac(i) = > x(n) * x(n-i) for all n
  99. * `--'
  100. * for lags between 0 and lag-1, and x == 0 outside 0...n-1
  101. */
  102. #ifndef OVERRIDE_SPEEX_AUTOCORR
  103. void _spx_autocorr(
  104. const spx_word16_t *x, /* in: [0...n-1] samples x */
  105. spx_word16_t *ac, /* out: [0...lag-1] ac values */
  106. int lag,
  107. int n
  108. )
  109. {
  110. spx_word32_t d;
  111. int i, j;
  112. spx_word32_t ac0=1;
  113. int shift, ac_shift;
  114. for (j=0;j<n;j++)
  115. ac0 = ADD32(ac0,SHR32(MULT16_16(x[j],x[j]),8));
  116. ac0 = ADD32(ac0,n);
  117. shift = 8;
  118. while (shift && ac0<0x40000000)
  119. {
  120. shift--;
  121. ac0 <<= 1;
  122. }
  123. ac_shift = 18;
  124. while (ac_shift && ac0<0x40000000)
  125. {
  126. ac_shift--;
  127. ac0 <<= 1;
  128. }
  129. for (i=0;i<lag;i++)
  130. {
  131. d=0;
  132. for (j=i;j<n;j++)
  133. {
  134. d = ADD32(d,SHR32(MULT16_16(x[j],x[j-i]), shift));
  135. }
  136. ac[i] = SHR32(d, ac_shift);
  137. }
  138. }
  139. #endif
  140. #else
  141. /* Compute the autocorrelation
  142. * ,--,
  143. * ac(i) = > x(n) * x(n-i) for all n
  144. * `--'
  145. * for lags between 0 and lag-1, and x == 0 outside 0...n-1
  146. */
  147. void _spx_autocorr(
  148. const spx_word16_t *x, /* in: [0...n-1] samples x */
  149. float *ac, /* out: [0...lag-1] ac values */
  150. int lag,
  151. int n
  152. )
  153. {
  154. float d;
  155. int i;
  156. while (lag--)
  157. {
  158. for (i = lag, d = 0; i < n; i++)
  159. d += x[i] * x[i-lag];
  160. ac[lag] = d;
  161. }
  162. ac[0] += 10;
  163. }
  164. #endif