celt_lpc.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. /* Copyright (c) 2009-2010 Xiph.Org Foundation
  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 COPYRIGHT OWNER
  16. OR 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 "celt_lpc.h"
  28. #include "stack_alloc.h"
  29. #include "mathops.h"
  30. void _celt_lpc(
  31. opus_val16 *_lpc, /* out: [0...p-1] LPC coefficients */
  32. const opus_val32 *ac, /* in: [0...p] autocorrelation values */
  33. int p
  34. )
  35. {
  36. int i, j;
  37. opus_val32 r;
  38. opus_val32 error = ac[0];
  39. #ifdef FIXED_POINT
  40. opus_val32 lpc[LPC_ORDER];
  41. #else
  42. float *lpc = _lpc;
  43. #endif
  44. for (i = 0; i < p; i++)
  45. lpc[i] = 0;
  46. if (ac[0] != 0)
  47. {
  48. for (i = 0; i < p; i++) {
  49. /* Sum up this iteration's reflection coefficient */
  50. opus_val32 rr = 0;
  51. for (j = 0; j < i; j++)
  52. rr += MULT32_32_Q31(lpc[j],ac[i - j]);
  53. rr += SHR32(ac[i + 1],3);
  54. r = -frac_div32(SHL32(rr,3), error);
  55. /* Update LPC coefficients and total error */
  56. lpc[i] = SHR32(r,3);
  57. for (j = 0; j < (i+1)>>1; j++)
  58. {
  59. opus_val32 tmp1, tmp2;
  60. tmp1 = lpc[j];
  61. tmp2 = lpc[i-1-j];
  62. lpc[j] = tmp1 + MULT32_32_Q31(r,tmp2);
  63. lpc[i-1-j] = tmp2 + MULT32_32_Q31(r,tmp1);
  64. }
  65. error = error - MULT32_32_Q31(MULT32_32_Q31(r,r),error);
  66. /* Bail out once we get 30 dB gain */
  67. #ifdef FIXED_POINT
  68. if (error<SHR32(ac[0],10))
  69. break;
  70. #else
  71. if (error<.001f*ac[0])
  72. break;
  73. #endif
  74. }
  75. }
  76. #ifdef FIXED_POINT
  77. for (i=0;i<p;i++)
  78. _lpc[i] = ROUND16(lpc[i],16);
  79. #endif
  80. }
  81. void celt_fir(const opus_val16 *x,
  82. const opus_val16 *num,
  83. opus_val16 *y,
  84. int N,
  85. int ord,
  86. opus_val16 *mem)
  87. {
  88. int i,j;
  89. for (i=0;i<N;i++)
  90. {
  91. opus_val32 sum = SHL32(EXTEND32(x[i]), SIG_SHIFT);
  92. for (j=0;j<ord;j++)
  93. {
  94. sum += MULT16_16(num[j],mem[j]);
  95. }
  96. for (j=ord-1;j>=1;j--)
  97. {
  98. mem[j]=mem[j-1];
  99. }
  100. mem[0] = x[i];
  101. y[i] = ROUND16(sum, SIG_SHIFT);
  102. }
  103. }
  104. void celt_iir(const opus_val32 *x,
  105. const opus_val16 *den,
  106. opus_val32 *y,
  107. int N,
  108. int ord,
  109. opus_val16 *mem)
  110. {
  111. int i,j;
  112. for (i=0;i<N;i++)
  113. {
  114. opus_val32 sum = x[i];
  115. for (j=0;j<ord;j++)
  116. {
  117. sum -= MULT16_16(den[j],mem[j]);
  118. }
  119. for (j=ord-1;j>=1;j--)
  120. {
  121. mem[j]=mem[j-1];
  122. }
  123. mem[0] = ROUND16(sum,SIG_SHIFT);
  124. y[i] = sum;
  125. }
  126. }
  127. void _celt_autocorr(
  128. const opus_val16 *x, /* in: [0...n-1] samples x */
  129. opus_val32 *ac, /* out: [0...lag-1] ac values */
  130. const opus_val16 *window,
  131. int overlap,
  132. int lag,
  133. int n
  134. )
  135. {
  136. opus_val32 d;
  137. int i;
  138. VARDECL(opus_val16, xx);
  139. SAVE_STACK;
  140. ALLOC(xx, n, opus_val16);
  141. celt_assert(n>0);
  142. celt_assert(overlap>=0);
  143. for (i=0;i<n;i++)
  144. xx[i] = x[i];
  145. for (i=0;i<overlap;i++)
  146. {
  147. xx[i] = MULT16_16_Q15(x[i],window[i]);
  148. xx[n-i-1] = MULT16_16_Q15(x[n-i-1],window[i]);
  149. }
  150. #ifdef FIXED_POINT
  151. {
  152. opus_val32 ac0=0;
  153. int shift;
  154. for(i=0;i<n;i++)
  155. ac0 += SHR32(MULT16_16(xx[i],xx[i]),9);
  156. ac0 += 1+n;
  157. shift = celt_ilog2(ac0)-30+10;
  158. shift = (shift+1)/2;
  159. for(i=0;i<n;i++)
  160. xx[i] = VSHR32(xx[i], shift);
  161. }
  162. #endif
  163. while (lag>=0)
  164. {
  165. for (i = lag, d = 0; i < n; i++)
  166. d += xx[i] * xx[i-lag];
  167. ac[lag] = d;
  168. /*printf ("%f ", ac[lag]);*/
  169. lag--;
  170. }
  171. /*printf ("\n");*/
  172. ac[0] += 10;
  173. RESTORE_STACK;
  174. }