diff options
Diffstat (limited to 'controller/fw/tools')
-rw-r--r-- | controller/fw/tools/cwt_wavelet_header_gen.py | 29 | ||||
-rw-r--r-- | controller/fw/tools/dsss_demod_test.c | 84 | ||||
-rw-r--r-- | controller/fw/tools/dsss_demod_test_runner.py | 39 | ||||
-rw-r--r-- | controller/fw/tools/freq_meas_test.c | 2 | ||||
-rw-r--r-- | controller/fw/tools/gold_code_header_gen.py | 59 |
5 files changed, 198 insertions, 15 deletions
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 <stdint.h> +#include <math.h> +#include <unistd.h> +#include <stdio.h> +#include <string.h> +#include <errno.h> +#include <stdlib.h> +#include <sys/stat.h> +#include <sys/types.h> +#include <sys/fcntl.h> + +#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<n_samples; i++) { + //fprintf(stderr, "Iteration %zd/%zd\n", i, n_samples); + dsss_demod_step(&demod, buf_f[i], i); + } + + return 0; +} diff --git a/controller/fw/tools/dsss_demod_test_runner.py b/controller/fw/tools/dsss_demod_test_runner.py new file mode 100644 index 0000000..4e93d7b --- /dev/null +++ b/controller/fw/tools/dsss_demod_test_runner.py @@ -0,0 +1,39 @@ +#!/usr/bin/env python3 + +import os +from os import path +import subprocess +import json + +import numpy as np +np.set_printoptions(linewidth=240) + + +if __name__ == '__main__': + import argparse + parser = argparse.ArgumentParser() + parser.add_argument(metavar='test_data_directory', dest='dir', help='Directory with test data .bin files') + default_binary = path.abspath(path.join(path.dirname(__file__), '../build/tools/dsss_demod_test')) + parser.add_argument(metavar='test_binary', dest='binary', nargs='?', default=default_binary) + parser.add_argument('-d', '--dump', help='Write raw measurements to JSON file') + args = parser.parse_args() + + bin_files = [ path.join(args.dir, d) for d in os.listdir(args.dir) if d.lower().endswith('.bin') ] + + savedata = {} + for p in bin_files: + output = subprocess.check_output([args.binary, p], stderr=subprocess.DEVNULL) + measurements = np.array([ float(value) for _offset, value in [ line.split() for line in output.splitlines() ] ]) + savedata[p] = list(measurements) + + # Cut off first and last sample for mean and RMS calculations as these show boundary effects. + measurements = measurements[1:-1] + mean = np.mean(measurements) + rms = np.sqrt(np.mean(np.square(measurements - mean))) + + print(f'{path.basename(p):<60}: mean={mean:<8.4f}Hz rms={rms*1000:.3f}mHz') + + if args.dump: + with open(args.dump, 'w') as f: + json.dump(savedata, f) + diff --git a/controller/fw/tools/freq_meas_test.c b/controller/fw/tools/freq_meas_test.c index df3e39d..e80290e 100644 --- a/controller/fw/tools/freq_meas_test.c +++ b/controller/fw/tools/freq_meas_test.c @@ -70,7 +70,7 @@ int main(int argc, char **argv) { } fprintf(stderr, " done.\n"); - size_t n_samples = st.st_size / sizeof(float); + const size_t n_samples = st.st_size / sizeof(float); float *buf_f = (float *)buf; int16_t *sim_adc_buf = calloc(sizeof(int16_t), n_samples); diff --git a/controller/fw/tools/gold_code_header_gen.py b/controller/fw/tools/gold_code_header_gen.py index e2bc210..8247457 100644 --- a/controller/fw/tools/gold_code_header_gen.py +++ b/controller/fw/tools/gold_code_header_gen.py @@ -1,6 +1,9 @@ #!/usr/bin/env python3 +import sys +import math import textwrap +import contextlib import numpy as np import scipy.signal as sig @@ -24,22 +27,50 @@ def gold(n): (seq0, _st0), (seq1, _st1) = sig.max_len_seq(n, taps=t0), sig.max_len_seq(n, taps=t1) return gen_gold(seq0, seq1) +@contextlib.contextmanager +def print_include_guards(macro_name): + print(f'#ifndef {macro_name}') + print(f'#define {macro_name}') + yield + print(f'#endif /* {macro_name} */') + if __name__ == '__main__': import argparse - parser = argparse.ArgumentParser() + parser = argparse.ArgumentParser(add_help=False) parser.add_argument('n', type=int, choices=preferred_pairs, help='bit width of shift register. Generate 2**n + 1 sequences of length 2**n - 1.') + parser.add_argument('-v', '--variable', default='gold_code_table', help='Name for weak alias of generated table') + parser.add_argument('-h', '--header', action='store_true', help='Generate header file') + parser.add_argument('-c', '--source', action='store_true', help='Generate table source file') args = parser.parse_args() - print('#include <unistd.h>') - 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 <unistd.h>') + print('#include <stdint.h>') + 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;') |