xref: /aosp_15_r20/external/libopus/dnn/adaconvtest.c (revision a58d3d2adb790c104798cd88c8a3aff4fa8b82cc)
1 #include "lace_data.h"
2 #include "nolace_data.h"
3 #include "osce.h"
4 #include "nndsp.h"
5 
6 
7 #include <stdlib.h>
8 #include <stdio.h>
9 #include <math.h>
10 
11 
12 extern const WeightArray lacelayers_arrays[];
13 extern const WeightArray nolacelayers_arrays[];
14 
adaconv_compare(const char * prefix,int num_frames,AdaConvState * hAdaConv,LinearLayer * kernel_layer,LinearLayer * gain_layer,int feature_dim,int frame_size,int overlap_size,int in_channels,int out_channels,int kernel_size,int left_padding,float filter_gain_a,float filter_gain_b,float shape_gain)15 void adaconv_compare(
16     const char * prefix,
17     int num_frames,
18     AdaConvState* hAdaConv,
19     LinearLayer *kernel_layer,
20     LinearLayer *gain_layer,
21     int feature_dim,
22     int frame_size,
23     int overlap_size,
24     int in_channels,
25     int out_channels,
26     int kernel_size,
27     int left_padding,
28     float filter_gain_a,
29     float filter_gain_b,
30     float shape_gain
31 )
32 {
33     char feature_file[256];
34     char x_in_file[256];
35     char x_out_file[256];
36     char message[512];
37     int i_frame, i_sample;
38     float mse;
39     float features[512];
40     float x_in[512];
41     float x_out_ref[512];
42     float x_out[512];
43     float window[40];
44 
45     init_adaconv_state(hAdaConv);
46     compute_overlap_window(window, 40);
47 
48     FILE *f_features, *f_x_in, *f_x_out;
49 
50     strcpy(feature_file, prefix);
51     strcat(feature_file, "_features.f32");
52     f_features = fopen(feature_file, "r");
53     if (f_features == NULL)
54     {
55         sprintf(message, "could not open file %s", feature_file);
56         perror(message);
57         exit(1);
58     }
59 
60     strcpy(x_in_file, prefix);
61     strcat(x_in_file, "_x_in.f32");
62     f_x_in = fopen(x_in_file, "r");
63     if (f_x_in == NULL)
64     {
65         sprintf(message, "could not open file %s", x_in_file);
66         perror(message);
67         exit(1);
68     }
69 
70     strcpy(x_out_file, prefix);
71     strcat(x_out_file, "_x_out.f32");
72     f_x_out = fopen(x_out_file, "r");
73     if (f_x_out == NULL)
74     {
75         sprintf(message, "could not open file %s", x_out_file);
76         perror(message);
77         exit(1);
78     }
79 
80     for (i_frame = 0; i_frame < num_frames; i_frame ++)
81     {
82         if (fread(features, sizeof(float), feature_dim, f_features) != feature_dim)
83         {
84             fprintf(stderr, "could not read frame %d from %s\n", i_frame, feature_file);
85             exit(1);
86         }
87 
88         if (fread(x_in, sizeof(float), frame_size * in_channels, f_x_in) != frame_size * in_channels)
89         {
90             fprintf(stderr, "could not read frame %d from %s\n", i_frame, x_in_file);
91             exit(1);
92         }
93 
94         if (fread(x_out_ref, sizeof(float), frame_size * out_channels, f_x_out) != frame_size * out_channels)
95         {
96             fprintf(stderr, "could not read frame %d from %s\n", i_frame, x_out_file);
97             exit(1);
98         }
99 
100         adaconv_process_frame(hAdaConv, x_out, x_in, features, kernel_layer, gain_layer, feature_dim,
101             frame_size, overlap_size, in_channels, out_channels, kernel_size, left_padding,
102             filter_gain_a, filter_gain_b, shape_gain, window, 0);
103 
104         mse = 0;
105         for (i_sample = 0; i_sample < frame_size * out_channels; i_sample ++)
106         {
107             mse += pow(x_out_ref[i_sample] - x_out[i_sample], 2);
108         }
109         mse = sqrt(mse / (frame_size * out_channels));
110         printf("rmse[%d] %f\n", i_frame, mse);
111 
112     }
113 }
114 
115 
adacomb_compare(const char * prefix,int num_frames,AdaCombState * hAdaComb,LinearLayer * kernel_layer,LinearLayer * gain_layer,LinearLayer * global_gain_layer,int feature_dim,int frame_size,int overlap_size,int kernel_size,int left_padding,float filter_gain_a,float filter_gain_b,float log_gain_limit)116 void adacomb_compare(
117     const char * prefix,
118     int num_frames,
119     AdaCombState* hAdaComb,
120     LinearLayer *kernel_layer,
121     LinearLayer *gain_layer,
122     LinearLayer *global_gain_layer,
123     int feature_dim,
124     int frame_size,
125     int overlap_size,
126     int kernel_size,
127     int left_padding,
128     float filter_gain_a,
129     float filter_gain_b,
130     float log_gain_limit
131 )
132 {
133     char feature_file[256];
134     char x_in_file[256];
135     char p_in_file[256];
136     char x_out_file[256];
137     char message[512];
138     int i_frame, i_sample;
139     float mse;
140     float features[512];
141     float x_in[512];
142     float x_out_ref[512];
143     float x_out[512];
144     int pitch_lag;
145     float window[40];
146 
147     init_adacomb_state(hAdaComb);
148     compute_overlap_window(window, 40);
149 
150     FILE *f_features, *f_x_in, *f_p_in, *f_x_out;
151 
152     strcpy(feature_file, prefix);
153     strcat(feature_file, "_features.f32");
154     f_features = fopen(feature_file, "r");
155     if (f_features == NULL)
156     {
157         sprintf(message, "could not open file %s", feature_file);
158         perror(message);
159         exit(1);
160     }
161 
162     strcpy(x_in_file, prefix);
163     strcat(x_in_file, "_x_in.f32");
164     f_x_in = fopen(x_in_file, "r");
165     if (f_x_in == NULL)
166     {
167         sprintf(message, "could not open file %s", x_in_file);
168         perror(message);
169         exit(1);
170     }
171 
172     strcpy(p_in_file, prefix);
173     strcat(p_in_file, "_p_in.s32");
174     f_p_in = fopen(p_in_file, "r");
175     if (f_p_in == NULL)
176     {
177         sprintf(message, "could not open file %s", p_in_file);
178         perror(message);
179         exit(1);
180     }
181 
182     strcpy(x_out_file, prefix);
183     strcat(x_out_file, "_x_out.f32");
184     f_x_out = fopen(x_out_file, "r");
185     if (f_x_out == NULL)
186     {
187         sprintf(message, "could not open file %s", x_out_file);
188         perror(message);
189         exit(1);
190     }
191 
192     for (i_frame = 0; i_frame < num_frames; i_frame ++)
193     {
194         if (fread(features, sizeof(float), feature_dim, f_features) != feature_dim)
195         {
196             fprintf(stderr, "could not read frame %d from %s\n", i_frame, feature_file);
197             exit(1);
198         }
199 
200         if (fread(x_in, sizeof(float), frame_size, f_x_in) != frame_size)
201         {
202             fprintf(stderr, "could not read frame %d from %s\n", i_frame, x_in_file);
203             exit(1);
204         }
205 
206         if (fread(&pitch_lag, sizeof(int), 1, f_p_in) != 1)
207         {
208             fprintf(stderr, "could not read frame %d from %s\n", i_frame, p_in_file);
209             exit(1);
210         }
211 
212         if (fread(x_out_ref, sizeof(float), frame_size, f_x_out) != frame_size)
213         {
214             fprintf(stderr, "could not read frame %d from %s\n", i_frame, x_out_file);
215             exit(1);
216         }
217 
218         adacomb_process_frame(hAdaComb, x_out, x_in, features, kernel_layer, gain_layer, global_gain_layer,
219             pitch_lag, feature_dim, frame_size, overlap_size, kernel_size, left_padding, filter_gain_a, filter_gain_b, log_gain_limit, window, 0);
220 
221 
222         mse = 0;
223         for (i_sample = 0; i_sample < frame_size; i_sample ++)
224         {
225             mse += pow(x_out_ref[i_sample] - x_out[i_sample], 2);
226         }
227         mse = sqrt(mse / (frame_size));
228         printf("rmse[%d] %f\n", i_frame, mse);
229 
230     }
231 }
232 
adashape_compare(const char * prefix,int num_frames,AdaShapeState * hAdaShape,LinearLayer * alpha1,LinearLayer * alpha2,int feature_dim,int frame_size,int avg_pool_k)233 void adashape_compare(
234     const char * prefix,
235     int num_frames,
236     AdaShapeState* hAdaShape,
237     LinearLayer *alpha1,
238     LinearLayer *alpha2,
239     int feature_dim,
240     int frame_size,
241     int avg_pool_k
242 )
243 {
244     char feature_file[256];
245     char x_in_file[256];
246     char x_out_file[256];
247     char message[512];
248     int i_frame, i_sample;
249     float mse;
250     float features[512];
251     float x_in[512];
252     float x_out_ref[512];
253     float x_out[512];
254 
255     init_adashape_state(hAdaShape);
256 
257     FILE *f_features, *f_x_in, *f_x_out;
258 
259     strcpy(feature_file, prefix);
260     strcat(feature_file, "_features.f32");
261     f_features = fopen(feature_file, "r");
262     if (f_features == NULL)
263     {
264         sprintf(message, "could not open file %s", feature_file);
265         perror(message);
266         exit(1);
267     }
268 
269     strcpy(x_in_file, prefix);
270     strcat(x_in_file, "_x_in.f32");
271     f_x_in = fopen(x_in_file, "r");
272     if (f_x_in == NULL)
273     {
274         sprintf(message, "could not open file %s", x_in_file);
275         perror(message);
276         exit(1);
277     }
278 
279     strcpy(x_out_file, prefix);
280     strcat(x_out_file, "_x_out.f32");
281     f_x_out = fopen(x_out_file, "r");
282     if (f_x_out == NULL)
283     {
284         sprintf(message, "could not open file %s", x_out_file);
285         perror(message);
286         exit(1);
287     }
288 
289     for (i_frame = 0; i_frame < num_frames; i_frame ++)
290     {
291         if (fread(features, sizeof(float), feature_dim, f_features) != feature_dim)
292         {
293             fprintf(stderr, "could not read frame %d from %s\n", i_frame, feature_file);
294             exit(1);
295         }
296 
297         if (fread(x_in, sizeof(float), frame_size, f_x_in) != frame_size)
298         {
299             fprintf(stderr, "could not read frame %d from %s\n", i_frame, x_in_file);
300             exit(1);
301         }
302 
303         if (fread(x_out_ref, sizeof(float), frame_size, f_x_out) != frame_size)
304         {
305             fprintf(stderr, "could not read frame %d from %s\n", i_frame, x_out_file);
306             exit(1);
307         }
308 
309         adashape_process_frame(hAdaShape, x_out, x_in, features, alpha1, alpha2, feature_dim,
310             frame_size, avg_pool_k, 0);
311 
312         mse = 0;
313         for (i_sample = 0; i_sample < frame_size; i_sample ++)
314         {
315             mse += pow(x_out_ref[i_sample] - x_out[i_sample], 2);
316         }
317         mse = sqrt(mse / (frame_size));
318         printf("rmse[%d] %f\n", i_frame, mse);
319 
320     }
321 }
322 
323 
main()324 int main()
325 {
326     LACELayers hLACE;
327     NOLACELayers hNoLACE;
328 
329     AdaConvState hAdaConv;
330     AdaCombState hAdaComb;
331     AdaShapeState hAdaShape;
332 
333     init_adaconv_state(&hAdaConv);
334 
335     init_lacelayers(&hLACE, lacelayers_arrays);
336     init_nolacelayers(&hNoLACE, nolacelayers_arrays);
337 
338     printf("\ntesting lace.af1 (1 in, 1 out)...\n");
339     adaconv_compare(
340         "testvectors/lace_af1",
341         5,
342         &hAdaConv,
343         &hLACE.lace_af1_kernel,
344         &hLACE.lace_af1_gain,
345         LACE_AF1_FEATURE_DIM,
346         LACE_AF1_FRAME_SIZE,
347         LACE_AF1_OVERLAP_SIZE,
348         LACE_AF1_IN_CHANNELS,
349         LACE_AF1_OUT_CHANNELS,
350         LACE_AF1_KERNEL_SIZE,
351         LACE_AF1_LEFT_PADDING,
352         LACE_AF1_FILTER_GAIN_A,
353         LACE_AF1_FILTER_GAIN_B,
354         LACE_AF1_SHAPE_GAIN
355     );
356 
357 
358     printf("\ntesting nolace.af1 (1 in, 2 out)...\n");
359     adaconv_compare(
360         "testvectors/nolace_af1",
361         5,
362         &hAdaConv,
363         &hNoLACE.nolace_af1_kernel,
364         &hNoLACE.nolace_af1_gain,
365         NOLACE_AF1_FEATURE_DIM,
366         NOLACE_AF1_FRAME_SIZE,
367         NOLACE_AF1_OVERLAP_SIZE,
368         NOLACE_AF1_IN_CHANNELS,
369         NOLACE_AF1_OUT_CHANNELS,
370         NOLACE_AF1_KERNEL_SIZE,
371         NOLACE_AF1_LEFT_PADDING,
372         NOLACE_AF1_FILTER_GAIN_A,
373         NOLACE_AF1_FILTER_GAIN_B,
374         NOLACE_AF1_SHAPE_GAIN
375     );
376 
377 
378     printf("testing nolace.af4 (2 in, 1 out)...\n");
379     adaconv_compare(
380         "testvectors/nolace_af4",
381         5,
382         &hAdaConv,
383         &hNoLACE.nolace_af4_kernel,
384         &hNoLACE.nolace_af4_gain,
385         NOLACE_AF4_FEATURE_DIM,
386         NOLACE_AF4_FRAME_SIZE,
387         NOLACE_AF4_OVERLAP_SIZE,
388         NOLACE_AF4_IN_CHANNELS,
389         NOLACE_AF4_OUT_CHANNELS,
390         NOLACE_AF4_KERNEL_SIZE,
391         NOLACE_AF4_LEFT_PADDING,
392         NOLACE_AF4_FILTER_GAIN_A,
393         NOLACE_AF4_FILTER_GAIN_B,
394         NOLACE_AF4_SHAPE_GAIN
395     );
396 
397     printf("\ntesting nolace.af2 (2 in, 2 out)...\n");
398     adaconv_compare(
399         "testvectors/nolace_af2",
400         5,
401         &hAdaConv,
402         &hNoLACE.nolace_af2_kernel,
403         &hNoLACE.nolace_af2_gain,
404         NOLACE_AF2_FEATURE_DIM,
405         NOLACE_AF2_FRAME_SIZE,
406         NOLACE_AF2_OVERLAP_SIZE,
407         NOLACE_AF2_IN_CHANNELS,
408         NOLACE_AF2_OUT_CHANNELS,
409         NOLACE_AF2_KERNEL_SIZE,
410         NOLACE_AF2_LEFT_PADDING,
411         NOLACE_AF2_FILTER_GAIN_A,
412         NOLACE_AF2_FILTER_GAIN_B,
413         NOLACE_AF2_SHAPE_GAIN
414     );
415 
416     printf("\ntesting lace.cf1...\n");
417     adacomb_compare(
418         "testvectors/lace_cf1",
419         5,
420         &hAdaComb,
421         &hLACE.lace_cf1_kernel,
422         &hLACE.lace_cf1_gain,
423         &hLACE.lace_cf1_global_gain,
424         LACE_CF1_FEATURE_DIM,
425         LACE_CF1_FRAME_SIZE,
426         LACE_CF1_OVERLAP_SIZE,
427         LACE_CF1_KERNEL_SIZE,
428         LACE_CF1_LEFT_PADDING,
429         LACE_CF1_FILTER_GAIN_A,
430         LACE_CF1_FILTER_GAIN_B,
431         LACE_CF1_LOG_GAIN_LIMIT
432     );
433 
434     printf("\ntesting nolace.tdshape1...\n");
435     adashape_compare(
436         "testvectors/nolace_tdshape1",
437         5,
438         &hAdaShape,
439         &hNoLACE.nolace_tdshape1_alpha1,
440         &hNoLACE.nolace_tdshape1_alpha2,
441         NOLACE_TDSHAPE1_FEATURE_DIM,
442         NOLACE_TDSHAPE1_FRAME_SIZE,
443         NOLACE_TDSHAPE1_AVG_POOL_K
444     );
445 
446     return 0;
447 }
448 
449 /* gcc -DVAR_ARRAYS -DENABLE_OSCE  -I ../include -I ../silk -I . -I ../celt adaconvtest.c nndsp.c lace_data.c nolace_data.c nnet.c nnet_default.c ../celt/pitch.c ../celt/celt_lpc.c parse_lpcnet_weights.c -lm -o adaconvtest */