ale-leehrtz has uploaded this change for review.

View Change

add SD-RI-PLL self-tuning of the PID gains

The PID gains of the SD-RI-PLL determine the loop bandwidth, and the
right bandwidth depends on the link: on a noisy link the loop must be
slow, while a large oscillator wander calls for a fast loop.

Add a self-tuning regulator layer on top of the unchanged PID law. It
estimates the measurement noise of the FIFO level (sigma_v) and the
frequency wander of the oscillator (q_phi, from Allan-style variances
over 1 h windows), and from these recomputes the loop bandwidth
wn = (q_phi / sigma_v^2)^(1/4) and Kp/Ki by pole placement, as well as
a smoothing time constant for the error. Gain changes are rate-limited.
It also gates outliers and reports link-down ticks, so that the caller
can hold the output instead of integrating a stale or empty FIFO.

On a 53 h A/B run against the sysmocom OCTOI hub (first 24 h with the
default gains, then self-tuned), time-in-band went from 71% to 90% and
the FIFO standard deviation from 70 to 47 frames.

Like the PID controller, this is hooked up in a follow-up patch.

Related: OS#7116
Change-Id: Ifa5e1762df67c02093636908b45c6b5ce78fdc85
---
M src/Makefile.am
A src/pll_tune.c
A src/pll_tune.h
3 files changed, 336 insertions(+), 2 deletions(-)

git pull ssh://gerrit.osmocom.org:29418/osmo-e1d refs/changes/31/44231/1
diff --git a/src/Makefile.am b/src/Makefile.am
index 94f9644..2670b9f 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -26,6 +26,7 @@
ice1usb_proto.h \
log.h \
pll_pid.h \
+ pll_tune.h \
usb.h \
e1gen/crc4itu.h \
e1gen/osmo_e1f.h \
@@ -79,6 +80,8 @@
$(NULL)

if ENABLE_DAHDI_TRUNKDEV
-osmo_e1d_SOURCES += dahdi_trunkdev.c pll_pid.c
-osmo_e1gen_SOURCES += dahdi_trunkdev.c pll_pid.c
+osmo_e1d_SOURCES += dahdi_trunkdev.c pll_pid.c pll_tune.c
+osmo_e1gen_SOURCES += dahdi_trunkdev.c pll_pid.c pll_tune.c
+osmo_e1d_LDADD += -lm
+osmo_e1gen_LDADD += -lm
endif
diff --git a/src/pll_tune.c b/src/pll_tune.c
new file mode 100644
index 0000000..1446096
--- /dev/null
+++ b/src/pll_tune.c
@@ -0,0 +1,231 @@
+/*
+ * pll_tune.c
+ *
+ * SD-RI-PLL: online self-tuning of the PID gains. See pll_tune.h.
+ *
+ * (C) 2025-2026 by C. Alejandra Carreon-Jimenez <alejandra.carreon@ale-leehrtz.org>
+ *
+ * All Rights Reserved
+ *
+ * SPDX-License-Identifier: GPL-2.0+
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ */
+#include <math.h>
+#include "pll_tune.h"
+
+#define TWO_POW_32 4294967296.0
+
+void pll_tune_default_params(struct pll_tune_params *p)
+{
+ p->n_star = 248.0;
+ p->tick_s = 1.0;
+ p->frames_per_s = 8000.0;
+ p->window = 3600;
+ p->zeta = 0.7071;
+ p->wn_min = 2.0 * M_PI / (8.0 * 3600.0);
+ p->wn_max = 2.0 * M_PI / 600.0;
+ p->warmup_windows = 6;
+ p->min_var_samples = 6;
+ p->max_ratio = 1.5;
+ p->ewma_win = 0.1;
+ p->ewma_tick = 1e-3;
+ p->filt_factor = 10.0;
+ p->gate = 6.0;
+ p->sigma_v_min = 8.0 / sqrt(12.0); /* DAHDI reads 8 frames per chunk */
+ p->max_reject = 5;
+}
+
+static void reset_window(struct pll_tune *t)
+{
+ t->ws_k = t->ws_nh = t->ws_nt = t->ws_bad = t->ws_n = 0;
+ t->ws_head = t->ws_tail = t->ws_u = 0.0;
+}
+
+void pll_tune_init(struct pll_tune *t, const struct pll_tune_params *p)
+{
+ unsigned int i;
+
+ t->p = *p;
+ t->k_plant = p->frames_per_s / TWO_POW_32;
+ t->absdiff = 0.0;
+ t->have_absdiff = false;
+ t->last_fifo = 0.0;
+ t->have_last = false;
+ t->n_rej = 0;
+ t->n_rejected = 0;
+ reset_window(t);
+ for (i = 0; i < PLL_TUNE_HIST; i++) {
+ t->phi[i] = 0.0;
+ t->phi_ok[i] = false;
+ }
+ t->n_phi = 0;
+ t->v1 = t->v4 = 0.0;
+ t->n_var = t->n_windows = 0;
+ t->wn = 0.0;
+ t->kp = t->ki = t->tau_f = 0.0;
+ t->q_phi = 0.0;
+}
+
+double pll_tune_sigma_v(const struct pll_tune *t)
+{
+ /* E|dz| = 2*sigma_v/sqrt(pi) for dz = v[k]-v[k-1], v Gaussian. The FIFO
+ * is quantized (DAHDI moves 8 frames per chunk), which is measurement noise
+ * in its own right and makes |dz| mostly zero when the jitter is small:
+ * never report less than the quantization noise. */
+ double sv;
+
+ if (!t->have_absdiff)
+ return -1.0;
+ sv = t->absdiff * sqrt(M_PI) / 2.0;
+ return sv > t->p.sigma_v_min ? sv : t->p.sigma_v_min;
+}
+
+bool pll_tune_input(struct pll_tune *t, double *fifo, double e_f, double u_applied)
+{
+ const unsigned int M = t->p.window / 5;
+ double sv, z = *fifo;
+
+ if (z <= 0.0) { /* link down / underrun: holdover */
+ t->ws_bad++;
+ t->have_last = false;
+ t->n_rej = 0;
+ return false;
+ }
+
+ /* noise estimate from 1-s differences of accepted samples */
+ if (t->have_last) {
+ double dz = fabs(z - t->last_fifo);
+ sv = pll_tune_sigma_v(t);
+ if (sv < 0.0 || dz < t->p.gate * M_SQRT2 * sv) {
+ if (!t->have_absdiff) {
+ t->absdiff = dz;
+ t->have_absdiff = true;
+ } else {
+ t->absdiff += t->p.ewma_tick * (dz - t->absdiff);
+ }
+ }
+ }
+
+ /* outlier gate against the smoothed error, never sticky */
+ sv = pll_tune_sigma_v(t);
+ if (sv >= 0.0 && !isnan(e_f) &&
+ fabs(z - (t->p.n_star + e_f)) > t->p.gate * sv && t->n_rej < t->p.max_reject) {
+ t->n_rej++;
+ t->n_rejected++;
+ z = t->p.n_star + e_f;
+ } else {
+ t->n_rej = 0;
+ t->last_fifo = z;
+ t->have_last = true;
+ }
+
+ /* window sums: head/tail FIFO means and mean applied fudge */
+ t->ws_n++;
+ t->ws_u += u_applied;
+ if (t->ws_k < M) {
+ t->ws_head += z;
+ t->ws_nh++;
+ } else if (t->ws_k >= t->p.window - M) {
+ t->ws_tail += z;
+ t->ws_nt++;
+ }
+
+ *fifo = z;
+ return true;
+}
+
+static bool retune(struct pll_tune *t)
+{
+ double sv = pll_tune_sigma_v(t);
+ double q = (t->v4 - t->v1) / (3.0 * t->p.window);
+ double wn;
+
+ if (sv <= 0.0 || q <= 0.0)
+ return false; // no evidence above the noise floor: keep gains
+
+ wn = pow(q / (sv * sv), 0.25);
+ if (wn < t->p.wn_min)
+ wn = t->p.wn_min;
+ if (wn > t->p.wn_max)
+ wn = t->p.wn_max;
+ if (t->wn > 0.0) {
+ if (wn < t->wn / t->p.max_ratio)
+ wn = t->wn / t->p.max_ratio;
+ if (wn > t->wn * t->p.max_ratio)
+ wn = t->wn * t->p.max_ratio;
+ }
+ t->wn = wn;
+ t->q_phi = q;
+ t->kp = 2.0 * t->p.zeta * wn / t->k_plant;
+ t->ki = wn * wn * t->p.tick_s / t->k_plant;
+ t->tau_f = 1.0 / (t->p.filt_factor * wn);
+ return true;
+}
+
+static bool close_window(struct pll_tune *t)
+{
+ const unsigned int M = t->p.window / 5;
+ bool ok = t->ws_bad <= t->p.window / 20 && t->ws_nh > 0 && t->ws_nt > 0;
+ double phi = 0.0;
+ bool all_ok = true;
+ unsigned int i;
+
+ if (ok) {
+ /* N(t2)-N(t1) = int(phi) - K*int(u) over the window */
+ double rate = (t->ws_tail / t->ws_nt - t->ws_head / t->ws_nh) /
+ (double)(t->p.window - M);
+ phi = rate + t->k_plant * t->ws_u / t->ws_n;
+ }
+ /* shift ring: [0] oldest ... [HIST-1] newest */
+ for (i = 0; i + 1 < PLL_TUNE_HIST; i++) {
+ t->phi[i] = t->phi[i + 1];
+ t->phi_ok[i] = t->phi_ok[i + 1];
+ }
+ t->phi[PLL_TUNE_HIST - 1] = phi;
+ t->phi_ok[PLL_TUNE_HIST - 1] = ok;
+ if (t->n_phi < PLL_TUNE_HIST)
+ t->n_phi++;
+ reset_window(t);
+ t->n_windows++;
+
+ // lag-1 uses windows [3],[4]; lag-4 uses [0],[4]: only those must be valid
+ all_ok = t->phi_ok[0] && t->phi_ok[3] && t->phi_ok[4];
+ if (t->n_phi == PLL_TUNE_HIST && all_ok) {
+ double d1 = t->phi[4] - t->phi[3];
+ double d4 = t->phi[4] - t->phi[0];
+ if (t->n_var == 0) {
+ t->v1 = d1 * d1;
+ t->v4 = d4 * d4;
+ } else {
+ t->v1 += t->p.ewma_win * (d1 * d1 - t->v1);
+ t->v4 += t->p.ewma_win * (d4 * d4 - t->v4);
+ }
+ t->n_var++;
+ }
+ if (t->n_windows >= t->p.warmup_windows && t->n_var >= t->p.min_var_samples)
+ return retune(t);
+ return false;
+}
+
+bool pll_tune_end_tick(struct pll_tune *t)
+{
+ t->ws_k++;
+ if (t->ws_k >= t->p.window)
+ return close_window(t);
+ return false;
+}
+
+double pll_tune_bumpless_integral(double u, double e_f, double d,
+ double kp, double ki, double kd)
+{
+ return (u - kp * e_f - kd * d) / ki;
+}
diff --git a/src/pll_tune.h b/src/pll_tune.h
new file mode 100644
index 0000000..a4fe1d8
--- /dev/null
+++ b/src/pll_tune.h
@@ -0,0 +1,100 @@
+/* SD-RI-PLL: online self-tuning of the PID gains (Kp, Ki) and of the e_i smoothing
+ *
+ * The PID law (paper Eq. 2) is NOT changed. This module watches the FIFO and the
+ * applied fudge, estimates the link noise (sigma_v) and the oscillator frequency
+ * wander (q_phi), and periodically hands new gains to the PID:
+ *
+ * wn = (q_phi / sigma_v^2)^(1/4), zeta = 1/sqrt(2)
+ * Kp = 2*zeta*wn / K, Ki = wn^2 * T / K, tau_f = 1 / (10*wn)
+ * K = frames_per_s / 2^32
+ *
+ * It also conditions the input (outlier gate) and tells the caller when the link
+ * is down so the PID output and integral are held (holdover, no windup).
+ *
+ * (C) 2025-2026 by C. Alejandra Carreon-Jimenez <alejandra.carreon@ale-leehrtz.org>
+ *
+ * All Rights Reserved
+ *
+ * SPDX-License-Identifier: GPL-2.0+
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ */
+#pragma once
+
+#include <stdbool.h>
+#include <stdint.h>
+
+#define PLL_TUNE_HIST 5 /* per-window frequency ring (lags 1 and 4) */
+
+struct pll_tune_params {
+ double n_star; /* 248 */
+ double tick_s; /* 1.0 */
+ double frames_per_s; /* 8000 */
+ unsigned int window; /* ticks per frequency window (3600) */
+ double zeta; /* 0.7071 */
+ double wn_min, wn_max; /* rad/s bounds on the loop bandwidth */
+ unsigned int warmup_windows; /* 6 */
+ unsigned int min_var_samples; /* 6 */
+ double max_ratio; /* max change of wn per retune (1.5) */
+ double ewma_win; /* EWMA weight for the window variances (0.1) */
+ double ewma_tick; /* EWMA weight for |dFIFO| (1e-3) */
+ double filt_factor; /* tau_f = 1/(filt_factor*wn) (10) */
+ double gate; /* outlier gate in sigma_v (6) */
+ double sigma_v_min; /* floor: FIFO quantization step/sqrt(12) (8 frames -> 2.31) */
+ unsigned int max_reject; /* consecutive rejections before forced accept (5) */
+};
+
+struct pll_tune {
+ struct pll_tune_params p;
+ double k_plant;
+ /* noise estimate */
+ double absdiff;
+ bool have_absdiff;
+ double last_fifo;
+ bool have_last;
+ unsigned int n_rej;
+ uint64_t n_rejected;
+ /* current window */
+ unsigned int ws_k, ws_nh, ws_nt, ws_bad, ws_n;
+ double ws_head, ws_tail, ws_u;
+ /* frequency history and Allan-style variances */
+ double phi[PLL_TUNE_HIST];
+ bool phi_ok[PLL_TUNE_HIST];
+ unsigned int n_phi;
+ double v1, v4;
+ unsigned int n_var, n_windows;
+ /* output */
+ double wn; /* 0 until the first retune */
+ double kp, ki, tau_f;
+ double q_phi;
+};
+
+void pll_tune_default_params(struct pll_tune_params *p);
+void pll_tune_init(struct pll_tune *t, const struct pll_tune_params *p);
+
+/* Call first in each PID tick.
+ * *fifo : raw e1t_fifo_level; replaced by the gated value to feed the PID
+ * e_f : the PID's current smoothed error (for the gate); pass NAN if none yet
+ * u_applied: fudge applied during the last tick (model sign, see sdripll_timer_cb())
+ * Returns false if the link is down (FIFO <= 0): skip the PID update, hold the output. */
+bool pll_tune_input(struct pll_tune *t, double *fifo, double e_f, double u_applied);
+
+/* Call after the PID update of a tick for which pll_tune_input() returned true.
+ * Returns true when new gains are in t->kp, t->ki, t->tau_f; apply them with
+ * pll_tune_bumpless_integral() so the fudge does not jump. */
+bool pll_tune_end_tick(struct pll_tune *t);
+
+/* Integral value that keeps u unchanged under the new gains:
+ * u = kp*e_f + ki*I + kd*d -> I = (u - kp*e_f - kd*d) / ki */
+double pll_tune_bumpless_integral(double u, double e_f, double d,
+ double kp, double ki, double kd);
+
+double pll_tune_sigma_v(const struct pll_tune *t);

To view, visit change 44231. To unsubscribe, or for help writing mail filters, visit settings.

Gerrit-MessageType: newchange
Gerrit-Project: osmo-e1d
Gerrit-Branch: master
Gerrit-Change-Id: Ifa5e1762df67c02093636908b45c6b5ce78fdc85
Gerrit-Change-Number: 44231
Gerrit-PatchSet: 1
Gerrit-Owner: ale-leehrtz <alejandra.carreon@ale-leehrtz.org>