LPCencode.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. /******************************************************************
  2. iLBC Speech Coder ANSI-C Source Code
  3. LPCencode.c
  4. Copyright (C) The Internet Society (2004).
  5. All Rights Reserved.
  6. ******************************************************************/
  7. #include <string.h>
  8. #include "iLBC_define.h"
  9. #include "helpfun.h"
  10. #include "lsf.h"
  11. #include "constants.h"
  12. /*----------------------------------------------------------------*
  13. * lpc analysis (subrutine to LPCencode)
  14. *---------------------------------------------------------------*/
  15. void SimpleAnalysis(
  16. float *lsf, /* (o) lsf coefficients */
  17. float *data, /* (i) new data vector */
  18. iLBC_Enc_Inst_t *iLBCenc_inst
  19. /* (i/o) the encoder state structure */
  20. ){
  21. int k, is;
  22. float temp[BLOCKL_MAX], lp[LPC_FILTERORDER + 1];
  23. float lp2[LPC_FILTERORDER + 1];
  24. float r[LPC_FILTERORDER + 1];
  25. is=LPC_LOOKBACK+BLOCKL_MAX-iLBCenc_inst->blockl;
  26. memcpy(iLBCenc_inst->lpc_buffer+is,data,
  27. iLBCenc_inst->blockl*sizeof(float));
  28. /* No lookahead, last window is asymmetric */
  29. for (k = 0; k < iLBCenc_inst->lpc_n; k++) {
  30. is = LPC_LOOKBACK;
  31. if (k < (iLBCenc_inst->lpc_n - 1)) {
  32. window(temp, lpc_winTbl,
  33. iLBCenc_inst->lpc_buffer, BLOCKL_MAX);
  34. } else {
  35. window(temp, lpc_asymwinTbl,
  36. iLBCenc_inst->lpc_buffer + is, BLOCKL_MAX);
  37. }
  38. autocorr(r, temp, BLOCKL_MAX, LPC_FILTERORDER);
  39. window(r, r, lpc_lagwinTbl, LPC_FILTERORDER + 1);
  40. levdurb(lp, temp, r, LPC_FILTERORDER);
  41. bwexpand(lp2, lp, LPC_CHIRP_SYNTDENUM, LPC_FILTERORDER+1);
  42. a2lsf(lsf + k*LPC_FILTERORDER, lp2);
  43. }
  44. is=LPC_LOOKBACK+BLOCKL_MAX-iLBCenc_inst->blockl;
  45. memmove(iLBCenc_inst->lpc_buffer,
  46. iLBCenc_inst->lpc_buffer+LPC_LOOKBACK+BLOCKL_MAX-is,
  47. is*sizeof(float));
  48. }
  49. /*----------------------------------------------------------------*
  50. * lsf interpolator and conversion from lsf to a coefficients
  51. * (subrutine to SimpleInterpolateLSF)
  52. *---------------------------------------------------------------*/
  53. void LSFinterpolate2a_enc(
  54. float *a, /* (o) lpc coefficients */
  55. float *lsf1,/* (i) first set of lsf coefficients */
  56. float *lsf2,/* (i) second set of lsf coefficients */
  57. float coef, /* (i) weighting coefficient to use between
  58. lsf1 and lsf2 */
  59. long length /* (i) length of coefficient vectors */
  60. ){
  61. float lsftmp[LPC_FILTERORDER];
  62. interpolate(lsftmp, lsf1, lsf2, coef, length);
  63. lsf2a(a, lsftmp);
  64. }
  65. /*----------------------------------------------------------------*
  66. * lsf interpolator (subrutine to LPCencode)
  67. *---------------------------------------------------------------*/
  68. void SimpleInterpolateLSF(
  69. float *syntdenum, /* (o) the synthesis filter denominator
  70. resulting from the quantized
  71. interpolated lsf */
  72. float *weightdenum, /* (o) the weighting filter denominator
  73. resulting from the unquantized
  74. interpolated lsf */
  75. float *lsf, /* (i) the unquantized lsf coefficients */
  76. float *lsfdeq, /* (i) the dequantized lsf coefficients */
  77. float *lsfold, /* (i) the unquantized lsf coefficients of
  78. the previous signal frame */
  79. float *lsfdeqold, /* (i) the dequantized lsf coefficients of
  80. the previous signal frame */
  81. int length, /* (i) should equate LPC_FILTERORDER */
  82. iLBC_Enc_Inst_t *iLBCenc_inst
  83. /* (i/o) the encoder state structure */
  84. ){
  85. int i, pos, lp_length;
  86. float lp[LPC_FILTERORDER + 1], *lsf2, *lsfdeq2;
  87. lsf2 = lsf + length;
  88. lsfdeq2 = lsfdeq + length;
  89. lp_length = length + 1;
  90. if (iLBCenc_inst->mode==30) {
  91. /* sub-frame 1: Interpolation between old and first
  92. set of lsf coefficients */
  93. LSFinterpolate2a_enc(lp, lsfdeqold, lsfdeq,
  94. lsf_weightTbl_30ms[0], length);
  95. memcpy(syntdenum,lp,lp_length*sizeof(float));
  96. LSFinterpolate2a_enc(lp, lsfold, lsf,
  97. lsf_weightTbl_30ms[0], length);
  98. bwexpand(weightdenum, lp, LPC_CHIRP_WEIGHTDENUM, lp_length);
  99. /* sub-frame 2 to 6: Interpolation between first
  100. and second set of lsf coefficients */
  101. pos = lp_length;
  102. for (i = 1; i < iLBCenc_inst->nsub; i++) {
  103. LSFinterpolate2a_enc(lp, lsfdeq, lsfdeq2,
  104. lsf_weightTbl_30ms[i], length);
  105. memcpy(syntdenum + pos,lp,lp_length*sizeof(float));
  106. LSFinterpolate2a_enc(lp, lsf, lsf2,
  107. lsf_weightTbl_30ms[i], length);
  108. bwexpand(weightdenum + pos, lp,
  109. LPC_CHIRP_WEIGHTDENUM, lp_length);
  110. pos += lp_length;
  111. }
  112. }
  113. else {
  114. pos = 0;
  115. for (i = 0; i < iLBCenc_inst->nsub; i++) {
  116. LSFinterpolate2a_enc(lp, lsfdeqold, lsfdeq,
  117. lsf_weightTbl_20ms[i], length);
  118. memcpy(syntdenum+pos,lp,lp_length*sizeof(float));
  119. LSFinterpolate2a_enc(lp, lsfold, lsf,
  120. lsf_weightTbl_20ms[i], length);
  121. bwexpand(weightdenum+pos, lp,
  122. LPC_CHIRP_WEIGHTDENUM, lp_length);
  123. pos += lp_length;
  124. }
  125. }
  126. /* update memory */
  127. if (iLBCenc_inst->mode==30) {
  128. memcpy(lsfold, lsf2, length*sizeof(float));
  129. memcpy(lsfdeqold, lsfdeq2, length*sizeof(float));
  130. }
  131. else {
  132. memcpy(lsfold, lsf, length*sizeof(float));
  133. memcpy(lsfdeqold, lsfdeq, length*sizeof(float));
  134. }
  135. }
  136. /*----------------------------------------------------------------*
  137. * lsf quantizer (subrutine to LPCencode)
  138. *---------------------------------------------------------------*/
  139. void SimplelsfQ(
  140. float *lsfdeq, /* (o) dequantized lsf coefficients
  141. (dimension FILTERORDER) */
  142. int *index, /* (o) quantization index */
  143. float *lsf, /* (i) the lsf coefficient vector to be
  144. quantized (dimension FILTERORDER ) */
  145. int lpc_n /* (i) number of lsf sets to quantize */
  146. ){
  147. /* Quantize first LSF with memoryless split VQ */
  148. SplitVQ(lsfdeq, index, lsf, lsfCbTbl, LSF_NSPLIT,
  149. dim_lsfCbTbl, size_lsfCbTbl);
  150. if (lpc_n==2) {
  151. /* Quantize second LSF with memoryless split VQ */
  152. SplitVQ(lsfdeq + LPC_FILTERORDER, index + LSF_NSPLIT,
  153. lsf + LPC_FILTERORDER, lsfCbTbl, LSF_NSPLIT,
  154. dim_lsfCbTbl, size_lsfCbTbl);
  155. }
  156. }
  157. /*----------------------------------------------------------------*
  158. * lpc encoder
  159. *---------------------------------------------------------------*/
  160. void LPCencode(
  161. float *syntdenum, /* (i/o) synthesis filter coefficients
  162. before/after encoding */
  163. float *weightdenum, /* (i/o) weighting denumerator
  164. coefficients before/after
  165. encoding */
  166. int *lsf_index, /* (o) lsf quantization index */
  167. float *data, /* (i) lsf coefficients to quantize */
  168. iLBC_Enc_Inst_t *iLBCenc_inst
  169. /* (i/o) the encoder state structure */
  170. ){
  171. float lsf[LPC_FILTERORDER * LPC_N_MAX];
  172. float lsfdeq[LPC_FILTERORDER * LPC_N_MAX];
  173. SimpleAnalysis(lsf, data, iLBCenc_inst);
  174. SimplelsfQ(lsfdeq, lsf_index, lsf, iLBCenc_inst->lpc_n);
  175. LSF_check(lsfdeq, LPC_FILTERORDER, iLBCenc_inst->lpc_n);
  176. SimpleInterpolateLSF(syntdenum, weightdenum,
  177. lsf, lsfdeq, iLBCenc_inst->lsfold,
  178. iLBCenc_inst->lsfdeqold, LPC_FILTERORDER, iLBCenc_inst);
  179. }