From 7e869070e31cf6182de4495163242c049e9712c7 Mon Sep 17 00:00:00 2001 From: Michael Maltese Date: Fri, 2 Jun 2017 16:20:25 -0700 Subject: [PATCH] generate_coefs: separate type conversion and packing This makes it easier to apply patches to the resulting binary before writing it to a file. --- docs/DSP/free_dsp_rom/generate_coefs.py | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/docs/DSP/free_dsp_rom/generate_coefs.py b/docs/DSP/free_dsp_rom/generate_coefs.py index 5b1771429d..9d3d9154b6 100644 --- a/docs/DSP/free_dsp_rom/generate_coefs.py +++ b/docs/DSP/free_dsp_rom/generate_coefs.py @@ -1,13 +1,16 @@ from numpy import * from struct import pack -def pack_coefs(c): +def convert_coefs(c): cw = list(zip(c[ :128][::-1], c[128:256][::-1], c[256:384][::-1], c[384: ][::-1])) m = max(sum(x) for x in cw) - return b''.join(pack('>4h', *(int(round(n / m * 32767)) for n in x)) for x in cw) + return [int(round(n / m * 32767)) & 0xffff for x in cw for n in x] + +def pack_coefs(short_coefs): + return b''.join(pack('>H', c) for c in short_coefs) x = linspace(-2, 2, 512, endpoint=False) @@ -18,8 +21,7 @@ coef_1 = [sinc(n * 0.5) for n in x] * w1 coef_2 = [sinc(n * 0.75) for n in x] * w2 coef_3 = [sinc(n) for n in x] * w1 +short_coefs = convert_coefs(coef_1) + convert_coefs(coef_2) + convert_coefs(coef_3) + [0] * 512 + with open('dsp_coef.bin', 'wb') as f: - f.write(pack_coefs(coef_1)) - f.write(pack_coefs(coef_2)) - f.write(pack_coefs(coef_3)) - f.write(b'\0' * 1024) + f.write(pack_coefs(short_coefs))