Files
ambed-xombe/xombe/scripts/gen_dstar_enc_tables.py
T

98 lines
4.0 KiB
Python

#!/usr/bin/env python3
"""writes src/tables/dstar_enc_tables.c and dstar_enc_tables.h, the D-STAR encoder's noise reference and windows
m_noise(N, b0) in scripts/data/dstar_tables.json is the fitted encoder model's mean voicing metric over 8 seeded
numpy white-noise draws, which C cannot reproduce, so it is tabulated for the (N, b0) pairs the encoder can reach,
as round(m*65536) in 16 bits (m < 1, smallest 0.18, relative error <= 3e-5)"""
import json
import math
import os
import struct
ROOT = os.path.join(os.path.dirname(os.path.abspath(__file__)), '..')
OUT_C = os.path.join(ROOT, 'src', 'tables', 'dstar_enc_tables.c')
OUT_H = os.path.join(ROOT, 'src', 'tables', 'dstar_enc_tables.h')
DATA = os.path.join(ROOT, 'scripts', 'data', 'dstar_tables.json')
N_MIN, N_MAX = 199, 321
N_COUNT = (N_MAX - N_MIN) // 2 + 1
B0_VOICE_MAX = 123
SCALE = 65536
TONE_LEN, NZ_LEN = 160, 134
def f32(x):
t = '%.9g' % struct.unpack('<f', struct.pack('<f', x))[0]
return t + ('f' if ('.' in t or 'e' in t) else '.0f')
def hann(n):
return [0.5 - 0.5 * math.cos(2.0 * math.pi * i / (n - 1)) for i in range(n)]
def main():
with open(DATA) as f:
windows = json.load(f)['m_noise']
assert [w['N'] for w in windows] == list(range(N_MIN, N_MAX + 1, 2))
lo, cnt, off, data = [], [], [], []
for w in windows:
assert 0 <= w['b0_lo'] and w['b0_lo'] + len(w['rows']) - 1 <= B0_VOICE_MAX
lo.append(w['b0_lo'])
cnt.append(len(w['rows']))
off.append(len(data))
for m in w['rows']:
q = [int(round(v * SCALE)) for v in m]
assert len(q) == 8 and all(0 < v < SCALE for v in q), (w['N'], m)
data.append(q)
rows = len(data)
h = '''#ifndef XOMBE_DSTAR_ENC_TABLES_H
#define XOMBE_DSTAR_ENC_TABLES_H
/* D-STAR encoder white-noise voicing reference m_noise(N, b0), generated by scripts/gen_dstar_enc_tables.py,
do not edit
window N = %d + 2i (i < XOMBE_DS_MN_WINDOWS) has rows for b0 lo[i]..lo[i] + cnt[i] - 1 from row off[i],
each row the 8 band values x %d */
#define XOMBE_DS_MN_N_MIN %d
#define XOMBE_DS_MN_WINDOWS %d
#define XOMBE_DS_MN_ROWS %d
#define XOMBE_DS_MN_SCALE %d.0f
extern const unsigned char xombe__ds_mn_lo[XOMBE_DS_MN_WINDOWS];
extern const unsigned char xombe__ds_mn_cnt[XOMBE_DS_MN_WINDOWS];
extern const unsigned short xombe__ds_mn_off[XOMBE_DS_MN_WINDOWS];
extern const unsigned short xombe__ds_mn[XOMBE_DS_MN_ROWS][8];
/* tone detector segment and tone-in-noise window lengths in samples, Hann windows 0.5 - 0.5*cos(2*pi*i/(n - 1)) */
#define XOMBE_DS_TONE_LEN %d
#define XOMBE_DS_NZ_LEN %d
extern const float xombe__ds_tone_win[XOMBE_DS_TONE_LEN];
extern const float xombe__ds_nz_win[XOMBE_DS_NZ_LEN];
#endif
''' % (N_MIN, SCALE, N_MIN, N_COUNT, rows, SCALE, TONE_LEN, NZ_LEN)
def vec(name, ctype, v, per=16):
lines = [' ' + ', '.join(str(x) for x in v[i:i + per]) + ',' for i in range(0, len(v), per)]
return 'const %s %s[%d] = {\n%s\n};\n' % (ctype, name, len(v), '\n'.join(lines))
c = ['/* generated by scripts/gen_dstar_enc_tables.py, do not edit */\n#include "dstar_enc_tables.h"\n']
c.append(vec('xombe__ds_mn_lo', 'unsigned char', lo))
c.append(vec('xombe__ds_mn_cnt', 'unsigned char', cnt))
c.append(vec('xombe__ds_mn_off', 'unsigned short', off))
c.append('const unsigned short xombe__ds_mn[XOMBE_DS_MN_ROWS][8] = {\n%s\n};\n' %
'\n'.join(' {%s},' % ', '.join(str(x) for x in r) for r in data))
for name, n in (('xombe__ds_tone_win', 'XOMBE_DS_TONE_LEN'), ('xombe__ds_nz_win', 'XOMBE_DS_NZ_LEN')):
w = [f32(v) for v in hann(TONE_LEN if n.endswith('TONE_LEN') else NZ_LEN)]
c.append('const float %s[%s] = {\n%s\n};\n' % (name, n, '\n'.join(
' ' + ', '.join(w[i:i + 8]) + ',' for i in range(0, len(w), 8))))
with open(OUT_C, 'w') as f:
f.write('\n'.join(c))
with open(OUT_H, 'w') as f:
f.write(h)
print('%d windows, %d rows, %d bytes' % (N_COUNT, rows, rows * 16 + N_COUNT * 4))
if __name__ == '__main__':
main()