-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathstringGenerators.py
More file actions
478 lines (383 loc) · 17.9 KB
/
stringGenerators.py
File metadata and controls
478 lines (383 loc) · 17.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
import bitrevIdxs
import valueGenerators as gens
tables_header_start = f"""#ifndef _ARM_EXTRA_TABLES_H
#define _ARM_EXTRA_TABLES_H
#include "arm_math_types.h"
#ifdef __cplusplus
extern "C"
{{
#endif
"""
tables_file_start = '#include "arm_math_types.h"\n\n#if !defined(ARM_DSP_CONFIG_TABLES) || defined(ARM_FFT_ALLOW_TABLES)\n'
def bitrev_table(stop_power):
bitrev = gens.bit_reversers(stop_power)
length_bitrev = len(bitrev)
header_string = f"""
#if !defined(ARM_DSP_CONFIG_TABLES) || defined(ARM_FFT_ALLOW_TABLES)
/* Double Precision Float CFFT twiddles */
#if !defined(ARM_DSP_CONFIG_TABLES) || defined(ARM_ALL_FFT_TABLES) || defined(ARM_TABLE_BITREV_{length_bitrev})
extern const uint16_t armBitRevTableExtra[{length_bitrev}];
#endif /* !defined(ARM_DSP_CONFIG_TABLES) || defined(ARM_ALL_FFT_TABLES) */
"""
table_string = f'#if !defined(ARM_DSP_CONFIG_TABLES) || defined(ARM_ALL_FFT_TABLES) || defined(ARM_TABLE_BITREV_{length_bitrev})\nconst uint16_t armBitRevTableExtra[{length_bitrev}] = {{\n '
table_string += (",\n ".join(
[', '.join(
f'0x{val:04x}' for val in bitrev[idx:idx+10]
)
for idx in range(0, length_bitrev, 10)]
))
table_string += ("};\n#endif\n\n")
return header_string, table_string
def bitrevidx_table(lengths):
table_string = ""
header_string = ""
for power, length in lengths:
tps = bitrevIdxs.create_transpositions(length, 8)
out = bitrevIdxs.transpositions_stringify(length, 8, tps)
if len(tps)*2 > 2**16-1:
print(f"warning: bitrevidx table for {length} is too long ({len(tps)*2}). Use a smaller value or figure out where to switch uint16_t to uint32_t")
table_string += f"#if !defined(ARM_DSP_CONFIG_TABLES) || defined(ARM_ALL_FFT_TABLES) || defined(ARM_TABLE_BITREVIDX_FLT_{length})\n"
table_string += out
table_string += "\n#endif /* !defined(ARM_DSP_CONFIG_TABLES) || defined(ARM_ALL_FFT_TABLES) */\n\n\n"
header_string += f""" #if !defined(ARM_DSP_CONFIG_TABLES) || defined(ARM_ALL_FFT_TABLES) || defined(ARM_TABLE_BITREVIDX_FLT_{length})
#define ARMBITREVINDEXTABLE_{length}_TABLE_LENGTH_EXTRA ((uint16_t){len(tps)*2})
extern const uint32_t armBitRevIndexTableExtra{length}[ARMBITREVINDEXTABLE_{length}_TABLE_LENGTH_EXTRA];
#endif /* !defined(ARM_DSP_CONFIG_TABLES) || defined(ARM_ALL_FFT_TABLES) */
"""
return header_string, table_string
def twiddle_complex_table(lengths):
table_string = ""
header_string = ""
for power, length in lengths:
twiddles = gens.twiddle_coefs_complex(length)
table_string += f"#if !defined(ARM_DSP_CONFIG_TABLES) || defined(ARM_ALL_FFT_TABLES) || defined(ARM_TABLE_TWIDDLECOEF_F32_{length*2})\n"
table_string += f'const float32_t twiddleCoefExtra_{length}[{length*2}] = {{\n'
table_string += "".join(f' {val.real:.9f}f, {val.imag:.9f}f,\n' for val in twiddles)
table_string += '};\n'
table_string += "#endif /* !defined(ARM_DSP_CONFIG_TABLES) || defined(ARM_ALL_FFT_TABLES) */\n\n"
header_string += f""" #if !defined(ARM_DSP_CONFIG_TABLES) || defined(ARM_ALL_FFT_TABLES) || defined(ARM_TABLE_TWIDDLECOEF_F32_{length})
extern const float32_t twiddleCoefExtra_{length}[{length*2}];
#endif /* !defined(ARM_DSP_CONFIG_TABLES) || defined(ARM_ALL_FFT_TABLES) */
"""
return header_string, table_string
def twiddle_real_table(lengths):
table_string = ""
header_string = ""
for power, length in lengths:
twiddles = gens.twiddle_coefs_real(length)
table_string += f"#if !defined(ARM_DSP_CONFIG_TABLES) || defined(ARM_ALL_FFT_TABLES) || defined(ARM_TABLE_TWIDDLECOEF_RFFT_F32_{length})\n"
table_string += f'const float32_t twiddleCoefExtra_rfft_{length}[{length}] = {{\n'
table_string += "".join(f' {val.real:.9f}f, {val.imag:.9f}f,\n' for val in twiddles)
table_string += '};\n'
table_string += "#endif /* !defined(ARM_DSP_CONFIG_TABLES) || defined(ARM_ALL_FFT_TABLES) */\n\n"
header_string += f""" #if !defined(ARM_DSP_CONFIG_TABLES) || defined(ARM_ALL_FFT_TABLES) || defined(ARM_TABLE_TWIDDLECOEF_RFFT_F32_{length})
extern const float32_t twiddleCoefExtra_rfft_{length}[{length}];
#endif /* !defined(ARM_DSP_CONFIG_TABLES) || defined(ARM_ALL_FFT_TABLES) */
"""
return header_string, table_string
tables_header_end = """
#endif /* if !defined(ARM_DSP_CONFIG_TABLES) || defined(ARM_FAST_TABLES) */
#ifdef __cplusplus
}
#endif
#endif /* ARM_EXTRA_TABLES_H */"""
tables_file_end = "\n#endif\n"
def rfft_init(lengths):
init_string = """
/* ----------------------------------------------------------------------
* Project: CMSIS DSP Library
* Title: arm_rfft_fast_init_f32_extra.c
* Description: Split Radix Decimation in Frequency CFFT Floating point processing function
*
* $Date: 23 April 2021
* $Revision: V1.9.0
*
* Target Processor: Cortex-M and Cortex-A cores
* -------------------------------------------------------------------- */
/*
* Copyright (C) 2010-2021 ARM Limited or its affiliates. All rights reserved.
*
* SPDX-License-Identifier: Apache-2.0
*
* Licensed under the Apache License, Version 2.0 (the License); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an AS IS BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "extra_ffts.h"
#include "arm_common_tables_extra.h"
/**
@ingroup groupTransforms
*/
/**
@addtogroup RealFFT
@{
*/
"""
# we can't do a rfft for the lowest power, since a rfft always includes a cfft of length/2
for power, length in lengths:
init_string += f"""
#if !defined(ARM_DSP_CONFIG_TABLES) || defined(ARM_ALL_FFT_TABLES) || (defined(ARM_TABLE_TWIDDLECOEF_F32_{length//2}) && (defined(ARM_TABLE_BITREVIDX_FLT_{length//2}) || defined(ARM_TABLE_BITREVIDX_FXT_{length//2})) && defined(ARM_TABLE_TWIDDLECOEF_RFFT_F32_{length}))
/**
@private
@brief Initialization function for the {length}pt floating-point real FFT.
@param[in,out] S points to an arm_rfft_fast_instance_f32_extra structure
@return execution status
- \\ref ARM_MATH_SUCCESS : Operation successful
- \\ref ARM_MATH_ARGUMENT_ERROR : an error is detected
*/
static arm_status arm_rfft_{length}_fast_init_f32( arm_rfft_fast_instance_f32_extra * S ) {{
arm_status status;
if( !S ) return ARM_MATH_ARGUMENT_ERROR;
status=arm_cfft_init_f32_extra(&(S->Sint),{length//2});
if (status != ARM_MATH_SUCCESS)
{{
return(status);
}}
S->fftLenRFFT = {length}U;
S->pTwiddleRFFT = (float32_t *) twiddleCoefExtra_rfft_{length};
return ARM_MATH_SUCCESS;
}}
#endif
"""
init_string += f"""
/**
@brief Initialization function for the floating-point real FFT.
@param[in,out] S points to an arm_rfft_fast_instance_f32_extra structure
@param[in] fftLen length of the Real Sequence
@return execution status
- \\ref ARM_MATH_SUCCESS : Operation successful
- \\ref ARM_MATH_ARGUMENT_ERROR : <code>fftLen</code> is not a supported length
@par Description
The parameter <code>fftLen</code> specifies the length of RFFT/CIFFT process.
Supported FFT Lengths are {", ".join(str(2**power) for power, _ in lengths)}.
@par
This Function also initializes Twiddle factor table pointer and Bit reversal table pointer.
*/
arm_status arm_rfft_fast_init_f32_extra(
arm_rfft_fast_instance_f32_extra * S,
uint16_t fftLen)
{{
typedef arm_status(*fft_init_ptr)( arm_rfft_fast_instance_f32_extra *);
fft_init_ptr fptr = 0x0;
switch (fftLen)
{{
"""
for power, length in lengths[::-1]:
init_string += f"""
#if !defined(ARM_DSP_CONFIG_TABLES) || defined(ARM_ALL_FFT_TABLES) || (defined(ARM_TABLE_TWIDDLECOEF_F32_{length//2}) && (defined(ARM_TABLE_BITREVIDX_FLT_{length//2}) || defined(ARM_TABLE_BITREVIDX_FXT_{length//2})) && defined(ARM_TABLE_TWIDDLECOEF_RFFT_F32_{length}))
case {length}U:
fptr = arm_rfft_{length}_fast_init_f32;
break;
#endif
"""
init_string += """
default:
break;
}
if( ! fptr ) return ARM_MATH_ARGUMENT_ERROR;
return fptr( S );
}
/**
@} end of RealFFT group
*/
"""
return init_string
def cfft_init(lengths):
init_string = """
/* ----------------------------------------------------------------------
* Project: CMSIS DSP Library
* Title: arm_cfft_init_f32_extra.c
* Description: Initialization function for cfft f32 instance
*
* $Date: 23 April 2021
* $Revision: V1.9.0
*
* Target Processor: Cortex-M and Cortex-A cores
* -------------------------------------------------------------------- */
/*
* Copyright (C) 2010-2021 ARM Limited or its affiliates. All rights reserved.
*
* SPDX-License-Identifier: Apache-2.0
*
* Licensed under the Apache License, Version 2.0 (the License); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an AS IS BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#define FFTINIT(EXT,SIZE) \\
S->bitRevLength = arm_cfft_sR_##EXT##_len##SIZE##_extra.bitRevLength; \\
S->pBitRevTable = arm_cfft_sR_##EXT##_len##SIZE##_extra.pBitRevTable; \\
S->pTwiddle = arm_cfft_sR_##EXT##_len##SIZE##_extra.pTwiddle;
/**
@addtogroup ComplexFFT
@{
*/
/**
@brief Initialization function for the cfft f32 function
@param[in,out] S points to an instance of the floating-point CFFT structure
@param[in] fftLen fft length (number of complex samples)
@return execution status
- \\ref ARM_MATH_SUCCESS : Operation successful
- \\ref ARM_MATH_ARGUMENT_ERROR : an error is detected
@par Use of this function is mandatory only for the MVE version of the FFT.
Other versions can still initialize directly the data structure using
variables declared in arm_const_structs_extra.h
*/
#include "extra_ffts.h"
#include "arm_common_tables_extra.h"
#include "arm_const_structs_extra.h"
#if defined(ARM_MATH_MVEF) && !defined(ARM_MATH_AUTOVECTORIZE)
#include "arm_vec_fft.h"
#include "arm_mve_tables.h"
arm_status arm_cfft_radix4by2_rearrange_twiddles_f32(arm_cfft_instance_f32_extra *S, int twidCoefModifier)
{
switch (S->fftLen >> (twidCoefModifier - 1)) {
"""
for power, length in lengths[::-1]:
if power % 2 != 0:
continue
init_string += f"""
#if !defined(ARM_DSP_CONFIG_TABLES) || defined(ARM_ALL_FFT_TABLES) \\
|| defined(ARM_TABLE_TWIDDLECOEF_F32_{length})
case {length}U:
S->rearranged_twiddle_tab_stride1_arr = rearranged_twiddle_tab_stride1_arr_{length}_f32;
S->rearranged_twiddle_stride1 = rearranged_twiddle_stride1_{length}_f32;
S->rearranged_twiddle_tab_stride2_arr = rearranged_twiddle_tab_stride2_arr_{length}_f32;
S->rearranged_twiddle_stride2 = rearranged_twiddle_stride2_{length}_f32;
S->rearranged_twiddle_tab_stride3_arr = rearranged_twiddle_tab_stride3_arr_{length}_f32;
S->rearranged_twiddle_stride3 = rearranged_twiddle_stride3_{length}_f32;
break;
#endif
"""
init_string += """
default:
return(ARM_MATH_ARGUMENT_ERROR);
break;
/* invalid sizes already filtered */
}
return(ARM_MATH_SUCCESS);
}
arm_status arm_cfft_init_f32_extra(
arm_cfft_instance_f32_extra * S,
uint16_t fftLen)
{
/* Initialise the default arm status */
arm_status status = ARM_MATH_SUCCESS;
/* Initialise the FFT length */
S->fftLen = fftLen;
/* Initialise the Twiddle coefficient pointer */
S->pTwiddle = NULL;
/* Initializations of Instance structure depending on the FFT length */
switch (S->fftLen) {
"""
for power, length in lengths[::-1]:
init_string += f"""
/* Initializations of structure parameters for {length} point FFT */
#if !defined(ARM_DSP_CONFIG_TABLES) || defined(ARM_ALL_FFT_TABLES) || (defined(ARM_TABLE_BITREVIDX_FXT_{length}) && defined(ARM_TABLE_TWIDDLECOEF_F32_{length}))
case {length}U:
/* Initialise the bit reversal table modifier */
S->bitRevLength = ARMBITREVINDEXTABLE_FIXED_{length}_TABLE_LENGTH_EXTRA;
S->pBitRevTable = (uint16_t *)armBitRevIndexTable_fixed_{length};
S->pTwiddle = (float32_t *)twiddleCoefExtra_{length};
status=arm_cfft_radix4by2_rearrange_twiddles_f32(S, {(power % 2) + 1});
break;
#endif
"""
init_string += """
default:
/* Reporting argument error if fftSize is not valid value */
status = ARM_MATH_ARGUMENT_ERROR;
break;
}
return (status);
}
#else
arm_status arm_cfft_init_f32_extra(
arm_cfft_instance_f32_extra * S,
uint16_t fftLen)
{
/* Initialise the default arm status */
arm_status status = ARM_MATH_SUCCESS;
/* Initialise the FFT length */
S->fftLen = fftLen;
/* Initialise the Twiddle coefficient pointer */
S->pTwiddle = NULL;
/* Initializations of Instance structure depending on the FFT length */
switch (S->fftLen) {
"""
for power, length in lengths[::-1]:
init_string += f"""
#if !defined(ARM_DSP_CONFIG_TABLES) || defined(ARM_ALL_FFT_TABLES) || (defined(ARM_TABLE_TWIDDLECOEF_F32_{length}) && defined(ARM_TABLE_BITREVIDX_FLT_{length}))
/* Initializations of structure parameters for {length} point FFT */
case {length}U:
/* Initialise the bit reversal table modifier */
FFTINIT(f32,{length});
break;
#endif
"""
init_string += """
default:
/* Reporting argument error if fftSize is not valid value */
status = ARM_MATH_ARGUMENT_ERROR;
break;
}
return (status);
}
#endif /* defined(ARM_MATH_MVEF) && !defined(ARM_MATH_AUTOVECTORIZE) */
/**
@} end of ComplexFFT group
*/
"""
return init_string
def const_structs(lengths):
struct_string = """
#include "arm_math_types.h"
#include "arm_const_structs_extra.h"
/* Floating-point structs */
#if !defined(ARM_MATH_MVEF) || defined(ARM_MATH_AUTOVECTORIZE)
"""
header_string = """
#ifndef _ARM_CONST_STRUCTS_EXTRA_H
#define _ARM_CONST_STRUCTS_EXTRA_H
#include "arm_math_types.h"
#include "arm_common_tables_extra.h"
#include "extra_ffts.h"
#ifdef __cplusplus
extern "C"
{
#endif
"""
for power, length in lengths:
struct_string += f"""
#if !defined(ARM_DSP_CONFIG_TABLES) || defined(ARM_ALL_FFT_TABLES) || (defined(ARM_TABLE_TWIDDLECOEF_F32_{length}) && defined(ARM_TABLE_BITREVIDX_FLT_{length}))
const arm_cfft_instance_f32_extra arm_cfft_sR_f32_len{length}_extra = {{
{length}, twiddleCoefExtra_{length}, armBitRevIndexTableExtra{length}, ARMBITREVINDEXTABLE_{length}_TABLE_LENGTH_EXTRA
}};
#endif
"""
header_string += f" extern const arm_cfft_instance_f32_extra arm_cfft_sR_f32_len{length}_extra;\n"
struct_string += """
#endif /* !defined(ARM_MATH_MVEF) || defined(ARM_MATH_AUTOVECTORIZE) */
"""
header_string += """
#ifdef __cplusplus
}
#endif
#endif
"""
return header_string, struct_string