Browse Source

libchirpmodem: O(n) CW ID smoothing; no preamble metric on digital silence

chirpmodem_tx_cw_id smoothed the keying envelope with a direct convolution
(5 ms raised cosine, kr = 0.005 fs taps, for every sample): 6 s for 5.1 s
of CW at 600 kS/s, 16 s at 1 MS/s, slower than real time, which stalled the
SDRangel modulator. The envelope is 0/1 and its runs are at least a dot
long, so each output sample is a sum over at most a few runs of
differences of the window's cumulative sum: O(n). Output is bit-identical
(float32) to before at 125 k, 250 k, 600 k and 1 MS/s, 13 and 20 WPM;
600 kS/s now takes 0.08 s.

The stream detector's metric divided a window's peak bin by its mean
power. On exact zeros (silence in transmit files, SDRangel FileOutput and
loopback paths) that is 0/0 = NaN, and NaN passed "met < 4.0" as not
below the threshold: 583 false "preamble found, header fail" per 5 s of
zeros. An all-zero window now has ratio 1, and the threshold test is
written so that NaN fails. Decoded frames, SNRs and offsets are identical
on all captures (ota.cu8 49, live_rx 42, ota_tx, ota_cw, soak_tx,
sweep_tx, live_tx, modtx); only the spurious failures on digital silence
are gone (ota_tx 181 -> 0).

make test and tools/check_c.py pass.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
master
Daniel Peter Chokola 2 weeks ago
parent
commit
a0bf1fc0fa
  1. 6
      c/src/cm_stream.c
  2. 51
      c/src/cm_tx.c

6
c/src/cm_stream.c

@ -339,7 +339,9 @@ static void detect(stream_t *s)
double m = 0; double m = 0;
for (int n = 0; n < N2; n++) for (int n = 0; n < N2; n++)
m += e[n]; m += e[n];
lg += log(e[B] / (m / N2) + 1e-12); /* an all-zero window (digital silence) has no peak: ratio 1,
* not 0/0, which made the metric NaN and passed the threshold */
lg += log((m > 0 ? e[B] / (m / N2) : 1.0) + 1e-12);
} }
s->met[i] = exp(lg / L); s->met[i] = exp(lg / L);
} }
@ -347,7 +349,7 @@ static void detect(stream_t *s)
/* accept local maxima above threshold once their neighbourhood is in */ /* accept local maxima above threshold once their neighbourhood is in */
for (long k = s->k_next; k <= k_final; k++) { for (long k = s->k_next; k <= k_final; k++) {
long i = k - s->w0; long i = k - s->w0;
if (i < 0 || s->met[i] < 4.0) if (i < 0 || !(s->met[i] >= 4.0)) /* written so that NaN fails too */
continue; continue;
int best = 1; int best = 1;
for (long j = k - n_pre; j <= k + n_pre && best; j++) { for (long j = k - n_pre; j <= k + n_pre && best; j++) {

51
c/src/cm_tx.c

@ -280,32 +280,61 @@ long chirpmodem_tx_cw_id(chirpmodem_tx *t, const char *call, double wpm, float *
long kr = (long)(0.005 * fs); /* raised-cosine smoothing */ long kr = (long)(0.005 * fs); /* raised-cosine smoothing */
if (kr < 1) if (kr < 1)
kr = 1; kr = 1;
double *wr = malloc(sizeof(double) * (size_t)kr), ws = 0; /* cum[m] = wr[0] + ... + wr[m-1], so cum[0] = 0 and cum[kr] = sum */
double *cum = malloc(sizeof(double) * (size_t)(kr + 1));
cf64 *tone = malloc(sizeof(cf64) * (size_t)n); cf64 *tone = malloc(sizeof(cf64) * (size_t)n);
if (!env || !wr || !tone) { long (*run)[2] = malloc(sizeof *run * (size_t)(n / 2 + 1));
if (!env || !cum || !tone || !run) {
free(env); free(env);
free(wr); free(cum);
free(tone); free(tone);
free(run);
return CHIRPMODEM_ENOMEM; return CHIRPMODEM_ENOMEM;
} }
cw_envelope(text, dot, env); cw_envelope(text, dot, env);
for (long k = 0; k < kr; k++) { cum[0] = 0;
wr[k] = 0.5 - 0.5 * cos(M_PI * k / kr); for (long k = 0; k < kr; k++)
ws += wr[k]; cum[k + 1] = cum[k] + (0.5 - 0.5 * cos(M_PI * k / kr));
double ws = cum[kr];
/* The keyed runs [a, b) of the 0/1 envelope */
long nr = 0;
for (long v = 0; v < n;) {
if (env[v] == 0) {
v++;
continue;
}
long a = v;
while (v < n && env[v] != 0)
v++;
run[nr][0] = a;
run[nr++][1] = v;
} }
/* s(u) = sum over k in [0, kr) of env[u + kr/2 - k] * wr[k]. A run [a, b)
* contributes the window taps k from u + kr/2 - b + 1 to u + kr/2 - a, so a
* difference of two cumulative sums: O(n) instead of O(n * kr). Runs are
* at least a dot long, so the window spans only a few of them. */
double fi = t->cfg.if_offset; double fi = t->cfg.if_offset;
long first = 0; /* first run that can still reach u */
for (long u = 0; u < n; u++) { for (long u = 0; u < n; u++) {
long c = u + kr / 2;
while (first < nr && run[first][1] <= c - kr + 1)
first++;
double s = 0; double s = 0;
for (long k = 0; k < kr; k++) { for (long r = first; r < nr && run[r][0] <= c; r++) {
long v = u + kr / 2 - k; long k0 = c - run[r][1] + 1, k1 = c - run[r][0]; /* taps k0..k1 */
if (v >= 0 && v < n) if (k0 < 0)
s += env[v] * wr[k]; k0 = 0;
if (k1 > kr - 1)
k1 = kr - 1;
if (k0 <= k1)
s += cum[k1 + 1] - cum[k0];
} }
tone[u] = s / ws * cexp(2.0 * M_PI * I * fmod(fi * u / fs, 1.0)); tone[u] = s / ws * cexp(2.0 * M_PI * I * fmod(fi * u / fs, 1.0));
} }
store(t, tone, n, iq); store(t, tone, n, iq);
free(env); free(env);
free(wr); free(cum);
free(tone); free(tone);
free(run);
return n; return n;
} }

Loading…
Cancel
Save