dsfb-rf 1.0.1

DSFB-RF Structural Semiotics Engine for RF Signal Monitoring - A Deterministic, Non-Intrusive Observer Layer for Typed Structural Interpretation of IQ Residual Streams in Electronic Warfare, Spectrum Monitoring, and Cognitive Radio
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
//! Heuristics bank H: fixed-capacity typed RF motif library.
//!
//! The heuristics bank is what makes DSFB diagnostically superior to a
//! Luenberger observer for operator-facing applications. A Luenberger
//! observer has no memory of what structural pattern it is observing —
//! it sees only "error." The heuristics bank accumulates typed,
//! provenance-aware motif entries that distinguish classes of structural
//! behavior (paper §V-F, Table I).
//!
//! ## Design
//!
//! Fixed-capacity array `[MotifEntry; M]` — no heap, no alloc.
//! Generic parameter M = max entries (default 32 in the engine).
//! Linear scan for lookup: O(M) per observe() call, M ≤ 32 → negligible.
//!
//! ## Non-Attribution Policy (paper §V-F)
//!
//! Motif entries carry *candidate mechanism hypotheses*, not attributions.
//! No physical mechanism is attributed from public datasets (RadioML, ORACLE).
//! The `provenance` field records whether the entry is framework-designed,
//! empirically observed, or field-validated.

use crate::grammar::GrammarState;
use crate::syntax::MotifClass;

/// Provenance of a heuristics bank entry.
///
/// Records the epistemic status of a motif entry — how it was derived.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub enum Provenance {
    /// Derived from framework design principles.
    FrameworkDesign,
    /// Observed in public dataset (RadioML or ORACLE). No physical attribution.
    PublicDataObserved,
    /// Observed in deployment; field-validated by domain expert.
    FieldValidated,
}

/// Semantic disposition returned by heuristics bank lookup.
///
/// This is what the operator sees as the final semantic label.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub enum SemanticDisposition {
    /// Structurally confirmed pre-transition cluster.
    PreTransitionCluster,
    /// Corroborating drift — secondary support for escalation.
    CorroboratingDrift,
    /// Transient noise spike — no escalation warranted.
    TransientNoise,
    /// Recurrent structural pattern — watch and monitor.
    RecurrentPattern,
    /// Abrupt onset event — immediate escalation warranted.
    AbruptOnsetEvent,
    /// Spectral mask approach — proactive monitoring warranted.
    MaskApproach,
    /// Phase noise degradation — link quality monitoring.
    PhaseNoiseDegradation,
    /// Endoductive: no named interpretation. Returns full σ(k) to operator.
    Unknown,
    /// LNA gain instability: progressive gain collapse detected.
    /// Signature: linear norm ramp below 30% ρ, near-zero r̈.
    /// Recommended action: flag node telemetry for operator review.
    /// Do NOT abort mission or reset radio — this is read-only observer output.
    LnaGainInstability,
    /// LO oscillator instability precursor detected.
    /// Signature: `RecurrentBoundaryGrazing` with oscillatory slew.
    /// Consistent with phase-noise excursion (OcxoWarmup or FreeRunXtal class).
    /// Recommended action: tag geolocation/timing data with advisory.
    LoInstabilityPrecursor,
}

/// A single entry in the heuristics bank.
#[derive(Debug, Clone, Copy)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct MotifEntry {
    /// The motif class this entry matches.
    pub motif_class: MotifClass,
    /// Minimum grammar severity required to activate this entry.
    /// 0 = any, 1 = Boundary or above, 2 = Violation only.
    pub min_severity: u8,
    /// Semantic disposition returned when this entry matches.
    pub disposition: SemanticDisposition,
    /// Provenance of this entry.
    pub provenance: Provenance,
    /// Human-readable description (fixed-length str for no_std).
    pub description: &'static str,
}

impl MotifEntry {
    /// Returns true if this entry matches the given motif class and grammar severity.
    #[inline]
    pub fn matches(&self, motif: MotifClass, grammar: GrammarState) -> bool {
        self.motif_class == motif && grammar.severity() >= self.min_severity
    }
}

/// Fixed-capacity heuristics bank.
///
/// M = maximum number of entries. Populated at construction with the
/// framework-designed RF motif library. Additional entries can be
/// registered at runtime (up to M capacity) for endoductive learning.
pub struct HeuristicsBank<const M: usize> {
    entries: [Option<MotifEntry>; M],
    count: usize,
}

impl<const M: usize> HeuristicsBank<M> {
    /// Create an empty heuristics bank.
    pub const fn empty() -> Self {
        Self {
            entries: [None; M],
            count: 0,
        }
    }

    /// Create a heuristics bank pre-populated with the RF framework motif library.
    ///
    /// Populates with the 7 canonical RF motifs from paper §V-F.
    /// These are framework-designed entries (Provenance::FrameworkDesign).
    pub fn default_rf() -> Self {
        const MOTIFS: [MotifEntry; 9] = [
            MotifEntry { motif_class: MotifClass::PreFailureSlowDrift,       min_severity: 1, disposition: SemanticDisposition::PreTransitionCluster,    provenance: Provenance::FrameworkDesign, description: "Persistent outward drift toward boundary" },
            MotifEntry { motif_class: MotifClass::TransientExcursion,        min_severity: 2, disposition: SemanticDisposition::TransientNoise,          provenance: Provenance::FrameworkDesign, description: "Brief violation with rapid recovery" },
            MotifEntry { motif_class: MotifClass::RecurrentBoundaryApproach, min_severity: 1, disposition: SemanticDisposition::RecurrentPattern,        provenance: Provenance::FrameworkDesign, description: "Repeated near-boundary excursions" },
            MotifEntry { motif_class: MotifClass::AbruptOnset,               min_severity: 2, disposition: SemanticDisposition::AbruptOnsetEvent,        provenance: Provenance::FrameworkDesign, description: "Abrupt large slew" },
            MotifEntry { motif_class: MotifClass::SpectralMaskApproach,      min_severity: 1, disposition: SemanticDisposition::MaskApproach,            provenance: Provenance::FrameworkDesign, description: "Monotone outward drift toward mask edge" },
            MotifEntry { motif_class: MotifClass::PhaseNoiseExcursion,       min_severity: 1, disposition: SemanticDisposition::PhaseNoiseDegradation,   provenance: Provenance::FrameworkDesign, description: "Oscillatory slew with growing amplitude" },
            MotifEntry { motif_class: MotifClass::FreqHopTransition,         min_severity: 1, disposition: SemanticDisposition::TransientNoise,          provenance: Provenance::FrameworkDesign, description: "FHSS waveform transition (suppressible)" },
            MotifEntry { motif_class: MotifClass::LnaGainInstability,        min_severity: 1, disposition: SemanticDisposition::LnaGainInstability,      provenance: Provenance::FrameworkDesign, description: "Linear gain ramp, near-zero second derivative" },
            MotifEntry { motif_class: MotifClass::LoInstabilityPrecursor,    min_severity: 1, disposition: SemanticDisposition::LoInstabilityPrecursor,  provenance: Provenance::FrameworkDesign, description: "Recurrent boundary grazing with oscillatory slew" },
        ];
        let mut bank = Self::empty();
        for entry in MOTIFS {
            bank.register(entry);
        }
        bank
    }

    /// Register a new motif entry. Returns false if bank is full.
    pub fn register(&mut self, entry: MotifEntry) -> bool {
        if self.count >= M {
            return false;
        }
        self.entries[self.count] = Some(entry);
        self.count += 1;
        true
    }

    /// Look up a motif in the bank. Returns `Unknown` if no entry matches.
    ///
    /// Linear scan O(M). For M≤32 this is negligible per-observation cost.
    pub fn lookup(&self, motif: MotifClass, grammar: GrammarState) -> SemanticDisposition {
        for i in 0..self.count {
            if let Some(ref entry) = self.entries[i] {
                if entry.matches(motif, grammar) {
                    return entry.disposition;
                }
            }
        }
        // No match: endoductive Unknown
        SemanticDisposition::Unknown
    }

    /// Number of registered entries.
    #[inline]
    pub fn len(&self) -> usize {
        self.count
    }

    /// Returns true if the bank is empty.
    #[inline]
    pub fn is_empty(&self) -> bool {
        self.count == 0
    }

    /// Returns an iterator over populated entries.
    pub fn entries(&self) -> impl Iterator<Item = &MotifEntry> {
        self.entries[..self.count]
            .iter()
            .filter_map(|e| e.as_ref())
    }
}

// ---------------------------------------------------------------
// Clock Stability Library
// ---------------------------------------------------------------
// Allan deviation σ_y(τ) slope classification for oscillator-based
// internal-cause heuristics (paper §10.3 "Clock and LO Instability").
//
// When the grammar trips at Boundary/Violation AND the Physics model
// returns ArrheniusModel with low activation energy, a parallel check
// of the Allan deviation signature can distinguish:
//   – TCXO first-warmup (τ^{-1} slope → white FM, settling within ~60 s)
//   – OCXO warmup (τ^{-3/2} slope → flicker FM, oven thermal lag)
//   – Free-run crystal (τ^{+1/2} slope → random walk, no oven)
//   – PLL acquisition (transient oscillation in σ_y at τ ≈ 1/f_bw)
//
// These are classified by fitting a log-log slope α to σ_y(τ) ∝ τ^α
// and matching against the IEEE Std 1139-2008 Table 1 canonical slopes.
//
// Reference:
//   IEEE Std 1139-2008, "Characterization of Clocks and Oscillators".
//   Allan (1966), Proc. IEEE 54(2):221.

/// Canonical clock / oscillator instability classes matched by Allan slope.
///
/// Classified by fitting log-log slope α to σ_y(τ) ∝ τ^α across the
/// provided τ window.  α ≈ –1 → white FM; α ≈ –3/2 → flicker FM (OCXO);
/// α ≈ +1/2 → random walk FM; α ≈ 0 → flicker phase noise.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum KnownClockClass {
    /// White frequency modulation (α ≈ –1.0).
    /// Typical of a TCXO in steady-state or immediately post-warmup.
    TcxoSteadyState,
    /// Flicker frequency modulation (α ≈ –0.5, strong τ^0 floor).
    /// Typical of OCXO during oven thermal equilibration (~1–10 min).
    OcxoWarmup,
    /// Random walk frequency modulation (α ≈ +0.5).
    /// Typical of a free-run crystal without temperature compensation.
    FreeRunXtal,
    /// Transient oscillation around a fast-crossing τ value.
    /// Indicates an active PLL mid-acquisition or a loop bandwidth mismatch.
    PllAcquisition,
    /// Flicker phase noise (α ≈ –1.5), adjacent to carrier.
    /// Typical of a low-noise OCXO or Rb oscillator in steady-state.
    LowNoiseOcxo,
    /// Slope could not be determined (too few τ points or noisy data).
    Unknown,
}

impl KnownClockClass {
    /// Human-readable label for SigMF annotation or log emission.
    pub const fn label(self) -> &'static str {
        match self {
            KnownClockClass::TcxoSteadyState  => "TcxoSteadyState",
            KnownClockClass::OcxoWarmup        => "OcxoWarmup",
            KnownClockClass::FreeRunXtal       => "FreeRunXtal",
            KnownClockClass::PllAcquisition    => "PllAcquisition",
            KnownClockClass::LowNoiseOcxo      => "LowNoiseOcxo",
            KnownClockClass::Unknown           => "Unknown",
        }
    }

    /// Whether this class indicates an *internal* cause (clock/LO issue)
    /// rather than an *external* cause (channel interference).
    pub const fn is_internal_cause(self) -> bool {
        matches!(
            self,
            KnownClockClass::TcxoSteadyState
            | KnownClockClass::OcxoWarmup
            | KnownClockClass::FreeRunXtal
            | KnownClockClass::PllAcquisition
            | KnownClockClass::LowNoiseOcxo
        )
    }
}

/// Classify an oscillator's instability class from its Allan deviation curve.
///
/// # Arguments
/// - `sigma_y`  — array of Allan deviation values, one per τ point
/// - `taus`     — corresponding integration times τ (seconds), monotone increasing
///
/// Both slices must have the same length ≥ 3.  Shorter inputs return
/// `KnownClockClass::Unknown`.
///
/// ## Algorithm
///
/// Fits log-log slope α = Δ log(σ_y) / Δ log(τ) via least-squares over
/// all provided τ points, then maps the slope to a canonical class:
///
/// | α range          | Class              |
/// |------------------|--------------------|
/// | (−∞, −1.2)       | `LowNoiseOcxo`     |
/// | [−1.2, −0.7]     | `TcxoSteadyState`  |
/// | (−0.7, −0.1)     | `OcxoWarmup`       |
/// | [−0.1, +0.2)     | `PllAcquisition`   |
/// | [+0.2, ∞)        | `FreeRunXtal`      |
pub fn classify_clock_instability(sigma_y: &[f32], taus: &[f32]) -> KnownClockClass {
    if sigma_y.len() < 3 || taus.len() < 3 || sigma_y.len() != taus.len() {
        return KnownClockClass::Unknown;
    }
    let (sum_x, sum_y, sum_xx, sum_xy, m) = accumulate_log_sums(sigma_y, taus);
    if m < 3 {
        return KnownClockClass::Unknown;
    }
    let mf = m as f32;
    let denom = mf * sum_xx - sum_x * sum_x;
    if denom.abs() < 1e-9 {
        return KnownClockClass::Unknown;
    }
    let alpha = (mf * sum_xy - sum_x * sum_y) / denom;
    classify_slope(alpha)
}

fn accumulate_log_sums(sigma_y: &[f32], taus: &[f32]) -> (f32, f32, f32, f32, u32) {
    let log = |v: f32| -> f32 { crate::math::ln_f32(v.max(1e-38)) };
    let n = sigma_y.len().min(taus.len());
    let mut sum_x  = 0.0_f32;
    let mut sum_y  = 0.0_f32;
    let mut sum_xx = 0.0_f32;
    let mut sum_xy = 0.0_f32;
    let mut m = 0u32;
    for i in 0..n {
        if taus[i] > 0.0 && sigma_y[i] > 0.0 {
            let lx = log(taus[i]);
            let ly = log(sigma_y[i]);
            sum_x  += lx;
            sum_y  += ly;
            sum_xx += lx * lx;
            sum_xy += lx * ly;
            m += 1;
        }
    }
    (sum_x, sum_y, sum_xx, sum_xy, m)
}

fn classify_slope(alpha: f32) -> KnownClockClass {
    if alpha < -1.2 {
        KnownClockClass::LowNoiseOcxo
    } else if alpha <= -0.7 {
        KnownClockClass::TcxoSteadyState
    } else if alpha < -0.1 {
        KnownClockClass::OcxoWarmup
    } else if alpha < 0.2 {
        KnownClockClass::PllAcquisition
    } else {
        KnownClockClass::FreeRunXtal
    }
}

// ---------------------------------------------------------------
// Tests
// ---------------------------------------------------------------
#[cfg(test)]
mod tests {
    use super::*;
    use crate::grammar::GrammarState;

    #[test]
    fn default_bank_has_nine_entries() {
        let bank = HeuristicsBank::<32>::default_rf();
        assert_eq!(bank.len(), 9);
    }

    #[test]
    fn slow_drift_lookup_returns_pre_transition() {
        let bank = HeuristicsBank::<32>::default_rf();
        let disp = bank.lookup(
            MotifClass::PreFailureSlowDrift,
            GrammarState::Boundary(crate::grammar::ReasonCode::SustainedOutwardDrift),
        );
        assert_eq!(disp, SemanticDisposition::PreTransitionCluster);
    }

    #[test]
    fn unknown_motif_returns_unknown() {
        let bank = HeuristicsBank::<32>::default_rf();
        let disp = bank.lookup(MotifClass::Unknown, GrammarState::Admissible);
        assert_eq!(disp, SemanticDisposition::Unknown);
    }

    #[test]
    fn abrupt_onset_lookup() {
        let bank = HeuristicsBank::<32>::default_rf();
        let disp = bank.lookup(
            MotifClass::AbruptOnset,
            GrammarState::Violation,
        );
        assert_eq!(disp, SemanticDisposition::AbruptOnsetEvent);
    }

    #[test]
    fn bank_register_beyond_capacity_returns_false() {
        let mut bank = HeuristicsBank::<2>::empty();
        let entry = MotifEntry {
            motif_class: MotifClass::Unknown,
            min_severity: 0,
            disposition: SemanticDisposition::Unknown,
            provenance: Provenance::FrameworkDesign,
            description: "test",
        };
        assert!(bank.register(entry));
        assert!(bank.register(entry));
        assert!(!bank.register(entry), "should be full at M=2");
    }

    #[test]
    fn transient_excursion_requires_violation_severity() {
        let bank = HeuristicsBank::<32>::default_rf();
        // min_severity=2 (Violation), so Boundary should return Unknown
        let disp = bank.lookup(
            MotifClass::TransientExcursion,
            GrammarState::Boundary(crate::grammar::ReasonCode::SustainedOutwardDrift),
        );
        assert_eq!(disp, SemanticDisposition::Unknown,
            "TransientExcursion requires Violation severity");
    }

    // ── Clock stability library tests ────────────────────────────────────

    #[test]
    fn clock_labels_are_correct() {
        assert_eq!(KnownClockClass::TcxoSteadyState.label(), "TcxoSteadyState");
        assert_eq!(KnownClockClass::OcxoWarmup.label(),       "OcxoWarmup");
        assert_eq!(KnownClockClass::FreeRunXtal.label(),      "FreeRunXtal");
        assert_eq!(KnownClockClass::PllAcquisition.label(),   "PllAcquisition");
        assert_eq!(KnownClockClass::LowNoiseOcxo.label(),     "LowNoiseOcxo");
    }

    #[test]
    fn clock_all_are_internal() {
        for &cls in &[
            KnownClockClass::TcxoSteadyState,
            KnownClockClass::OcxoWarmup,
            KnownClockClass::FreeRunXtal,
            KnownClockClass::PllAcquisition,
            KnownClockClass::LowNoiseOcxo,
        ] {
            assert!(cls.is_internal_cause(), "{:?} should be internal", cls);
        }
        assert!(!KnownClockClass::Unknown.is_internal_cause());
    }

    #[test]
    fn classify_tcxo_steady_state_slope_minus_one() {
        // σ_y ∝ τ^{-1}: σ_y(τ) = 1e-11 / τ
        let taus:    [f32; 5] = [1.0, 2.0, 4.0, 8.0, 16.0];
        let sigma_y: [f32; 5] = [1e-11, 0.5e-11, 0.25e-11, 0.125e-11, 0.0625e-11];
        let cls = classify_clock_instability(&sigma_y, &taus);
        assert_eq!(cls, KnownClockClass::TcxoSteadyState, "α≈-1 slope: {:?}", cls);
    }

    #[test]
    fn classify_freerun_xtal_slope_plus_half() {
        // σ_y ∝ τ^{+0.5}: σ_y(τ) = 1e-11 * sqrt(τ)
        let taus:    [f32; 5] = [1.0, 4.0, 9.0, 16.0, 25.0];
        let sigma_y: [f32; 5] = [1e-11, 2e-11, 3e-11, 4e-11, 5e-11];
        let cls = classify_clock_instability(&sigma_y, &taus);
        assert_eq!(cls, KnownClockClass::FreeRunXtal, "α≈+0.5 slope: {:?}", cls);
    }

    #[test]
    fn classify_too_few_points_returns_unknown() {
        let taus:    [f32; 2] = [1.0, 2.0];
        let sigma_y: [f32; 2] = [1e-11, 0.5e-11];
        assert_eq!(classify_clock_instability(&sigma_y, &taus), KnownClockClass::Unknown);
    }
}