FrameClassify.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. /******************************************************************
  2. iLBC Speech Coder ANSI-C Source Code
  3. FrameClassify.c
  4. Copyright (C) The Internet Society (2004).
  5. All Rights Reserved.
  6. ******************************************************************/
  7. #include "iLBC_define.h"
  8. /*---------------------------------------------------------------*
  9. * Classification of subframes to localize start state
  10. *--------------------------------------------------------------*/
  11. int FrameClassify( /* index to the max-energy sub-frame */
  12. iLBC_Enc_Inst_t *iLBCenc_inst,
  13. /* (i/o) the encoder state structure */
  14. float *residual /* (i) lpc residual signal */
  15. ) {
  16. float max_ssqEn, fssqEn[NSUB_MAX], bssqEn[NSUB_MAX], *pp;
  17. int n, l, max_ssqEn_n;
  18. const float ssqEn_win[NSUB_MAX-1]={(float)0.8,(float)0.9,
  19. (float)1.0,(float)0.9,(float)0.8};
  20. const float sampEn_win[5]={(float)1.0/(float)6.0,
  21. (float)2.0/(float)6.0, (float)3.0/(float)6.0,
  22. (float)4.0/(float)6.0, (float)5.0/(float)6.0};
  23. /* init the front and back energies to zero */
  24. memset(fssqEn, 0, NSUB_MAX*sizeof(float));
  25. memset(bssqEn, 0, NSUB_MAX*sizeof(float));
  26. /* Calculate front of first seqence */
  27. n=0;
  28. pp=residual;
  29. for (l=0; l<5; l++) {
  30. fssqEn[n] += sampEn_win[l] * (*pp) * (*pp);
  31. pp++;
  32. }
  33. for (l=5; l<SUBL; l++) {
  34. fssqEn[n] += (*pp) * (*pp);
  35. pp++;
  36. }
  37. /* Calculate front and back of all middle sequences */
  38. for (n=1; n<iLBCenc_inst->nsub-1; n++) {
  39. pp=residual+n*SUBL;
  40. for (l=0; l<5; l++) {
  41. fssqEn[n] += sampEn_win[l] * (*pp) * (*pp);
  42. bssqEn[n] += (*pp) * (*pp);
  43. pp++;
  44. }
  45. for (l=5; l<SUBL-5; l++) {
  46. fssqEn[n] += (*pp) * (*pp);
  47. bssqEn[n] += (*pp) * (*pp);
  48. pp++;
  49. }
  50. for (l=SUBL-5; l<SUBL; l++) {
  51. fssqEn[n] += (*pp) * (*pp);
  52. bssqEn[n] += sampEn_win[SUBL-l-1] * (*pp) * (*pp);
  53. pp++;
  54. }
  55. }
  56. /* Calculate back of last seqence */
  57. n=iLBCenc_inst->nsub-1;
  58. pp=residual+n*SUBL;
  59. for (l=0; l<SUBL-5; l++) {
  60. bssqEn[n] += (*pp) * (*pp);
  61. pp++;
  62. }
  63. for (l=SUBL-5; l<SUBL; l++) {
  64. bssqEn[n] += sampEn_win[SUBL-l-1] * (*pp) * (*pp);
  65. pp++;
  66. }
  67. /* find the index to the weighted 80 sample with
  68. most energy */
  69. if (iLBCenc_inst->mode==20) l=1;
  70. else l=0;
  71. max_ssqEn=(fssqEn[0]+bssqEn[1])*ssqEn_win[l];
  72. max_ssqEn_n=1;
  73. for (n=2; n<iLBCenc_inst->nsub; n++) {
  74. l++;
  75. if ((fssqEn[n-1]+bssqEn[n])*ssqEn_win[l] > max_ssqEn) {
  76. max_ssqEn=(fssqEn[n-1]+bssqEn[n]) *
  77. ssqEn_win[l];
  78. max_ssqEn_n=n;
  79. }
  80. }
  81. return max_ssqEn_n;
  82. }