Team Fortress 2 Source Code as on 22/4/2020
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.

119 lines
3.9 KiB

  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 slightly 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. /* LPC- and Reflection Coefficients
  41. *
  42. * The next two functions calculate linear prediction coefficients
  43. * and/or the related reflection coefficients from the first P_MAX+1
  44. * values of the autocorrelation function.
  45. */
  46. /* Invented by N. Levinson in 1947, modified by J. Durbin in 1959.
  47. */
  48. #include "lpc.h"
  49. float /* returns minimum mean square error */
  50. wld(
  51. float * lpc, /* [0...p-1] LPC coefficients */
  52. const float * ac, /* in: [0...p] autocorrelation values */
  53. float * ref, /* out: [0...p-1] reflection coef's */
  54. int p
  55. )
  56. {
  57. int i, j; float r, error = ac[0];
  58. if (ac[0] == 0) {
  59. for (i = 0; i < p; i++) ref[i] = 0; return 0; }
  60. for (i = 0; i < p; i++) {
  61. /* Sum up this iteration's reflection coefficient.
  62. */
  63. r = -ac[i + 1];
  64. for (j = 0; j < i; j++) r -= lpc[j] * ac[i - j];
  65. ref[i] = r /= error;
  66. /* Update LPC coefficients and total error.
  67. */
  68. lpc[i] = r;
  69. for (j = 0; j < i/2; j++) {
  70. float tmp = lpc[j];
  71. lpc[j] += r * lpc[i-1-j];
  72. lpc[i-1-j] += r * tmp;
  73. }
  74. if (i % 2) lpc[j] += lpc[j] * r;
  75. error *= 1.0 - r * r;
  76. }
  77. return error;
  78. }
  79. /* Compute the autocorrelation
  80. * ,--,
  81. * ac(i) = > x(n) * x(n-i) for all n
  82. * `--'
  83. * for lags between 0 and lag-1, and x == 0 outside 0...n-1
  84. */
  85. void _spx_autocorr(
  86. const float * x, /* in: [0...n-1] samples x */
  87. float *ac, /* out: [0...lag-1] ac values */
  88. int lag, int n)
  89. {
  90. float d; int i;
  91. while (lag--) {
  92. for (i = lag, d = 0; i < n; i++) d += x[i] * x[i-lag];
  93. ac[lag] = d;
  94. }
  95. }