lsf.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. /******************************************************************
  2. iLBC Speech Coder ANSI-C Source Code
  3. lsf.c
  4. Copyright (C) The Internet Society (2004).
  5. All Rights Reserved.
  6. ******************************************************************/
  7. #include <string.h>
  8. #include <math.h>
  9. #include "iLBC_define.h"
  10. #include "lsf.h"
  11. /*----------------------------------------------------------------*
  12. * conversion from lpc coefficients to lsf coefficients
  13. *---------------------------------------------------------------*/
  14. void a2lsf(
  15. float *freq,/* (o) lsf coefficients */
  16. float *a /* (i) lpc coefficients */
  17. ){
  18. float steps[LSF_NUMBER_OF_STEPS] =
  19. {(float)0.00635, (float)0.003175, (float)0.0015875,
  20. (float)0.00079375};
  21. float step;
  22. int step_idx;
  23. int lsp_index;
  24. float p[LPC_HALFORDER];
  25. float q[LPC_HALFORDER];
  26. float p_pre[LPC_HALFORDER];
  27. float q_pre[LPC_HALFORDER];
  28. float old_p, old_q, *old;
  29. float *pq_coef;
  30. float omega, old_omega;
  31. int i;
  32. float hlp, hlp1, hlp2, hlp3, hlp4, hlp5;
  33. for (i=0; i<LPC_HALFORDER; i++) {
  34. p[i] = (float)-1.0 * (a[i + 1] + a[LPC_FILTERORDER - i]);
  35. q[i] = a[LPC_FILTERORDER - i] - a[i + 1];
  36. }
  37. p_pre[0] = (float)-1.0 - p[0];
  38. p_pre[1] = - p_pre[0] - p[1];
  39. p_pre[2] = - p_pre[1] - p[2];
  40. p_pre[3] = - p_pre[2] - p[3];
  41. p_pre[4] = - p_pre[3] - p[4];
  42. p_pre[4] = p_pre[4] / 2;
  43. q_pre[0] = (float)1.0 - q[0];
  44. q_pre[1] = q_pre[0] - q[1];
  45. q_pre[2] = q_pre[1] - q[2];
  46. q_pre[3] = q_pre[2] - q[3];
  47. q_pre[4] = q_pre[3] - q[4];
  48. q_pre[4] = q_pre[4] / 2;
  49. omega = 0.0;
  50. old_omega = 0.0;
  51. old_p = FLOAT_MAX;
  52. old_q = FLOAT_MAX;
  53. /* Here we loop through lsp_index to find all the
  54. LPC_FILTERORDER roots for omega. */
  55. for (lsp_index = 0; lsp_index<LPC_FILTERORDER; lsp_index++) {
  56. /* Depending on lsp_index being even or odd, we
  57. alternatively solve the roots for the two LSP equations. */
  58. if ((lsp_index & 0x1) == 0) {
  59. pq_coef = p_pre;
  60. old = &old_p;
  61. } else {
  62. pq_coef = q_pre;
  63. old = &old_q;
  64. }
  65. /* Start with low resolution grid */
  66. for (step_idx = 0, step = steps[step_idx];
  67. step_idx < LSF_NUMBER_OF_STEPS;){
  68. /* cos(10piw) + pq(0)cos(8piw) + pq(1)cos(6piw) +
  69. pq(2)cos(4piw) + pq(3)cod(2piw) + pq(4) */
  70. hlp = (float)cos(omega * TWO_PI);
  71. hlp1 = (float)2.0 * hlp + pq_coef[0];
  72. hlp2 = (float)2.0 * hlp * hlp1 - (float)1.0 +
  73. pq_coef[1];
  74. hlp3 = (float)2.0 * hlp * hlp2 - hlp1 + pq_coef[2];
  75. hlp4 = (float)2.0 * hlp * hlp3 - hlp2 + pq_coef[3];
  76. hlp5 = hlp * hlp4 - hlp3 + pq_coef[4];
  77. if (((hlp5 * (*old)) <= 0.0) || (omega >= 0.5)){
  78. if (step_idx == (LSF_NUMBER_OF_STEPS - 1)){
  79. if (fabs(hlp5) >= fabs(*old)) {
  80. freq[lsp_index] = omega - step;
  81. } else {
  82. freq[lsp_index] = omega;
  83. }
  84. if ((*old) >= 0.0){
  85. *old = (float)-1.0 * FLOAT_MAX;
  86. } else {
  87. *old = FLOAT_MAX;
  88. }
  89. omega = old_omega;
  90. step_idx = 0;
  91. step_idx = LSF_NUMBER_OF_STEPS;
  92. } else {
  93. if (step_idx == 0) {
  94. old_omega = omega;
  95. }
  96. step_idx++;
  97. omega -= steps[step_idx];
  98. /* Go back one grid step */
  99. step = steps[step_idx];
  100. }
  101. } else {
  102. /* increment omega until they are of different sign,
  103. and we know there is at least one root between omega
  104. and old_omega */
  105. *old = hlp5;
  106. omega += step;
  107. }
  108. }
  109. }
  110. for (i = 0; i<LPC_FILTERORDER; i++) {
  111. freq[i] = freq[i] * TWO_PI;
  112. }
  113. }
  114. /*----------------------------------------------------------------*
  115. * conversion from lsf coefficients to lpc coefficients
  116. *---------------------------------------------------------------*/
  117. void lsf2a(
  118. float *a_coef, /* (o) lpc coefficients */
  119. float *freq /* (i) lsf coefficients */
  120. ){
  121. int i, j;
  122. float hlp;
  123. float p[LPC_HALFORDER], q[LPC_HALFORDER];
  124. float a[LPC_HALFORDER + 1], a1[LPC_HALFORDER],
  125. a2[LPC_HALFORDER];
  126. float b[LPC_HALFORDER + 1], b1[LPC_HALFORDER],
  127. b2[LPC_HALFORDER];
  128. for (i=0; i<LPC_FILTERORDER; i++) {
  129. freq[i] = freq[i] * PI2;
  130. }
  131. /* Check input for ill-conditioned cases. This part is not
  132. found in the TIA standard. It involves the following 2 IF
  133. blocks. If "freq" is judged ill-conditioned, then we first
  134. modify freq[0] and freq[LPC_HALFORDER-1] (normally
  135. LPC_HALFORDER = 10 for LPC applications), then we adjust
  136. the other "freq" values slightly */
  137. if ((freq[0] <= 0.0) || (freq[LPC_FILTERORDER - 1] >= 0.5)){
  138. if (freq[0] <= 0.0) {
  139. freq[0] = (float)0.022;
  140. }
  141. if (freq[LPC_FILTERORDER - 1] >= 0.5) {
  142. freq[LPC_FILTERORDER - 1] = (float)0.499;
  143. }
  144. hlp = (freq[LPC_FILTERORDER - 1] - freq[0]) /
  145. (float) (LPC_FILTERORDER - 1);
  146. for (i=1; i<LPC_FILTERORDER; i++) {
  147. freq[i] = freq[i - 1] + hlp;
  148. }
  149. }
  150. memset(a1, 0, LPC_HALFORDER*sizeof(float));
  151. memset(a2, 0, LPC_HALFORDER*sizeof(float));
  152. memset(b1, 0, LPC_HALFORDER*sizeof(float));
  153. memset(b2, 0, LPC_HALFORDER*sizeof(float));
  154. memset(a, 0, (LPC_HALFORDER+1)*sizeof(float));
  155. memset(b, 0, (LPC_HALFORDER+1)*sizeof(float));
  156. /* p[i] and q[i] compute cos(2*pi*omega_{2j}) and
  157. cos(2*pi*omega_{2j-1} in eqs. 4.2.2.2-1 and 4.2.2.2-2.
  158. Note that for this code p[i] specifies the coefficients
  159. used in .Q_A(z) while q[i] specifies the coefficients used
  160. in .P_A(z) */
  161. for (i=0; i<LPC_HALFORDER; i++) {
  162. p[i] = (float)cos(TWO_PI * freq[2 * i]);
  163. q[i] = (float)cos(TWO_PI * freq[2 * i + 1]);
  164. }
  165. a[0] = 0.25;
  166. b[0] = 0.25;
  167. for (i= 0; i<LPC_HALFORDER; i++) {
  168. a[i + 1] = a[i] - 2 * p[i] * a1[i] + a2[i];
  169. b[i + 1] = b[i] - 2 * q[i] * b1[i] + b2[i];
  170. a2[i] = a1[i];
  171. a1[i] = a[i];
  172. b2[i] = b1[i];
  173. b1[i] = b[i];
  174. }
  175. for (j=0; j<LPC_FILTERORDER; j++) {
  176. if (j == 0) {
  177. a[0] = 0.25;
  178. b[0] = -0.25;
  179. } else {
  180. a[0] = b[0] = 0.0;
  181. }
  182. for (i=0; i<LPC_HALFORDER; i++) {
  183. a[i + 1] = a[i] - 2 * p[i] * a1[i] + a2[i];
  184. b[i + 1] = b[i] - 2 * q[i] * b1[i] + b2[i];
  185. a2[i] = a1[i];
  186. a1[i] = a[i];
  187. b2[i] = b1[i];
  188. b1[i] = b[i];
  189. }
  190. a_coef[j + 1] = 2 * (a[LPC_HALFORDER] + b[LPC_HALFORDER]);
  191. }
  192. a_coef[0] = 1.0;
  193. }