From c13410191a3ec78570bb508992ca1c826d7f7814 Mon Sep 17 00:00:00 2001 From: xdab <47300315+xdab@users.noreply.github.com> Date: Mon, 21 Sep 2026 20:48:33 +0200 Subject: [PATCH] Frame sync: replace per-symbol full sort with single-pass top-5 selection The frame sync hunt loop copied the symbol amplitude window into lbuf2 and ran a full qsort over it on every iteration, only to read three ranked values off each end for lmin/lmax. Replace that with a single pass maintaining the five smallest and five largest values in two fixed 5-slot arrays; the same ranked endpoints are read from those, so output is bit-identical. The datascope histogram now bins the unsorted window (order does not affect bin counts). The unused lbuf2 scratch buffer is removed; comp() stays as dsd_dibit.c still uses it. Ref: #142 --- src/dsd_frame_sync.c | 28 +++++++++++++++++++++------- 1 file changed, 21 insertions(+), 7 deletions(-) diff --git a/src/dsd_frame_sync.c b/src/dsd_frame_sync.c index 1993c239..b2ce0c23 100644 --- a/src/dsd_frame_sync.c +++ b/src/dsd_frame_sync.c @@ -16,6 +16,7 @@ */ #include "dsd.h" +#include void printFrameSync (dsd_opts * opts, dsd_state * state, char *frametype, int offset, char *modulation) @@ -74,14 +75,13 @@ getFrameSync (dsd_opts * opts, dsd_state * state) char *synctest_p; char synctest_buf[10240]; int lmin, lmax, lidx; - int lbuf[24], lbuf2[24]; + int lbuf[24]; int lsum; char spectrum[64]; for (i = 18; i < 24; i++) { lbuf[i] = 0; - lbuf2[i] = 0; } // detect frame sync @@ -178,13 +178,27 @@ getFrameSync (dsd_opts * opts, dsd_state * state) *synctest_p = dibit; if (t >= 18) { + int lmn[5] = {INT_MAX, INT_MAX, INT_MAX, INT_MAX, INT_MAX}; + int lmx[5] = {INT_MIN, INT_MIN, INT_MIN, INT_MIN, INT_MIN}; + int j, x; for (i = 0; i < 24; i++) { - lbuf2[i] = lbuf[i]; + x = lbuf[i]; + if (x < lmn[4]) + { + for (j = 4; j > 0 && x < lmn[j - 1]; j--) + lmn[j] = lmn[j - 1]; + lmn[j] = x; + } + if (x > lmx[4]) + { + for (j = 4; j > 0 && x > lmx[j - 1]; j--) + lmx[j] = lmx[j - 1]; + lmx[j] = x; + } } - qsort (lbuf2, 24, sizeof (int), comp); - lmin = (lbuf2[2] + lbuf2[3] + lbuf2[4]) / 3; - lmax = (lbuf2[21] + lbuf2[20] + lbuf2[19]) / 3; + lmin = (lmn[2] + lmn[3] + lmn[4]) / 3; + lmax = (lmx[2] + lmx[3] + lmx[4]) / 3; if (state->rf_mod == 1) { @@ -243,7 +257,7 @@ getFrameSync (dsd_opts * opts, dsd_state * state) } for (i = 0; i < 24; i++) { - o = (lbuf2[i] + 32768) / 1024; + o = (lbuf[i] + 32768) / 1024; spectrum[o]++; } if (state->symbolcnt > (4800 / opts->scoperate))