createCB.c 6.2 KB

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