Debian Codegraph @

docs/DSP/free_dsp_rom/generate_coefs.py

raw symbols SCIP
1 from numpy import *
2 from struct import pack
3
4 def convert_coefs(c):
5 cw = list(zip(c[ :128][::-1],
6 c[128:256][::-1],
7 c[256:384][::-1],
8 c[384: ][::-1]))
9 m = max(sum(x) for x in cw)
10 return [int(round(n / m * 32767)) & 0xffff for x in cw for n in x]
11
12 def pack_coefs(short_coefs):
13 return b''.join(pack('>H', c) for c in short_coefs)
14
15 x = linspace(-2, 2, 512, endpoint=True)
16
17 w1 = hamming(512)
18 w2 = kaiser(512, pi * 9/4)
19
20 coef_1 = [sinc(n * 0.5) for n in x] * w1
21 coef_2 = [sinc(n * 0.75) for n in x] * w2
22 coef_3 = [sinc(n) for n in x] * w1
23
24 short_coefs = convert_coefs(coef_1) + convert_coefs(coef_2) + convert_coefs(coef_3) + [0] * 512
25
26 # needed for GBA ucode
27 gba_coefs = (
28 (0x03b, 0x0065),
29 (0x043, 0x0076),
30 (0x0ca, 0x3461),
31 (0x0e2, 0x376f),
32 (0x1b8, 0x007f),
33 (0x1b8, 0x007f),
34 (0x1f8, 0x0009),
35 (0x1fc, 0x0003),
36 (0x229, 0x657c),
37 (0x231, 0x64fc),
38 (0x259, 0x6143),
39 (0x285, 0x5aff),
40 (0x456, 0x102f),
41 (0x468, 0xf808),
42 (0x491, 0x6a0f),
43 (0x5f1, 0x0200),
44 (0x5f6, 0x7f65),
45 (0x65b, 0x0000),
46 (0x66b, 0x0000),
47 (0x66c, 0x06f2),
48 (0x6fe, 0x0008),
49 (0x723, 0xffe0),
50 (0x766, 0x0273),
51 )
52 for (addr, value) in gba_coefs:
53 old_value = short_coefs[addr]
54 if old_value != value:
55 print("At %04x: replacing %04x with %04x (diff. of % #x)" % (addr, old_value, value, value - old_value))
56 short_coefs[addr] = value
57
58 with open('dsp_coef.bin', 'wb') as f:
59 f.write(pack_coefs(short_coefs))