generate_coefs: separate type conversion and packing

This makes it easier to apply patches to the resulting binary before
writing it to a file.
This commit is contained in:
Michael Maltese 2017-06-02 16:20:25 -07:00
parent 4b53093acb
commit 7e869070e3
1 changed files with 8 additions and 6 deletions

View File

@ -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))