xref: /aosp_15_r20/external/libopus/dnn/dump_lpcnet_tables.c (revision a58d3d2adb790c104798cd88c8a3aff4fa8b82cc)
1 /* Copyright (c) 2017-2018 Mozilla
2    Copyright (c) 2023 Amazon */
3 /*
4    Redistribution and use in source and binary forms, with or without
5    modification, are permitted provided that the following conditions
6    are met:
7 
8    - Redistributions of source code must retain the above copyright
9    notice, this list of conditions and the following disclaimer.
10 
11    - Redistributions in binary form must reproduce the above copyright
12    notice, this list of conditions and the following disclaimer in the
13    documentation and/or other materials provided with the distribution.
14 
15    THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16    ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17    LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
18    A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR
19    CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
20    EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21    PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
22    PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
23    LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
24    NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25    SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27 
28 #ifdef HAVE_CONFIG_H
29 #include "config.h"
30 #endif
31 
32 #include <math.h>
33 #include <stdio.h>
34 #include "freq.h"
35 #include "kiss_fft.h"
36 
37 
main(void)38 int main(void) {
39   int i;
40   FILE *file;
41   kiss_fft_state *kfft;
42   float half_window[OVERLAP_SIZE];
43   float dct_table[NB_BANDS*NB_BANDS];
44 
45   file=fopen("lpcnet_tables.c", "wb");
46   fprintf(file, "/* The contents of this file was automatically generated by dump_lpcnet_tables.c*/\n\n");
47   fprintf(file, "#ifdef HAVE_CONFIG_H\n");
48   fprintf(file, "#include \"config.h\"\n");
49   fprintf(file, "#endif\n");
50 
51   fprintf(file, "#include \"kiss_fft.h\"\n\n");
52 
53   kfft = opus_fft_alloc_twiddles(WINDOW_SIZE, NULL, NULL, NULL, 0);
54 
55   fprintf(file, "static const arch_fft_state arch_fft = {0, NULL};\n\n");
56 
57   fprintf (file, "static const opus_int16 fft_bitrev[%d] = {\n", kfft->nfft);
58   for (i=0;i<kfft->nfft;i++)
59     fprintf (file, "%d,%c", kfft->bitrev[i],(i+16)%15==0?'\n':' ');
60   fprintf (file, "};\n\n");
61 
62   fprintf (file, "static const kiss_twiddle_cpx fft_twiddles[%d] = {\n", kfft->nfft);
63   for (i=0;i<kfft->nfft;i++)
64     fprintf (file, "{%#0.9gf, %#0.9gf},%c", kfft->twiddles[i].r, kfft->twiddles[i].i,(i+3)%2==0?'\n':' ');
65   fprintf (file, "};\n\n");
66 
67 
68   fprintf(file, "const kiss_fft_state kfft = {\n");
69   fprintf(file, "%d, /* nfft */\n", kfft->nfft);
70   fprintf(file, "%#0.8gf, /* scale */\n", kfft->scale);
71   fprintf(file, "%d, /* shift */\n", kfft->shift);
72   fprintf(file, "{");
73   for (i=0;i<2*MAXFACTORS;i++) {
74     fprintf(file, "%d, ", kfft->factors[i]);
75   }
76   fprintf(file, "}, /* factors */\n");
77   fprintf(file, "fft_bitrev, /* bitrev*/\n");
78   fprintf(file, "fft_twiddles, /* twiddles*/\n");
79   fprintf(file, "(arch_fft_state *)&arch_fft, /* arch_fft*/\n");
80 
81   fprintf(file, "};\n\n");
82 
83   for (i=0;i<OVERLAP_SIZE;i++)
84     half_window[i] = sin(.5*M_PI*sin(.5*M_PI*(i+.5)/OVERLAP_SIZE) * sin(.5*M_PI*(i+.5)/OVERLAP_SIZE));
85   fprintf(file, "const float half_window[] = {\n");
86   for (i=0;i<OVERLAP_SIZE;i++)
87     fprintf (file, "%#0.9gf,%c", half_window[i],(i+6)%5==0?'\n':' ');
88   fprintf(file, "};\n\n");
89 
90   for (i=0;i<NB_BANDS;i++) {
91     int j;
92     for (j=0;j<NB_BANDS;j++) {
93       dct_table[i*NB_BANDS + j] = cos((i+.5)*j*M_PI/NB_BANDS);
94       if (j==0) dct_table[i*NB_BANDS + j] *= sqrt(.5);
95     }
96   }
97   fprintf(file, "const float dct_table[] = {\n");
98   for (i=0;i<NB_BANDS*NB_BANDS;i++)
99     fprintf (file, "%#0.9gf,%c", dct_table[i],(i+6)%5==0?'\n':' ');
100   fprintf(file, "};\n");
101 
102   fclose(file);
103   return 0;
104 }
105