Asterisk - The Open Source Telephony Project GIT-master-2de1a68
gainquant.c
Go to the documentation of this file.
1
2 /******************************************************************
3
4 iLBC Speech Coder ANSI-C Source Code
5
6
7
8
9
10
11 gainquant.c
12
13 Copyright (C) The Internet Society (2004).
14 All Rights Reserved.
15
16 ******************************************************************/
17
18 #include <string.h>
19 #include <math.h>
20 #include "constants.h"
21 #include "filter.h"
22
23 /*----------------------------------------------------------------*
24 * quantizer for the gain in the gain-shape coding of residual
25 *---------------------------------------------------------------*/
26
27 float gainquant(/* (o) quantized gain value */
28 float in, /* (i) gain value */
29 float maxIn,/* (i) maximum of gain value */
30 int cblen, /* (i) number of quantization indices */
31 int *index /* (o) quantization index */
32 ){
33 int i, tindex;
34 float minmeasure,measure, *cb, scale;
35
36 /* ensure a lower bound on the scaling factor */
37
38 scale=maxIn;
39
40 if (scale<0.1) {
41 scale=(float)0.1;
42 }
43
44 /* select the quantization table */
45
46 if (cblen == 8) {
47 cb = gain_sq3Tbl;
48 } else if (cblen == 16) {
49 cb = gain_sq4Tbl;
50 } else {
51 cb = gain_sq5Tbl;
52 }
53
54 /* select the best index in the quantization table */
55
56 minmeasure=10000000.0;
57 tindex=0;
58 for (i=0; i<cblen; i++) {
59
60
61
62
63
64 measure=(in-scale*cb[i])*(in-scale*cb[i]);
65
66 if (measure<minmeasure) {
67 tindex=i;
68 minmeasure=measure;
69 }
70 }
71 *index=tindex;
72
73 /* return the quantized value */
74
75 return scale*cb[tindex];
76 }
77
78 /*----------------------------------------------------------------*
79 * decoder for quantized gains in the gain-shape coding of
80 * residual
81 *---------------------------------------------------------------*/
82
83 float gaindequant( /* (o) quantized gain value */
84 int index, /* (i) quantization index */
85 float maxIn,/* (i) maximum of unquantized gain */
86 int cblen /* (i) number of quantization indices */
87 ){
88 float scale;
89
90 /* obtain correct scale factor */
91
92 scale=(float)fabs(maxIn);
93
94 if (scale<0.1) {
95 scale=(float)0.1;
96 }
97
98 /* select the quantization table and return the decoded value */
99
100 if (cblen==8) {
101 return scale*gain_sq3Tbl[index];
102 } else if (cblen==16) {
103 return scale*gain_sq4Tbl[index];
104 }
105 else if (cblen==32) {
106 return scale*gain_sq5Tbl[index];
107 }
108
109 return 0.0;
110 }
float gain_sq4Tbl[16]
Definition: constants.c:160
float gain_sq5Tbl[32]
Definition: constants.c:168
float gain_sq3Tbl[8]
Definition: constants.c:150
float gaindequant(int index, float maxIn, int cblen)
Definition: gainquant.c:83
float gainquant(float in, float maxIn, int cblen, int *index)
Definition: gainquant.c:27
FILE * in
Definition: utils/frame.c:33