diff options
Diffstat (limited to 'controller/fw/src/dsss_demod.c')
-rw-r--r-- | controller/fw/src/dsss_demod.c | 88 |
1 files changed, 45 insertions, 43 deletions
diff --git a/controller/fw/src/dsss_demod.c b/controller/fw/src/dsss_demod.c index 7feb41b..cc1c34e 100644 --- a/controller/fw/src/dsss_demod.c +++ b/controller/fw/src/dsss_demod.c @@ -22,70 +22,77 @@ float gold_correlate_step(const size_t ncode, const float a[DSSS_CORRELATION_LEN float cwt_convolve_step(const float v[DSSS_WAVELET_LUT_SIZE], size_t offx); float run_iir(const float x, const int order, const struct iir_biquad q[order], struct iir_biquad_state st[order]); float run_biquad(float x, const struct iir_biquad *const q, struct iir_biquad_state *const restrict st); +void debug_print_vector(const char *name, size_t len, const float *data, size_t stride, bool index, bool debug); #ifdef SIMULATION -void dsss_demod_step(struct dsss_demod_state *st, float new_value, size_t sim_pos) { -#else /* SIMULATION */ +void debug_print_vector(const char *name, size_t len, const float *data, size_t stride, bool index, bool debug) { + if (!debug) + return; + + if (index) { + DEBUG_PRINTN(" %16s [", ""); + for (size_t i=0; i<len; i++) + DEBUG_PRINTN("%8d ", i); + DEBUG_PRINTN("]\n"); + } + + DEBUG_PRINTN(" %16s: [", name); + for (size_t i=0; i<len; i++) + DEBUG_PRINTN("%8.5f, ", data[i*stride]); + DEBUG_PRINTN("]\n"); +} +#else +void debug_print_vector(const char *name, size_t len, const float *data, size_t stride, bool index, bool debug) {} +#endif + +#ifdef SIMULATION +void dsss_demod_step(struct dsss_demod_state *st, float new_value, size_t sim_pos, int record_channel) { + bool debug = (record_channel == -1) + && (sim_pos > 1000) + && (sim_pos % DSSS_CORRELATION_LENGTH == DSSS_CORRELATION_LENGTH-1); + + if (debug) DEBUG_PRINT("Iteration %zd: signal=%f", sim_pos, new_value); +#else void dsss_demod_step(struct dsss_demod_state *st, float new_value) { -#endif /* SIMULATION */ +#endif -//#define DEBUG_PRINT(...) ((void)0) -//#define DEBUG_PRINTN(...) ((void)0) - bool debug = sim_pos % DSSS_CORRELATION_LENGTH == DSSS_CORRELATION_LENGTH-1; - //bool debug = sim_pos > 1000; - if (debug) DEBUG_PRINT("Iteration %zd", sim_pos); //const float peak_group_threshold = 0.05 * DSSS_CORRELATION_LENGTH; //const float hole_patching_threshold = 0.01 * DSSS_CORRELATION_LENGTH; st->signal[st->signal_wpos] = new_value; st->signal_wpos = (st->signal_wpos + 1) % ARRAY_LENGTH(st->signal); - if (debug) DEBUG_PRINT(" signal: %f", new_value); /* use new, incremented wpos for gold_correlate_step as first element of old data in ring buffer */ for (size_t i=0; i<DSSS_GOLD_CODE_COUNT; i++) st->correlation[i][st->correlation_wpos] = gold_correlate_step(i, st->signal, st->signal_wpos, false); - /* debug */ - if (debug) { - DEBUG_PRINTN(" ["); - for (size_t i=0; i<DSSS_GOLD_CODE_COUNT; i++) - DEBUG_PRINTN("%8d ", i); - DEBUG_PRINTN("]\n"); - DEBUG_PRINTN(" correlation: ["); - for (size_t i=0; i<DSSS_GOLD_CODE_COUNT; i++) - DEBUG_PRINTN("%8.5f, ", st->correlation[i][st->correlation_wpos]); - DEBUG_PRINTN("]\n"); - } - /* end */ + + debug_print_vector("correlation", + DSSS_GOLD_CODE_COUNT, &st->correlation[0][st->correlation_wpos], DSSS_WAVELET_LUT_SIZE, true, debug); + st->correlation_wpos = (st->correlation_wpos + 1) % ARRAY_LENGTH(st->correlation[0]); float cwt[DSSS_GOLD_CODE_COUNT]; for (size_t i=0; i<DSSS_GOLD_CODE_COUNT; i++) cwt[i] = cwt_convolve_step(st->correlation[i], st->correlation_wpos); - /* debug */ - if (debug) DEBUG_PRINTN(" cwt: ["); - for (size_t i=0; i<DSSS_GOLD_CODE_COUNT; i++) - if (debug) DEBUG_PRINTN("%8.5f, ", cwt[i]); - if (debug) DEBUG_PRINTN("]\n"); - /* end */ + + debug_print_vector("cwt", DSSS_GOLD_CODE_COUNT, cwt, 1, false, debug); float avg[DSSS_GOLD_CODE_COUNT]; for (size_t i=0; i<DSSS_GOLD_CODE_COUNT; i++) avg[i] = run_iir(fabs(cwt[i]), ARRAY_LENGTH(cwt_filter_bq), cwt_filter_bq, st->cwt_filter[i].st); - /* debug */ - if (debug) DEBUG_PRINTN(" avg: ["); - for (size_t i=0; i<DSSS_GOLD_CODE_COUNT; i++) - if (debug) DEBUG_PRINTN("%8.5f, ", avg[i]); - if (debug) DEBUG_PRINTN("]\n"); - /* end */ + + debug_print_vector("avg", DSSS_GOLD_CODE_COUNT, avg, 1, false, debug); + + if (record_channel != -1) + DEBUG_PRINTN("%f, %f, %f\n", + st->correlation[record_channel][st->correlation_wpos], cwt[record_channel], avg[record_channel]); float max_val = st->group.max; int max_ch = st->group.max_ch; int max_idx = st->group.max_idx + 1; bool found = false; - //if (debug) DEBUG_PRINTN(" rel: ["); for (size_t i=0; i<DSSS_GOLD_CODE_COUNT; i++) { float val = cwt[i] / avg[i]; - //if (debug) DEBUG_PRINTN("%f, ", val); if (fabs(val) > DSSS_THESHOLD_FACTOR) found = true; @@ -96,12 +103,6 @@ void dsss_demod_step(struct dsss_demod_state *st, float new_value) { max_idx = st->group.len; } } - //if (debug) DEBUG_PRINTN("]\n"); - - /* debug */ - if (debug) DEBUG_PRINT(" found=%d len=%d idx=%d ch=%d max=%f", - found, st->group.len, st->group.max_idx, st->group.max_ch, st->group.max); - /* end */ if (found) { /* Continue ongoing group */ @@ -117,8 +118,9 @@ void dsss_demod_step(struct dsss_demod_state *st, float new_value) { return; /* A group ended. Process result. */ - DEBUG_PRINT("GROUP FOUND: %8d len=%3d max=%f ch=%d offx=%d", - sim_pos, st->group.len, st->group.max, st->group.max_ch, st->group.max_idx); + if (record_channel == -1) + DEBUG_PRINT("GROUP FOUND: %8d len=%3d max=%f ch=%d offx=%d", + sim_pos, st->group.len, st->group.max, st->group.max_ch, st->group.max_idx); /* reset grouping state */ st->group.len = 0; |