StateSearchW.c 5.9 KB

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