gainquant.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. /******************************************************************
  2. iLBC Speech Coder ANSI-C Source Code
  3. gainquant.c
  4. Copyright (C) The Internet Society (2004).
  5. All Rights Reserved.
  6. ******************************************************************/
  7. #include <string.h>
  8. #include <math.h>
  9. #include "constants.h"
  10. #include "filter.h"
  11. /*----------------------------------------------------------------*
  12. * quantizer for the gain in the gain-shape coding of residual
  13. *---------------------------------------------------------------*/
  14. float gainquant(/* (o) quantized gain value */
  15. float in, /* (i) gain value */
  16. float maxIn,/* (i) maximum of gain value */
  17. int cblen, /* (i) number of quantization indices */
  18. int *index /* (o) quantization index */
  19. ){
  20. int i, tindex;
  21. float minmeasure,measure, *cb, scale;
  22. /* ensure a lower bound on the scaling factor */
  23. scale=maxIn;
  24. if (scale<0.1) {
  25. scale=(float)0.1;
  26. }
  27. /* select the quantization table */
  28. if (cblen == 8) {
  29. cb = gain_sq3Tbl;
  30. } else if (cblen == 16) {
  31. cb = gain_sq4Tbl;
  32. } else {
  33. cb = gain_sq5Tbl;
  34. }
  35. /* select the best index in the quantization table */
  36. minmeasure=10000000.0;
  37. tindex=0;
  38. for (i=0; i<cblen; i++) {
  39. measure=(in-scale*cb[i])*(in-scale*cb[i]);
  40. if (measure<minmeasure) {
  41. tindex=i;
  42. minmeasure=measure;
  43. }
  44. }
  45. *index=tindex;
  46. /* return the quantized value */
  47. return scale*cb[tindex];
  48. }
  49. /*----------------------------------------------------------------*
  50. * decoder for quantized gains in the gain-shape coding of
  51. * residual
  52. *---------------------------------------------------------------*/
  53. float gaindequant( /* (o) quantized gain value */
  54. int index, /* (i) quantization index */
  55. float maxIn,/* (i) maximum of unquantized gain */
  56. int cblen /* (i) number of quantization indices */
  57. ){
  58. float scale;
  59. /* obtain correct scale factor */
  60. scale=(float)fabs(maxIn);
  61. if (scale<0.1) {
  62. scale=(float)0.1;
  63. }
  64. /* select the quantization table and return the decoded value */
  65. if (cblen==8) {
  66. return scale*gain_sq3Tbl[index];
  67. } else if (cblen==16) {
  68. return scale*gain_sq4Tbl[index];
  69. }
  70. else if (cblen==32) {
  71. return scale*gain_sq5Tbl[index];
  72. }
  73. return 0.0;
  74. }