createCB.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. /******************************************************************
  2. iLBC Speech Coder ANSI-C Source Code
  3. createCB.c
  4. Copyright (C) The Internet Society (2004).
  5. All Rights Reserved.
  6. ******************************************************************/
  7. #include "iLBC_define.h"
  8. #include "constants.h"
  9. #include <string.h>
  10. #include <math.h>
  11. /*----------------------------------------------------------------*
  12. * Construct an additional codebook vector by filtering the
  13. * initial codebook buffer. This vector is then used to expand
  14. * the codebook with an additional section.
  15. *---------------------------------------------------------------*/
  16. void filteredCBvecs(
  17. float *cbvectors, /* (o) Codebook vectors for the
  18. higher section */
  19. float *mem, /* (i) Buffer to create codebook
  20. vector from */
  21. int lMem /* (i) Length of buffer */
  22. ){
  23. int j, k;
  24. float *pp, *pp1;
  25. float tempbuff2[CB_MEML+CB_FILTERLEN];
  26. float *pos;
  27. memset(tempbuff2, 0, (CB_HALFFILTERLEN-1)*sizeof(float));
  28. memcpy(&tempbuff2[CB_HALFFILTERLEN-1], mem, lMem*sizeof(float));
  29. memset(&tempbuff2[lMem+CB_HALFFILTERLEN-1], 0,
  30. (CB_HALFFILTERLEN+1)*sizeof(float));
  31. /* Create codebook vector for higher section by filtering */
  32. /* do filtering */
  33. pos=cbvectors;
  34. memset(pos, 0, lMem*sizeof(float));
  35. for (k=0; k<lMem; k++) {
  36. pp=&tempbuff2[k];
  37. pp1=&cbfiltersTbl[CB_FILTERLEN-1];
  38. for (j=0;j<CB_FILTERLEN;j++) {
  39. (*pos)+=(*pp++)*(*pp1--);
  40. }
  41. pos++;
  42. }
  43. }
  44. /*----------------------------------------------------------------*
  45. * Search the augmented part of the codebook to find the best
  46. * measure.
  47. *----------------------------------------------------------------*/
  48. void searchAugmentedCB(
  49. int low, /* (i) Start index for the search */
  50. int high, /* (i) End index for the search */
  51. int stage, /* (i) Current stage */
  52. int startIndex, /* (i) Codebook index for the first
  53. aug vector */
  54. float *target, /* (i) Target vector for encoding */
  55. float *buffer, /* (i) Pointer to the end of the buffer for
  56. augmented codebook construction */
  57. float *max_measure, /* (i/o) Currently maximum measure */
  58. int *best_index,/* (o) Currently the best index */
  59. float *gain, /* (o) Currently the best gain */
  60. float *energy, /* (o) Energy of augmented codebook
  61. vectors */
  62. float *invenergy/* (o) Inv energy of augmented codebook
  63. vectors */
  64. ) {
  65. int icount, ilow, j, tmpIndex;
  66. float *pp, *ppo, *ppi, *ppe, crossDot, alfa;
  67. float weighted, measure, nrjRecursive;
  68. float ftmp;
  69. /* Compute the energy for the first (low-5)
  70. noninterpolated samples */
  71. nrjRecursive = (float) 0.0;
  72. pp = buffer - low + 1;
  73. for (j=0; j<(low-5); j++) {
  74. nrjRecursive += ( (*pp)*(*pp) );
  75. pp++;
  76. }
  77. ppe = buffer - low;
  78. for (icount=low; icount<=high; icount++) {
  79. /* Index of the codebook vector used for retrieving
  80. energy values */
  81. tmpIndex = startIndex+icount-20;
  82. ilow = icount-4;
  83. /* Update the energy recursively to save complexity */
  84. nrjRecursive = nrjRecursive + (*ppe)*(*ppe);
  85. ppe--;
  86. energy[tmpIndex] = nrjRecursive;
  87. /* Compute cross dot product for the first (low-5)
  88. samples */
  89. crossDot = (float) 0.0;
  90. pp = buffer-icount;
  91. for (j=0; j<ilow; j++) {
  92. crossDot += target[j]*(*pp++);
  93. }
  94. /* interpolation */
  95. alfa = (float) 0.2;
  96. ppo = buffer-4;
  97. ppi = buffer-icount-4;
  98. for (j=ilow; j<icount; j++) {
  99. weighted = ((float)1.0-alfa)*(*ppo)+alfa*(*ppi);
  100. ppo++;
  101. ppi++;
  102. energy[tmpIndex] += weighted*weighted;
  103. crossDot += target[j]*weighted;
  104. alfa += (float)0.2;
  105. }
  106. /* Compute energy and cross dot product for the
  107. remaining samples */
  108. pp = buffer - icount;
  109. for (j=icount; j<SUBL; j++) {
  110. energy[tmpIndex] += (*pp)*(*pp);
  111. crossDot += target[j]*(*pp++);
  112. }
  113. if (energy[tmpIndex]>0.0) {
  114. invenergy[tmpIndex]=(float)1.0/(energy[tmpIndex]+EPS);
  115. } else {
  116. invenergy[tmpIndex] = (float) 0.0;
  117. }
  118. if (stage==0) {
  119. measure = (float)-10000000.0;
  120. if (crossDot > 0.0) {
  121. measure = crossDot*crossDot*invenergy[tmpIndex];
  122. }
  123. }
  124. else {
  125. measure = crossDot*crossDot*invenergy[tmpIndex];
  126. }
  127. /* check if measure is better */
  128. ftmp = crossDot*invenergy[tmpIndex];
  129. if ((measure>*max_measure) && (fabs(ftmp)<CB_MAXGAIN)) {
  130. *best_index = tmpIndex;
  131. *max_measure = measure;
  132. *gain = ftmp;
  133. }
  134. }
  135. }
  136. /*----------------------------------------------------------------*
  137. * Recreate a specific codebook vector from the augmented part.
  138. *
  139. *----------------------------------------------------------------*/
  140. void createAugmentedVec(
  141. int index, /* (i) Index for the augmented vector
  142. to be created */
  143. float *buffer, /* (i) Pointer to the end of the buffer for
  144. augmented codebook construction */
  145. float *cbVec/* (o) The construced codebook vector */
  146. ) {
  147. int ilow, j;
  148. float *pp, *ppo, *ppi, alfa, alfa1, weighted;
  149. ilow = index-5;
  150. /* copy the first noninterpolated part */
  151. pp = buffer-index;
  152. memcpy(cbVec,pp,sizeof(float)*index);
  153. /* interpolation */
  154. alfa1 = (float)0.2;
  155. alfa = 0.0;
  156. ppo = buffer-5;
  157. ppi = buffer-index-5;
  158. for (j=ilow; j<index; j++) {
  159. weighted = ((float)1.0-alfa)*(*ppo)+alfa*(*ppi);
  160. ppo++;
  161. ppi++;
  162. cbVec[j] = weighted;
  163. alfa += alfa1;
  164. }
  165. /* copy the second noninterpolated part */
  166. pp = buffer - index;
  167. memcpy(cbVec+index,pp,sizeof(float)*(SUBL-index));
  168. }