From ad9e17c35c2fc358e6660d179c844b14b92c5541 Mon Sep 17 00:00:00 2001 From: jaseg Date: Fri, 6 Mar 2020 11:09:35 +0100 Subject: WIP DSSS decoding --- controller/fw/tools/butter_filter_gen.py | 50 +++++++++++++++++++++++++++++ controller/fw/tools/gold_code_header_gen.py | 8 +---- 2 files changed, 51 insertions(+), 7 deletions(-) create mode 100644 controller/fw/tools/butter_filter_gen.py (limited to 'controller/fw/tools') diff --git a/controller/fw/tools/butter_filter_gen.py b/controller/fw/tools/butter_filter_gen.py new file mode 100644 index 0000000..059dea0 --- /dev/null +++ b/controller/fw/tools/butter_filter_gen.py @@ -0,0 +1,50 @@ +#!/usr/bin/env python3 + +import sys +import contextlib + +import scipy.signal as sig + + +@contextlib.contextmanager +def wrap(left='{', right='}', file=None, end=''): + print(left, file=file, end=end) + yield + print(right, file=file, end=end) + +@contextlib.contextmanager +def print_include_guards(macro_name): + print(f'#ifndef {macro_name}') + print(f'#define {macro_name}') + yield + print(f'#endif /* {macro_name} */') + +macro_float = lambda f: f'{f}'.replace('.', 'F').replace('-', 'N').replace('+', 'P') + + +if __name__ == '__main__': + import argparse + parser = argparse.ArgumentParser() + parser.add_argument('-m', '--macro-name', default='butter_filter', help='Prefix for output macro names') + parser.add_argument('fc', type=float, help='Corner frequency [Hz]') + parser.add_argument('fs', type=float, help='Sampling rate [Hz]') + parser.add_argument('n', type=int, nargs='?', default=6, help='Filter order') + args = parser.parse_args() + + sos = sig.butter(args.n, args.fc, fs=args.fs, output='sos') + + print('/* THIS IS A GENERATED FILE. DO NOT EDIT! */') + with print_include_guards(f'__BUTTER_FILTER_GENERATED_{args.n}_{macro_float(args.fc)}_{macro_float(args.fs)}__'): + print(f'#define {args.macro_name.upper()}_ORDER {args.n}') + print(f'#define {args.macro_name.upper()}_CLEN {(args.n+1)//2}') + print(f'#define {args.macro_name.upper()}_COEFF ', end='') + for sec in sos: + with wrap(): + print('.b=', end='') + with wrap(): + print(', '.join(f'{v}' for v in sec[:3]), end='') + print(', .a=', end='') + with wrap(): + print(', '.join(f'{v}' for v in sec[4:6]), end='') + print(', ', end='') + print() diff --git a/controller/fw/tools/gold_code_header_gen.py b/controller/fw/tools/gold_code_header_gen.py index 8247457..fa98fce 100644 --- a/controller/fw/tools/gold_code_header_gen.py +++ b/controller/fw/tools/gold_code_header_gen.py @@ -58,19 +58,13 @@ if __name__ == '__main__': print(f' *') print(f' * Each code is packed left-aligned into {nbytes} bytes in big-endian byte order.') print(f' */') - print(f'const uint8_t gold_code_{args.n}bit[{2**args.n+1}][{nbytes}] = {{') + print(f'const uint8_t {args.variable}[{2**args.n+1}][{nbytes}] = {{') for i, code in enumerate(gold(args.n)): par = '{' + ' '.join(f'0x{d:02x},' for d in np.packbits(code)) + f'}}, /* {i: 3d} "{"".join(str(x) for x in code)}" */' print(textwrap.fill(par, initial_indent=' '*4, subsequent_indent=' '*4, width=120)) print('};') print() - print(f'const uint8_t * const {args.variable} __attribute__((weak)) = (uint8_t *const)gold_code_{args.n}bit;') - print(f'const size_t {args.variable}_nbits __attribute__((weak)) = {args.n};') else: print('/* THIS IS A GENERATED FILE. DO NOT EDIT! */') with print_include_guards(f'__GOLD_CODE_GENERATED_HEADER_{args.n}__'): - print(f'extern const uint8_t gold_code_{args.n}bit[{2**args.n+1}][{nbytes}];') - - with print_include_guards(f'__GOLD_CODE_GENERATED_HEADER_GLOBAL_SYM_{args.variable.upper()}__'): print(f'extern const uint8_t {args.variable}[{2**args.n+1}][{nbytes}];') - print(f'extern const size_t {args.variable}_nbits;') -- cgit