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.

328 lines
8.8 KiB

  1. /*---------------------------------------------------------------------------*\
  2. Original copyright
  3. FILE........: AKSLSPD.C
  4. TYPE........: Turbo C
  5. COMPANY.....: Voicetronix
  6. AUTHOR......: David Rowe
  7. DATE CREATED: 24/2/93
  8. Modified by Jean-Marc Valin
  9. This file contains functions for converting Linear Prediction
  10. Coefficients (LPC) to Line Spectral Pair (LSP) and back. Note that the
  11. LSP coefficients are not in radians format but in the x domain of the
  12. unit circle.
  13. Speex License:
  14. Redistribution and use in source and binary forms, with or without
  15. modification, are permitted provided that the following conditions
  16. are met:
  17. - Redistributions of source code must retain the above copyright
  18. notice, this list of conditions and the following disclaimer.
  19. - Redistributions in binary form must reproduce the above copyright
  20. notice, this list of conditions and the following disclaimer in the
  21. documentation and/or other materials provided with the distribution.
  22. - Neither the name of the Xiph.org Foundation nor the names of its
  23. contributors may be used to endorse or promote products derived from
  24. this software without specific prior written permission.
  25. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  26. ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  27. LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  28. A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR
  29. CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  30. EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  31. PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  32. PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  33. LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  34. NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  35. SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  36. */
  37. #include <math.h>
  38. #include "lsp.h"
  39. #include "stack_alloc.h"
  40. #ifndef M_PI
  41. #define M_PI 3.14159265358979323846 /* pi */
  42. #endif
  43. #ifndef NULL
  44. #define NULL 0
  45. #endif
  46. /*---------------------------------------------------------------------------*\
  47. FUNCTION....: cheb_poly_eva()
  48. AUTHOR......: David Rowe
  49. DATE CREATED: 24/2/93
  50. This function evaluates a series of Chebyshev polynomials
  51. \*---------------------------------------------------------------------------*/
  52. static float cheb_poly_eva(float *coef,float x,int m,char *stack)
  53. /* float coef[] coefficients of the polynomial to be evaluated */
  54. /* float x the point where polynomial is to be evaluated */
  55. /* int m order of the polynomial */
  56. {
  57. int i;
  58. float *T,sum;
  59. int m2=m>>1;
  60. /* Allocate memory for Chebyshev series formulation */
  61. T=PUSH(stack, m2+1, float);
  62. /* Initialise values */
  63. T[0]=1;
  64. T[1]=x;
  65. /* Evaluate Chebyshev series formulation using iterative approach */
  66. /* Evaluate polynomial and return value also free memory space */
  67. sum = coef[m2] + coef[m2-1]*x;
  68. x *= 2;
  69. for(i=2;i<=m2;i++)
  70. {
  71. T[i] = x*T[i-1] - T[i-2];
  72. sum += coef[m2-i] * T[i];
  73. }
  74. return sum;
  75. }
  76. /*---------------------------------------------------------------------------*\
  77. FUNCTION....: lpc_to_lsp()
  78. AUTHOR......: David Rowe
  79. DATE CREATED: 24/2/93
  80. This function converts LPC coefficients to LSP
  81. coefficients.
  82. \*---------------------------------------------------------------------------*/
  83. int lpc_to_lsp (float *a,int lpcrdr,float *freq,int nb,float delta, char *stack)
  84. /* float *a lpc coefficients */
  85. /* int lpcrdr order of LPC coefficients (10) */
  86. /* float *freq LSP frequencies in the x domain */
  87. /* int nb number of sub-intervals (4) */
  88. /* float delta grid spacing interval (0.02) */
  89. {
  90. float psuml,psumr,psumm,temp_xr,xl,xr,xm=0;
  91. float temp_psumr/*,temp_qsumr*/;
  92. int i,j,m,flag,k;
  93. float *Q; /* ptrs for memory allocation */
  94. float *P;
  95. float *px; /* ptrs of respective P'(z) & Q'(z) */
  96. float *qx;
  97. float *p;
  98. float *q;
  99. float *pt; /* ptr used for cheb_poly_eval()
  100. whether P' or Q' */
  101. int roots=0; /* DR 8/2/94: number of roots found */
  102. flag = 1; /* program is searching for a root when,
  103. 1 else has found one */
  104. m = lpcrdr/2; /* order of P'(z) & Q'(z) polynomials */
  105. /* Allocate memory space for polynomials */
  106. Q = PUSH(stack, (m+1), float);
  107. P = PUSH(stack, (m+1), float);
  108. /* determine P'(z)'s and Q'(z)'s coefficients where
  109. P'(z) = P(z)/(1 + z^(-1)) and Q'(z) = Q(z)/(1-z^(-1)) */
  110. px = P; /* initialise ptrs */
  111. qx = Q;
  112. p = px;
  113. q = qx;
  114. *px++ = 1.0;
  115. *qx++ = 1.0;
  116. for(i=1;i<=m;i++){
  117. *px++ = a[i]+a[lpcrdr+1-i]-*p++;
  118. *qx++ = a[i]-a[lpcrdr+1-i]+*q++;
  119. }
  120. px = P;
  121. qx = Q;
  122. for(i=0;i<m;i++){
  123. *px = 2**px;
  124. *qx = 2**qx;
  125. px++;
  126. qx++;
  127. }
  128. px = P; /* re-initialise ptrs */
  129. qx = Q;
  130. /* Search for a zero in P'(z) polynomial first and then alternate to Q'(z).
  131. Keep alternating between the two polynomials as each zero is found */
  132. xr = 0; /* initialise xr to zero */
  133. xl = 1.0; /* start at point xl = 1 */
  134. for(j=0;j<lpcrdr;j++){
  135. if(j%2) /* determines whether P' or Q' is eval. */
  136. pt = qx;
  137. else
  138. pt = px;
  139. psuml = cheb_poly_eva(pt,xl,lpcrdr,stack); /* evals poly. at xl */
  140. flag = 1;
  141. while(flag && (xr >= -1.0)){
  142. float dd;
  143. /* Modified by JMV to provide smaller steps around x=+-1 */
  144. dd=(delta*(1-.9*xl*xl));
  145. if (fabs(psuml)<.2)
  146. dd *= .5;
  147. xr = xl - dd; /* interval spacing */
  148. psumr = cheb_poly_eva(pt,xr,lpcrdr,stack);/* poly(xl-delta_x) */
  149. temp_psumr = psumr;
  150. temp_xr = xr;
  151. /* if no sign change increment xr and re-evaluate poly(xr). Repeat til
  152. sign change.
  153. if a sign change has occurred the interval is bisected and then
  154. checked again for a sign change which determines in which
  155. interval the zero lies in.
  156. If there is no sign change between poly(xm) and poly(xl) set interval
  157. between xm and xr else set interval between xl and xr and repeat till
  158. root is located within the specified limits */
  159. if((psumr*psuml)<0.0){
  160. roots++;
  161. psumm=psuml;
  162. for(k=0;k<=nb;k++){
  163. xm = (xl+xr)/2; /* bisect the interval */
  164. psumm=cheb_poly_eva(pt,xm,lpcrdr,stack);
  165. if(psumm*psuml>0.){
  166. psuml=psumm;
  167. xl=xm;
  168. }
  169. else{
  170. psumr=psumm;
  171. xr=xm;
  172. }
  173. }
  174. /* once zero is found, reset initial interval to xr */
  175. freq[j] = (xm);
  176. xl = xm;
  177. flag = 0; /* reset flag for next search */
  178. }
  179. else{
  180. psuml=temp_psumr;
  181. xl=temp_xr;
  182. }
  183. }
  184. }
  185. return(roots);
  186. }
  187. /*---------------------------------------------------------------------------*\
  188. FUNCTION....: lsp_to_lpc()
  189. AUTHOR......: David Rowe
  190. DATE CREATED: 24/2/93
  191. lsp_to_lpc: This function converts LSP coefficients to LPC
  192. coefficients.
  193. \*---------------------------------------------------------------------------*/
  194. void lsp_to_lpc(float *freq,float *ak,int lpcrdr, char *stack)
  195. /* float *freq array of LSP frequencies in the x domain */
  196. /* float *ak array of LPC coefficients */
  197. /* int lpcrdr order of LPC coefficients */
  198. {
  199. int i,j;
  200. float xout1,xout2,xin1,xin2;
  201. float *Wp;
  202. float *pw,*n1,*n2,*n3,*n4=NULL;
  203. int m = lpcrdr/2;
  204. Wp = PUSH(stack, 4*m+2, float);
  205. pw = Wp;
  206. /* initialise contents of array */
  207. for(i=0;i<=4*m+1;i++){ /* set contents of buffer to 0 */
  208. *pw++ = 0.0;
  209. }
  210. /* Set pointers up */
  211. pw = Wp;
  212. xin1 = 1.0;
  213. xin2 = 1.0;
  214. /* reconstruct P(z) and Q(z) by cascading second order
  215. polynomials in form 1 - 2xz(-1) +z(-2), where x is the
  216. LSP coefficient */
  217. for(j=0;j<=lpcrdr;j++){
  218. int i2=0;
  219. for(i=0;i<m;i++,i2+=2){
  220. n1 = pw+(i*4);
  221. n2 = n1 + 1;
  222. n3 = n2 + 1;
  223. n4 = n3 + 1;
  224. xout1 = xin1 - 2*(freq[i2]) * *n1 + *n2;
  225. xout2 = xin2 - 2*(freq[i2+1]) * *n3 + *n4;
  226. *n2 = *n1;
  227. *n4 = *n3;
  228. *n1 = xin1;
  229. *n3 = xin2;
  230. xin1 = xout1;
  231. xin2 = xout2;
  232. }
  233. xout1 = xin1 + *(n4+1);
  234. xout2 = xin2 - *(n4+2);
  235. ak[j] = (xout1 + xout2)*0.5;
  236. *(n4+1) = xin1;
  237. *(n4+2) = xin2;
  238. xin1 = 0.0;
  239. xin2 = 0.0;
  240. }
  241. }
  242. /*Added by JMV
  243. Makes sure the LSPs are stable*/
  244. void lsp_enforce_margin(float *lsp, int len, float margin)
  245. {
  246. int i;
  247. if (lsp[0]<margin)
  248. lsp[0]=margin;
  249. if (lsp[len-1]>M_PI-margin)
  250. lsp[len-1]=M_PI-margin;
  251. for (i=1;i<len-1;i++)
  252. {
  253. if (lsp[i]<lsp[i-1]+margin)
  254. lsp[i]=lsp[i-1]+margin;
  255. if (lsp[i]>lsp[i+1]-margin)
  256. lsp[i]= .5* (lsp[i] + lsp[i+1]-margin);
  257. }
  258. }