generate_coefs.py 1.5 KB

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