getCBvec.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. /******************************************************************
  2. iLBC Speech Coder ANSI-C Source Code
  3. getCBvec.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. /*----------------------------------------------------------------*
  11. * Construct codebook vector for given index.
  12. *---------------------------------------------------------------*/
  13. void getCBvec(
  14. float *cbvec, /* (o) Constructed codebook vector */
  15. float *mem, /* (i) Codebook buffer */
  16. int index, /* (i) Codebook index */
  17. int lMem, /* (i) Length of codebook buffer */
  18. int cbveclen/* (i) Codebook vector length */
  19. ){
  20. int j, k, n, memInd, sFilt;
  21. float tmpbuf[CB_MEML];
  22. int base_size;
  23. int ilow, ihigh;
  24. float alfa, alfa1;
  25. /* Determine size of codebook sections */
  26. base_size=lMem-cbveclen+1;
  27. if (cbveclen==SUBL) {
  28. base_size+=cbveclen/2;
  29. }
  30. /* No filter -> First codebook section */
  31. if (index<lMem-cbveclen+1) {
  32. /* first non-interpolated vectors */
  33. k=index+cbveclen;
  34. /* get vector */
  35. memcpy(cbvec, mem+lMem-k, cbveclen*sizeof(float));
  36. } else if (index < base_size) {
  37. k=2*(index-(lMem-cbveclen+1))+cbveclen;
  38. ihigh=k/2;
  39. ilow=ihigh-5;
  40. /* Copy first noninterpolated part */
  41. memcpy(cbvec, mem+lMem-k/2, ilow*sizeof(float));
  42. /* interpolation */
  43. alfa1=(float)0.2;
  44. alfa=0.0;
  45. for (j=ilow; j<ihigh; j++) {
  46. cbvec[j]=((float)1.0-alfa)*mem[lMem-k/2+j]+
  47. alfa*mem[lMem-k+j];
  48. alfa+=alfa1;
  49. }
  50. /* Copy second noninterpolated part */
  51. memcpy(cbvec+ihigh, mem+lMem-k+ihigh,
  52. (cbveclen-ihigh)*sizeof(float));
  53. }
  54. /* Higher codebook section based on filtering */
  55. else {
  56. /* first non-interpolated vectors */
  57. if (index-base_size<lMem-cbveclen+1) {
  58. float tempbuff2[CB_MEML+CB_FILTERLEN+1];
  59. float *pos;
  60. float *pp, *pp1;
  61. memset(tempbuff2, 0,
  62. CB_HALFFILTERLEN*sizeof(float));
  63. memcpy(&tempbuff2[CB_HALFFILTERLEN], mem,
  64. lMem*sizeof(float));
  65. memset(&tempbuff2[lMem+CB_HALFFILTERLEN], 0,
  66. (CB_HALFFILTERLEN+1)*sizeof(float));
  67. k=index-base_size+cbveclen;
  68. sFilt=lMem-k;
  69. memInd=sFilt+1-CB_HALFFILTERLEN;
  70. /* do filtering */
  71. pos=cbvec;
  72. memset(pos, 0, cbveclen*sizeof(float));
  73. for (n=0; n<cbveclen; n++) {
  74. pp=&tempbuff2[memInd+n+CB_HALFFILTERLEN];
  75. pp1=&cbfiltersTbl[CB_FILTERLEN-1];
  76. for (j=0; j<CB_FILTERLEN; j++) {
  77. (*pos)+=(*pp++)*(*pp1--);
  78. }
  79. pos++;
  80. }
  81. }
  82. /* interpolated vectors */
  83. else {
  84. float tempbuff2[CB_MEML+CB_FILTERLEN+1];
  85. float *pos;
  86. float *pp, *pp1;
  87. int i;
  88. memset(tempbuff2, 0,
  89. CB_HALFFILTERLEN*sizeof(float));
  90. memcpy(&tempbuff2[CB_HALFFILTERLEN], mem,
  91. lMem*sizeof(float));
  92. memset(&tempbuff2[lMem+CB_HALFFILTERLEN], 0,
  93. (CB_HALFFILTERLEN+1)*sizeof(float));
  94. k=2*(index-base_size-
  95. (lMem-cbveclen+1))+cbveclen;
  96. sFilt=lMem-k;
  97. memInd=sFilt+1-CB_HALFFILTERLEN;
  98. /* do filtering */
  99. pos=&tmpbuf[sFilt];
  100. memset(pos, 0, k*sizeof(float));
  101. for (i=0; i<k; i++) {
  102. pp=&tempbuff2[memInd+i+CB_HALFFILTERLEN];
  103. pp1=&cbfiltersTbl[CB_FILTERLEN-1];
  104. for (j=0; j<CB_FILTERLEN; j++) {
  105. (*pos)+=(*pp++)*(*pp1--);
  106. }
  107. pos++;
  108. }
  109. ihigh=k/2;
  110. ilow=ihigh-5;
  111. /* Copy first noninterpolated part */
  112. memcpy(cbvec, tmpbuf+lMem-k/2,
  113. ilow*sizeof(float));
  114. /* interpolation */
  115. alfa1=(float)0.2;
  116. alfa=0.0;
  117. for (j=ilow; j<ihigh; j++) {
  118. cbvec[j]=((float)1.0-alfa)*
  119. tmpbuf[lMem-k/2+j]+alfa*tmpbuf[lMem-k+j];
  120. alfa+=alfa1;
  121. }
  122. /* Copy second noninterpolated part */
  123. memcpy(cbvec+ihigh, tmpbuf+lMem-k+ihigh,
  124. (cbveclen-ihigh)*sizeof(float));
  125. }
  126. }
  127. }