From 4b419bd1ad9f22266e068341176f5ab665a26222 Mon Sep 17 00:00:00 2001 From: jaseg Date: Thu, 5 Mar 2020 19:15:28 +0100 Subject: Working on DSSS demodulator sim --- controller/fw/tools/cwt_wavelet_header_gen.py | 29 +++++++++ controller/fw/tools/dsss_demod_test.c | 84 +++++++++++++++++++++++++++ controller/fw/tools/dsss_demod_test_runner.py | 39 +++++++++++++ controller/fw/tools/freq_meas_test.c | 2 +- controller/fw/tools/gold_code_header_gen.py | 59 ++++++++++++++----- 5 files changed, 198 insertions(+), 15 deletions(-) create mode 100644 controller/fw/tools/cwt_wavelet_header_gen.py create mode 100644 controller/fw/tools/dsss_demod_test.c create mode 100644 controller/fw/tools/dsss_demod_test_runner.py (limited to 'controller/fw/tools') diff --git a/controller/fw/tools/cwt_wavelet_header_gen.py b/controller/fw/tools/cwt_wavelet_header_gen.py new file mode 100644 index 0000000..e9713a7 --- /dev/null +++ b/controller/fw/tools/cwt_wavelet_header_gen.py @@ -0,0 +1,29 @@ +#!/usr/bin/env python3 + +import textwrap + +import scipy.signal as sig +import numpy as np + +if __name__ == '__main__': + import argparse + parser = argparse.ArgumentParser() + parser.add_argument('n', type=int, help='Window size') + parser.add_argument('w', type=float, help='Wavelet width') + parser.add_argument('-v', '--variable', default='cwt_ricker_table', help='Name for alias variable pointing to generated wavelet LUT') + args = parser.parse_args() + + print(f'/* CWT Ricker wavelet LUT for {args.n} sample window of width {args.w}. */') + varname = f'cwt_ricker_{args.n}_window_{str(args.w).replace(".", "F")}' + print(f'const float {varname}[{args.n}] = {{') + + win = sig.ricker(args.n, args.w) + par = ' '.join(f'{f:>015.8g}f,' for f in win) + print(textwrap.fill(par, + initial_indent=' '*4, subsequent_indent=' '*4, + width=120, + replace_whitespace=False, drop_whitespace=False)) + print('};') + print() + print(f'const float * const {args.variable} __attribute__((weak)) = {varname};') + diff --git a/controller/fw/tools/dsss_demod_test.c b/controller/fw/tools/dsss_demod_test.c new file mode 100644 index 0000000..51742b1 --- /dev/null +++ b/controller/fw/tools/dsss_demod_test.c @@ -0,0 +1,84 @@ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "dsss_demod.h" + +void print_usage() { + fprintf(stderr, "Usage: dsss_demod_test [test_data.bin]\n"); +} + +int main(int argc, char **argv) { + if (argc != 2) { + fprintf(stderr, "Error: Invalid arguments.\n"); + print_usage(); + return 1; + } + + int fd = open(argv[1], O_RDONLY); + struct stat st; + if (fstat(fd, &st)) { + fprintf(stderr, "Error querying test data file size: %s\n", strerror(errno)); + return 2; + } + + if (st.st_size < 0 || st.st_size > 1000000) { + fprintf(stderr, "Error reading test data: too much test data (size=%zd)\n", st.st_size); + return 2; + } + + if (st.st_size % sizeof(float) != 0) { + fprintf(stderr, "Error reading test data: file size is not divisible by %zd (size=%zd)\n", sizeof(float), st.st_size); + return 2; + } + + char *buf = malloc(st.st_size); + if (!buf) { + fprintf(stderr, "Error allocating memory"); + return 2; + } + + fprintf(stderr, "Reading %zd samples test data...", st.st_size/sizeof(float)); + size_t nread = 0; + while (nread < st.st_size) { + ssize_t rc = read(fd, buf, st.st_size - nread); + + if (rc == -EINTR || rc == -EAGAIN) + continue; + + if (rc < 0) { + fprintf(stderr, "\nError reading test data: %s\n", strerror(errno)); + return 2; + } + + if (rc == 0) { + fprintf(stderr, "\nError reading test data: Unexpected end of file\n"); + return 2; + } + + nread += rc; + } + fprintf(stderr, " done.\n"); + + const size_t n_samples = st.st_size / sizeof(float); + float *buf_f = (float *)buf; + + fprintf(stderr, "Starting simulation.\n"); + + struct dsss_demod_state demod; + memset(&demod, 0, sizeof(demod)); + for (size_t i=0; i') - print() - print(f'/* {args.n} bit gold sequences: {2**args.n+1} sequences of length {2**args.n-1} bit.') - print(f' *') - print(f' * Each code is packed left-aligned into {2**args.n // 8} bytes in big-endian byte order.') - print(f' */') - print(f'const uint8_t gold_code_{args.n}bit[] = {{') - 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 gold_code_table __attribute__((weak)) = gold_code_{args.n}bit;') + if not args.header != args.source: + print('Exactly one of --header and --source must be given.', file=sys.stderr) + sys.exit(1) + + nbytes = math.ceil((2**args.n-1)/8) + + if args.source: + print('/* THIS IS A GENERATED FILE. DO NOT EDIT! */') + print('#include ') + print('#include ') + print() + print(f'/* {args.n} bit gold sequences: {2**args.n+1} sequences of length {2**args.n-1} bit.') + 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}] = {{') + 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