comparison libtwamr/cor_h.c @ 315:5401aaf7acb0

libtwamr: integrate cor_h.c
author Mychaela Falconia <falcon@freecalypso.org>
date Thu, 18 Apr 2024 17:39:10 +0000
parents
children
comparison
equal deleted inserted replaced
314:15c354f75110 315:5401aaf7acb0
1 /*
2 *****************************************************************************
3 *
4 * GSM AMR-NB speech codec R98 Version 7.6.0 December 12, 2001
5 * R99 Version 3.3.0
6 * REL-4 Version 4.1.0
7 *
8 ******************************************************************************
9 *
10 * File : cor_h.c
11 * Purpose : correlation functions for codebook search
12 *
13 *****************************************************************************
14 */
15 /*
16 *****************************************************************************
17 * MODULE INCLUDE FILE AND VERSION ID
18 *****************************************************************************
19 */
20 #include "namespace.h"
21 #include "cor_h.h"
22 /*
23 *****************************************************************************
24 * INCLUDE FILES
25 *****************************************************************************
26 */
27 #include "typedef.h"
28 #include "basic_op.h"
29 #include "no_count.h"
30 #include "inv_sqrt.h"
31 #include "cnst.h"
32
33 /*
34 *****************************************************************************
35 * PUBLIC PROGRAM CODE
36 *****************************************************************************
37 */
38 /*************************************************************************
39 *
40 * FUNCTION: cor_h_x()
41 *
42 * PURPOSE: Computes correlation between target signal "x[]" and
43 * impulse response"h[]".
44 *
45 * DESCRIPTION:
46 * The correlation is given by:
47 * d[n] = sum_{i=n}^{L-1} x[i] h[i-n] n=0,...,L-1
48 *
49 * d[n] is normalized such that the sum of 5 maxima of d[n] corresponding
50 * to each position track does not saturate.
51 *
52 *************************************************************************/
53 void cor_h_x (
54 Word16 h[], /* (i): impulse response of weighted synthesis filter */
55 Word16 x[], /* (i): target */
56 Word16 dn[], /* (o): correlation between target and h[] */
57 Word16 sf /* (i): scaling factor: 2 for 12.2, 1 for others */
58 )
59 {
60 cor_h_x2(h, x, dn, sf, NB_TRACK, STEP);
61 }
62
63 /*************************************************************************
64 *
65 * FUNCTION: cor_h_x2()
66 *
67 * PURPOSE: Computes correlation between target signal "x[]" and
68 * impulse response"h[]".
69 *
70 * DESCRIPTION:
71 * See cor_h_x, d[n] can be normalized regards to sum of the
72 * five MR122 maxima or the four MR102 maxima.
73 *
74 *************************************************************************/
75 void cor_h_x2 (
76 Word16 h[], /* (i): impulse response of weighted synthesis filter */
77 Word16 x[], /* (i): target */
78 Word16 dn[], /* (o): correlation between target and h[] */
79 Word16 sf, /* (i): scaling factor: 2 for 12.2, 1 for others */
80 Word16 nb_track,/* (i): the number of ACB tracks */
81 Word16 step /* (i): step size from one pulse position to the next
82 in one track */
83 )
84 {
85 Word16 i, j, k;
86 Word32 s, y32[L_CODE], max, tot;
87
88 /* first keep the result on 32 bits and find absolute maximum */
89
90 tot = 5; move32 ();
91
92 for (k = 0; k < nb_track; k++)
93 {
94 max = 0; move32 ();
95 for (i = k; i < L_CODE; i += step)
96 {
97 s = 0; move32 ();
98 for (j = i; j < L_CODE; j++)
99 s = L_mac (s, x[j], h[j - i]);
100
101 y32[i] = s; move32 ();
102
103 s = L_abs (s);
104 test ();
105 if (L_sub (s, max) > (Word32) 0L)
106 max = s; move32 ();
107 }
108 tot = L_add (tot, L_shr (max, 1));
109 }
110
111 j = sub (norm_l (tot), sf);
112
113 for (i = 0; i < L_CODE; i++)
114 {
115 dn[i] = round (L_shl (y32[i], j)); move16 ();
116 }
117 }
118
119 /*************************************************************************
120 *
121 * FUNCTION: cor_h()
122 *
123 * PURPOSE: Computes correlations of h[] needed for the codebook search;
124 * and includes the sign information into the correlations.
125 *
126 * DESCRIPTION: The correlations are given by
127 * rr[i][j] = sum_{n=i}^{L-1} h[n-i] h[n-j]; i>=j; i,j=0,...,L-1
128 *
129 * and the sign information is included by
130 * rr[i][j] = rr[i][j]*sign[i]*sign[j]
131 *
132 *************************************************************************/
133
134 void cor_h (
135 Word16 h[], /* (i) : impulse response of weighted synthesis
136 filter */
137 Word16 sign[], /* (i) : sign of d[n] */
138 Word16 rr[][L_CODE] /* (o) : matrix of autocorrelation */
139 )
140 {
141 Word16 i, j, k, dec, h2[L_CODE];
142 Word32 s;
143
144 /* Scaling for maximum precision */
145
146 s = 2; move32 ();
147 for (i = 0; i < L_CODE; i++)
148 s = L_mac (s, h[i], h[i]);
149
150 j = sub (extract_h (s), 32767);
151 test ();
152 if (j == 0)
153 {
154 for (i = 0; i < L_CODE; i++)
155 {
156 h2[i] = shr (h[i], 1); move16 ();
157 }
158 }
159 else
160 {
161 s = L_shr (s, 1);
162 k = extract_h (L_shl (Inv_sqrt (s), 7));
163 k = mult (k, 32440); /* k = 0.99*k */
164
165 for (i = 0; i < L_CODE; i++)
166 {
167 h2[i] = round (L_shl (L_mult (h[i], k), 9));
168 move16 ();
169 }
170 }
171
172 /* build matrix rr[] */
173 s = 0; move32 ();
174 i = L_CODE - 1;
175 for (k = 0; k < L_CODE; k++, i--)
176 {
177 s = L_mac (s, h2[k], h2[k]);
178 rr[i][i] = round (s); move16 ();
179 }
180
181 for (dec = 1; dec < L_CODE; dec++)
182 {
183 s = 0; move32 ();
184 j = L_CODE - 1;
185 i = sub (j, dec);
186 for (k = 0; k < (L_CODE - dec); k++, i--, j--)
187 {
188 s = L_mac (s, h2[k], h2[k + dec]);
189 rr[j][i] = mult (round (s), mult (sign[i], sign[j]));
190 move16 ();
191 rr[i][j] = rr[j][i]; move16 ();
192 }
193 }
194 }