StateSearchW.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. /******************************************************************
  2. iLBC Speech Coder ANSI-C Source Code
  3. StateSearchW.c
  4. Copyright (c) 2001,
  5. Global IP Sound AB.
  6. All rights reserved.
  7. ******************************************************************/
  8. #include <math.h>
  9. #include <string.h>
  10. #include "iLBC_define.h"
  11. #include "constants.h"
  12. #include "filter.h"
  13. #include "helpfun.h"
  14. #include "StateSearchW.h"
  15. /*----------------------------------------------------------------*
  16. * predictive noise shaping encoding of scaled start state
  17. * (subrutine for StateSearchW)
  18. *---------------------------------------------------------------*/
  19. void AbsQuantW(
  20. float *in, /* (i) vector to encode */
  21. float *syntDenum, /* (i) denominator of synthesis filter */
  22. float *weightDenum, /* (i) denominator of weighting filter */
  23. int *out, /* (o) vector of quantizer indexes */
  24. int len, /* (i) length of vector to encode and
  25. vector of quantizer indexes */
  26. int state_first /* (i) position of start state in the
  27. 80 vec */
  28. ){
  29. float *syntOut, syntOutBuf[LPC_FILTERORDER+STATE_SHORT_LEN];
  30. float toQ, xq;
  31. int n;
  32. int index;
  33. /* initialization of buffer for filtering */
  34. memset(syntOutBuf, 0, LPC_FILTERORDER*sizeof(float));
  35. /* initialization of pointer for filtering */
  36. syntOut = &syntOutBuf[LPC_FILTERORDER];
  37. /* synthesis and weighting filters on input */
  38. if (state_first) {
  39. AllPoleFilter (in, weightDenum, SUBL, LPC_FILTERORDER);
  40. } else {
  41. AllPoleFilter (in, weightDenum, STATE_SHORT_LEN-SUBL,
  42. LPC_FILTERORDER);
  43. }
  44. /* encoding loop */
  45. for(n=0;n<len;n++){
  46. /* time update of filter coefficients */
  47. if ((state_first)&&(n==SUBL)){
  48. syntDenum += (LPC_FILTERORDER+1);
  49. weightDenum += (LPC_FILTERORDER+1);
  50. /* synthesis and weighting filters on input */
  51. AllPoleFilter (&in[n], weightDenum, len-n,
  52. LPC_FILTERORDER);
  53. } else if ((state_first==0)&&(n==(STATE_SHORT_LEN-SUBL))) {
  54. syntDenum += (LPC_FILTERORDER+1);
  55. weightDenum += (LPC_FILTERORDER+1);
  56. /* synthesis and weighting filters on input */
  57. AllPoleFilter (&in[n], weightDenum, len-n,
  58. LPC_FILTERORDER);
  59. }
  60. /* prediction of synthesized and weighted input */
  61. syntOut[n] = 0.0;
  62. AllPoleFilter (&syntOut[n], weightDenum, 1, LPC_FILTERORDER);
  63. /* quantization */
  64. toQ = in[n]-syntOut[n];
  65. sort_sq(&xq, &index, toQ, state_sq3Tbl, 8);
  66. out[n]=index;
  67. syntOut[n] = state_sq3Tbl[out[n]];
  68. /* update of the prediction filter */
  69. AllPoleFilter(&syntOut[n], weightDenum, 1, LPC_FILTERORDER);
  70. }
  71. }
  72. /*----------------------------------------------------------------*
  73. * encoding of start state
  74. *---------------------------------------------------------------*/
  75. void StateSearchW(
  76. float *residual,/* (i) target residual vector */
  77. float *syntDenum, /* (i) lpc synthesis filter */
  78. float *weightDenum, /* (i) weighting filter denuminator */
  79. int *idxForMax, /* (o) quantizer index for maximum
  80. amplitude */
  81. int *idxVec, /* (o) vector of quantization indexes */
  82. int len, /* (i) length of all vectors */
  83. int state_first /* (i) position of start state in the
  84. 80 vec */
  85. ){
  86. float dtmp, maxVal, tmpbuf[LPC_FILTERORDER+2*STATE_SHORT_LEN];
  87. float *tmp, numerator[1+LPC_FILTERORDER];
  88. float foutbuf[LPC_FILTERORDER+2*STATE_SHORT_LEN], *fout;
  89. int k;
  90. float qmax, scal;
  91. /* initialization of buffers and filter coefficients */
  92. memset(tmpbuf, 0, LPC_FILTERORDER*sizeof(float));
  93. memset(foutbuf, 0, LPC_FILTERORDER*sizeof(float));
  94. for(k=0; k<LPC_FILTERORDER; k++){
  95. numerator[k]=syntDenum[LPC_FILTERORDER-k];
  96. }
  97. numerator[LPC_FILTERORDER]=syntDenum[0];
  98. tmp = &tmpbuf[LPC_FILTERORDER];
  99. fout = &foutbuf[LPC_FILTERORDER];
  100. /* circular convolution with the all-pass filter */
  101. memcpy(tmp, residual, len*sizeof(float));
  102. memset(tmp+len, 0, len*sizeof(float));
  103. ZeroPoleFilter(tmp, numerator, syntDenum, 2*len,
  104. LPC_FILTERORDER, fout);
  105. for(k=0;k<len;k++){
  106. fout[k] += fout[k+len];
  107. }
  108. /* identification of the maximum amplitude value */
  109. maxVal = fout[0];
  110. for(k=1; k<len; k++){
  111. if(fout[k]*fout[k] > maxVal*maxVal){
  112. maxVal = fout[k];
  113. }
  114. }
  115. maxVal=(float)fabs(maxVal);
  116. /* encoding of the maximum amplitude value */
  117. if(maxVal < 10.0){
  118. maxVal = 10.0;
  119. }
  120. maxVal = (float)log10(maxVal);
  121. sort_sq(&dtmp, idxForMax, maxVal, state_frgqTbl, 64);
  122. /* decoding of the maximum amplitude representation value,
  123. and corresponding scaling of start state */
  124. maxVal=state_frgqTbl[*idxForMax];
  125. qmax = (float)pow(10,maxVal);
  126. scal = (float)(4.5)/qmax;
  127. for(k=0;k<len;k++){
  128. fout[k] *= scal;
  129. }
  130. /* predictive noise shaping encoding of scaled start state */
  131. AbsQuantW(fout,syntDenum,weightDenum,idxVec, len, state_first);
  132. }