comparison libtwamr/pstfilt.c @ 401:59655481e45b

libtwamr: integrate pstfilt.c
author Mychaela Falconia <falcon@freecalypso.org>
date Mon, 06 May 2024 19:44:15 +0000
parents
children
comparison
equal deleted inserted replaced
400:ffd48f0a2ab5 401:59655481e45b
1 /*************************************************************************
2 *
3 * GSM AMR-NB speech codec R98 Version 7.6.0 December 12, 2001
4 * R99 Version 3.3.0
5 * REL-4 Version 4.1.0
6 *
7 ********************************************************************************
8 *
9 * File : pstfilt.c
10 * Purpose : Performs adaptive postfiltering on the synthesis
11 * : speech
12 *
13 ********************************************************************************
14 */
15
16 /*
17 ********************************************************************************
18 * MODULE INCLUDE FILE AND VERSION ID
19 ********************************************************************************
20 */
21 #include "namespace.h"
22 #include "pstfilt.h"
23
24 /*
25 ********************************************************************************
26 * INCLUDE FILES
27 ********************************************************************************
28 */
29 #include "tw_amr.h"
30 #include "typedef.h"
31 #include "basic_op.h"
32 #include "memops.h"
33 #include "weight_a.h"
34 #include "residu.h"
35 #include "syn_filt.h"
36 #include "preemph.h"
37 #include "no_count.h"
38 #include "cnst.h"
39
40 /*
41 ********************************************************************************
42 * LOCAL VARIABLES AND TABLES
43 ********************************************************************************
44 */
45 /*---------------------------------------------------------------*
46 * Postfilter constant parameters (defined in "cnst.h") *
47 *---------------------------------------------------------------*
48 * L_FRAME : Frame size. *
49 * L_SUBFR : Sub-frame size. *
50 * M : LPC order. *
51 * MP1 : LPC order+1 *
52 * MU : Factor for tilt compensation filter *
53 * AGC_FAC : Factor for automatic gain control *
54 *---------------------------------------------------------------*/
55
56 #define L_H 22 /* size of truncated impulse response of A(z/g1)/A(z/g2) */
57
58 /* Spectral expansion factors */
59 static const Word16 gamma3_MR122[M] = {
60 22938, 16057, 11240, 7868, 5508,
61 3856, 2699, 1889, 1322, 925
62 };
63
64 static const Word16 gamma3[M] = {
65 18022, 9912, 5451, 2998, 1649, 907, 499, 274, 151, 83
66 };
67
68 static const Word16 gamma4_MR122[M] = {
69 24576, 18432, 13824, 10368, 7776,
70 5832, 4374, 3281, 2461, 1846
71 };
72
73 static const Word16 gamma4[M] = {
74 22938, 16057, 11240, 7868, 5508, 3856, 2699, 1889, 1322, 925
75 };
76
77 /*
78 ********************************************************************************
79 * PUBLIC PROGRAM CODE
80 ********************************************************************************
81 */
82
83 /*************************************************************************
84 *
85 * Function: Post_Filter_reset
86 * Purpose: Initializes state memory to zero
87 *
88 **************************************************************************
89 */
90 void Post_Filter_reset (Post_FilterState *state)
91 {
92 Set_zero (state->mem_syn_pst, M);
93 Set_zero (state->res2, L_SUBFR);
94 Set_zero (state->synth_buf, L_FRAME + M);
95 agc_reset(&state->agc_state);
96 preemphasis_reset(&state->preemph_state);
97 }
98
99 /*
100 **************************************************************************
101 * Function: Post_Filter
102 * Purpose: postfiltering of synthesis speech.
103 * Description:
104 * The postfiltering process is described as follows:
105 *
106 * - inverse filtering of syn[] through A(z/0.7) to get res2[]
107 * - tilt compensation filtering; 1 - MU*k*z^-1
108 * - synthesis filtering through 1/A(z/0.75)
109 * - adaptive gain control
110 *
111 **************************************************************************
112 */
113 int Post_Filter (
114 Post_FilterState *st, /* i/o : post filter states */
115 enum Mode mode, /* i : AMR mode */
116 Word16 *syn, /* i/o : synthesis speech (postfiltered is output) */
117 Word16 *Az_4 /* i : interpolated LPC parameters in all subfr. */
118 )
119 {
120 /*-------------------------------------------------------------------*
121 * Declaration of parameters *
122 *-------------------------------------------------------------------*/
123
124 Word16 Ap3[MP1], Ap4[MP1]; /* bandwidth expanded LP parameters */
125 Word16 *Az; /* pointer to Az_4: */
126 /* LPC parameters in each subframe */
127 Word16 i_subfr; /* index for beginning of subframe */
128 Word16 h[L_H];
129
130 Word16 i;
131 Word16 temp1, temp2;
132 Word32 L_tmp;
133 Word16 *syn_work = &st->synth_buf[M]; move16 ();
134
135 /*-----------------------------------------------------*
136 * Post filtering *
137 *-----------------------------------------------------*/
138
139 Copy (syn, syn_work , L_FRAME);
140
141 Az = Az_4;
142
143 for (i_subfr = 0; i_subfr < L_FRAME; i_subfr += L_SUBFR)
144 {
145 /* Find weighted filter coefficients Ap3[] and ap[4] */
146
147 test (); test ();
148 if (sub(mode, MR122) == 0 || sub(mode, MR102) == 0)
149 {
150 Weight_Ai (Az, gamma3_MR122, Ap3);
151 Weight_Ai (Az, gamma4_MR122, Ap4);
152 }
153 else
154 {
155 Weight_Ai (Az, gamma3, Ap3);
156 Weight_Ai (Az, gamma4, Ap4);
157 }
158
159 /* filtering of synthesis speech by A(z/0.7) to find res2[] */
160
161 Residu (Ap3, &syn_work[i_subfr], st->res2, L_SUBFR);
162
163 /* tilt compensation filter */
164
165 /* impulse response of A(z/0.7)/A(z/0.75) */
166
167 Copy (Ap3, h, M + 1);
168 Set_zero (&h[M + 1], L_H - M - 1);
169 Syn_filt (Ap4, h, h, L_H, &h[M + 1], 0);
170
171 /* 1st correlation of h[] */
172
173 L_tmp = L_mult (h[0], h[0]);
174 for (i = 1; i < L_H; i++)
175 {
176 L_tmp = L_mac (L_tmp, h[i], h[i]);
177 }
178 temp1 = extract_h (L_tmp);
179
180 L_tmp = L_mult (h[0], h[1]);
181 for (i = 1; i < L_H - 1; i++)
182 {
183 L_tmp = L_mac (L_tmp, h[i], h[i + 1]);
184 }
185 temp2 = extract_h (L_tmp);
186
187 test ();
188 if (temp2 <= 0)
189 {
190 temp2 = 0; move16 ();
191 }
192 else
193 {
194 temp2 = mult (temp2, MU);
195 temp2 = div_s (temp2, temp1);
196 }
197
198 preemphasis (&st->preemph_state, st->res2, temp2, L_SUBFR);
199
200 /* filtering through 1/A(z/0.75) */
201
202 Syn_filt (Ap4, st->res2, &syn[i_subfr], L_SUBFR, st->mem_syn_pst, 1);
203
204 /* scale output to input */
205
206 agc (&st->agc_state, &syn_work[i_subfr], &syn[i_subfr],
207 AGC_FAC, L_SUBFR);
208
209 Az += MP1;
210 }
211
212 /* update syn_work[] buffer */
213
214 Copy (&syn_work[L_FRAME - M], &syn_work[-M], M);
215
216 return 0;
217 }