voracious 0.4.1

VOR signal decoder for aviation navigation
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
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
//! ILS (Instrument Landing System) localizer decoder.
//!
//! The ILS localizer provides lateral (left-right) guidance for precision aircraft landings.
//! This module demodulates ILS localizer signals to extract:
//! - **90 Hz tone**: Amplitude modulation deeper on left side of centreline
//! - **150 Hz tone**: Amplitude modulation deeper on right side of centreline
//! - **DDM (Difference in Depth of Modulation)**: Lateral deviation indicator (−1.0 to +1.0)
//! - **Morse ident**: 4-letter station identifier on 1020 Hz tone
//!
//! ## Demodulation Process
//!
//! The `IlsDemodulator` processes raw I/Q samples or WAV audio through these steps:
//!
//! 1. **Frequency shifting** (I/Q path): Translates RF carrier to baseband
//! 2. **Baseband filtering**: 500 Hz lowpass removes RF noise before decimation
//! 3. **AM envelope detection**: Extracts baseband signal via magnitude (|I² + Q²|)
//! 4. **Decimation**: Reduces from 1.8 MSps to 9 kHz (decimation factor = 200)
//! 5. **Dual bandpass filtering**:
//!    - 90 Hz tone: 80–100 Hz bandpass filter
//!    - 150 Hz tone: 140–160 Hz bandpass filter
//! 6. **Envelope extraction**: Hilbert transform → magnitude → envelope
//!
//! ## DDM Calculation
//!
//! The [`compute_ddm`] function computes:
//!
//! ```text
//! DDM = (depth_90 - depth_150) / (depth_90 + depth_150)
//! ```
//!
//! Where `depth_90` and `depth_150` are the mean envelope amplitudes of the respective tones.
//!
//! **Interpretation:**
//! - **DDM > +0.015**: 90 Hz dominant → left of centreline → fly right
//! - **|DDM| ≤ 0.015**: on centreline → maintain
//! - **DDM < −0.015**: 150 Hz dominant → right of centreline → fly left
//!
//! The threshold of 0.015 is the FAA standard for "on-course" guidance.
//!
//! ## Filter Design
//!
//! All filter parameters (cutoff frequencies, orders, thresholds) are centralized in
//! the filter configuration. Key design choices:
//! - **Baseband LPF (500 Hz)**: Preserves both 90 Hz and 150 Hz tones while rejecting RF noise
//! - **90/150 Hz BPF (order 4)**: Narrow passband (20 Hz wide) for tone isolation
//! - **Morse envelope LPF (16 Hz stated, ~8 Hz effective)**: Suppresses ~36 Hz Hilbert ripple
//!   while resolving 100 ms dots at 9 kHz audio rate
//! - **DDM sum threshold (1e-9)**: Prevents division by near-zero when both tones are at noise floor
//!
//! ## WAV File Processing
//!
//! For pre-recorded audio files, use `IlsDemodulator::new_at_audio_rate` to skip the I/Q
//! decimation path. For Morse ident extraction over long files, use
//! `IlsDemodulator::precompute_morse_envelope` to apply zero-phase filtering across the
//! entire file at once, avoiding edge artifacts from rolling windowed filtering.
//!
//! Morse ident decoding is handled by the generic [`super::morse`] module.

use num_complex::Complex;
use serde::{Deserialize, Serialize};

// ── ILS Filter & Processing Constants ────────────────────────────────────────

/// ILS baseband lowpass filter cutoff frequency (Hz).
///
/// Pre-decimation filter removes out-of-band RF noise.
/// Typical range: 250–1000 Hz at full rate (1.8 MSps).
/// At post-decimation Nyquist of 4500 Hz (9 kHz rate), 500 Hz cutoff
/// preserves DC envelope and low-frequency modulation.
pub const ILS_BASEBAND_LPF_CUTOFF: f64 = 500.0;

/// ILS baseband lowpass filter order.
pub const ILS_BASEBAND_LPF_ORDER: usize = 5;

/// ILS 90 Hz tone bandpass filter low cutoff (Hz).
///
/// ILS 90 Hz localizer tone: deeper on the left of runway centreline.
/// Bandpass range: 80–100 Hz captures the tone with sidebands.
pub const ILS_90HZ_BPF_LOW: f64 = 80.0;

/// ILS 90 Hz tone bandpass filter high cutoff (Hz).
pub const ILS_90HZ_BPF_HIGH: f64 = 100.0;

/// ILS 90 Hz tone bandpass filter order.
pub const ILS_90HZ_BPF_ORDER: usize = 4;

/// ILS 150 Hz tone bandpass filter low cutoff (Hz).
///
/// ILS 150 Hz localizer tone: deeper on the right of runway centreline.
/// Bandpass range: 140–160 Hz captures the tone with sidebands.
pub const ILS_150HZ_BPF_LOW: f64 = 140.0;

/// ILS 150 Hz tone bandpass filter high cutoff (Hz).
pub const ILS_150HZ_BPF_HIGH: f64 = 160.0;

/// ILS 150 Hz tone bandpass filter order.
pub const ILS_150HZ_BPF_ORDER: usize = 4;

/// ILS Morse ident bandpass filter low cutoff (Hz).
///
/// Isolates the 1020 Hz Morse ident tone from the AM-modulated carrier.
/// Bandpass range: 900–1100 Hz.
pub const ILS_MORSE_BPF_LOW: f64 = 900.0;

/// ILS Morse ident bandpass filter high cutoff (Hz).
pub const ILS_MORSE_BPF_HIGH: f64 = 1_100.0;

/// ILS Morse ident bandpass filter order (IIR).
///
/// ILS uses zero-phase IIR filtfilt for sharper, better-normalized
/// bandpass response compared to FIR. Order 2 with filtfilt (4 biquads total,
/// 2 forward + 2 backward) provides Q ≈ 5 at 1020 Hz center.
pub const ILS_MORSE_BPF_ORDER: usize = 2;

/// ILS Morse audio output bandpass filter low cutoff (Hz).
///
/// Matches analysis filter band (980-1060 Hz) for consistent audio output.
/// Uses stateful FIR implementation for streaming audio compatibility.
pub const ILS_MORSE_AUDIO_BPF_LOW: f64 = 980.0;

/// ILS Morse audio output bandpass filter high cutoff (Hz).
pub const ILS_MORSE_AUDIO_BPF_HIGH: f64 = 1_060.0;

/// ILS Morse audio output bandpass filter order.
///
/// Order 4 (65 taps FIR) provides smooth passband for audio output.
/// Applied to audio output only, not to signal measurements.
pub const ILS_MORSE_AUDIO_BPF_ORDER: usize = 4;

/// ILS Morse ident lowpass filter cutoff frequency (Hz).
///
/// Applied to the envelope of the 1020 Hz Morse tone.
/// The Hilbert envelope of a narrow-band 1020 Hz tone has residual
/// oscillation at ~36 Hz (from noise beating). 16 Hz stated cutoff
/// applied via cascaded-biquad filtfilt with order=4 has an effective
/// -3 dB at ~0.5 × stated = ~8 Hz, which suppresses the 36 Hz ripple
/// while resolving 100 ms dots at 9 kHz audio rate.
pub const ILS_MORSE_ENV_LPF_CUTOFF: f64 = 16.0;

/// ILS Morse ident lowpass filter order (IIR).
///
/// Applied via cascaded-biquad filtfilt; order 4 means 2 biquads
/// each direction (4 total) for sharper roll-off.
pub const ILS_MORSE_ENV_LPF_ORDER: usize = 4;

/// ILS DDM threshold for on-course (Hz).
///
/// |DDM| ≤ 0.015 → on centreline
/// |DDM| > 0.015 → off centreline (left or right)
pub const ILS_DDM_ON_COURSE_THRESHOLD: f64 = 0.015;

/// ILS DDM computation threshold (minimum sum of 90 + 150 Hz).
///
/// If (mean_90 + mean_150) < this value, both tones are near zero
/// (noise floor) and DDM computation is unreliable. Fail gracefully.
pub const ILS_DDM_SUM_MIN: f64 = 1e-9;

/// ILS signal strength normalization factor.
///
/// Carrier strength = mean_envelope / this factor, clamped to [0, 1].
/// Typical normalized envelope magnitude is ~0.5, so dividing by 0.5
/// maps 0.5 → 1.0 (nominal), <0.1 → <0.2 (weak), >1.0 → 1.0 (clipped).
pub const ILS_CARRIER_NORMALIZATION: f64 = 0.5;

/// ILS baseband downsampling target audio rate (Hz).
///
/// Decimation factor from I/Q rate is calculated as
/// ceil(input_rate / this_value).
pub const ILS_DECIMATION_TARGET_RATE: f64 = 9_000.0;

/// Minimum RMS value before signal is considered noise.
///
/// Used in signal normalization across both VOR and ILS.
/// Below this threshold, RMS-based normalization is skipped
/// to avoid amplifying noise floor.
pub const SIGNAL_RMS_MIN: f64 = 1e-10;

/// Minimum sum of modulation depths for ILS DDM calculation.
///
/// If both 90 Hz and 150 Hz tones are below noise floor,
/// DDM is undefined. This threshold gates the calculation.
pub const MODULATION_DEPTH_MIN: f64 = 1e-9;
use std::f64::consts::PI;

use super::error::{Error, IlsDdmResult, Result};
use super::morse;
use crate::dsp_utils::{envelope, hilbert_transform};
use desperado::dsp::filters::ButterworthFilter;
use desperado::dsp::iir::{filtfilt_bandpass, filtfilt_lowpass};

/// Sample rate constants for ILS decoding
pub const ILS_SAMPLE_RATE_1_8M: u32 = 1_800_000;
pub const ILS_AUDIO_RATE: f64 = 9_000.0;

/// ILS side-of-centreline indication derived from DDM.
#[derive(Debug, Clone, Copy, PartialEq, Serialize, Deserialize)]
pub enum IlsSide {
    /// DDM > +0.015: 90 Hz dominant, aircraft is left of centreline → fly right
    Left,
    /// |DDM| ≤ 0.015: on centreline
    OnCourse,
    /// DDM < −0.015: 150 Hz dominant, aircraft is right of centreline → fly left
    Right,
}

impl IlsSide {
    /// Classify lateral position from a DDM value.
    ///
    /// - `|ddm| ≤ 0.015` → [`OnCourse`](Self::OnCourse)
    /// - `ddm > 0.015`  → [`Left`](Self::Left) (90 Hz dominant, fly right)
    /// - `ddm < −0.015` → [`Right`](Self::Right) (150 Hz dominant, fly left)
    pub fn from_ddm(ddm: f64) -> Self {
        if ddm > ILS_DDM_ON_COURSE_THRESHOLD {
            IlsSide::Left
        } else if ddm < -ILS_DDM_ON_COURSE_THRESHOLD {
            IlsSide::Right
        } else {
            IlsSide::OnCourse
        }
    }
}

/// One decoded ILS localizer measurement covering approximately one second of audio.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct IlsFrame {
    /// Unix timestamp (seconds) of the end of this measurement window.
    pub timestamp: f64,
    /// Localizer centre frequency in MHz (derived from carrier offset + centre freq).
    pub frequency_mhz: f64,
    /// Difference in Depth of Modulation: (env90 − env150) / (env90 + env150).
    /// Bounded −1.0..+1.0; positive = left of centreline.
    pub ddm: f64,
    /// 90 Hz modulation depth as a percentage of total signal (0–100).
    pub mod_90_pct: f64,
    /// 150 Hz modulation depth as a percentage of total signal (0–100).
    pub mod_150_pct: f64,
    /// Mean signal strength (normalised 0–1), derived from the AM envelope.
    pub signal_strength: f64,
    /// Decoded Morse ident if present (e.g. `"IKLO"`).
    pub ident: Option<String>,
    /// Optional debug details about Morse decoding attempts and candidates.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub morse_debug: Option<IlsMorseDebugInfo>,
    /// Lateral position interpretation derived from DDM.
    pub side: IlsSide,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct IlsMorseDebugInfo {
    pub candidates: Vec<IlsMorseCandidate>,
    pub total_tokens: usize,
    pub decode_attempts: Vec<morse::MorseDecodeAttempt>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct IlsMorseCandidate {
    pub token: String,
    pub count: usize,
    pub confidence: f64,
}

/// ILS localizer demodulator.
///
/// Processes raw cf32 I/Q samples at 1.8 MSps and extracts 90 Hz / 150 Hz
/// tone envelopes needed to compute DDM.
pub struct IlsLocalizerDemodulator {
    /// Input I/Q sample rate in Hz.
    pub sample_rate: f64,
    /// Decimated audio sample rate in Hz (= sample_rate / decim_factor).
    pub audio_rate: f64,
    decim_factor: usize,

    // Filters applied at the full I/Q sample rate
    baseband_lpf: ButterworthFilter,

    // Filters applied at the decimated audio rate
    bpf_90: ButterworthFilter,
    bpf_150: ButterworthFilter,
}

impl IlsLocalizerDemodulator {
    /// Create a new demodulator for the given I/Q sample rate.
    ///
    /// With the default 1.8 MSps input the decimation factor is 200,
    /// yielding a 9 kHz audio rate.
    pub fn new(sample_rate: u32) -> Self {
        let sample_rate_f = sample_rate as f64;
        // Keep the audio rate at exactly 9 kHz regardless of rounding
        let decim_factor = ((sample_rate_f / ILS_AUDIO_RATE).round() as usize).max(1);
        let audio_rate = sample_rate_f / decim_factor as f64;

        Self {
            sample_rate: sample_rate_f,
            audio_rate,
            decim_factor,

            baseband_lpf: ButterworthFilter::lowpass(
                ILS_BASEBAND_LPF_CUTOFF,
                sample_rate_f,
                ILS_BASEBAND_LPF_ORDER,
            ),

            bpf_90: ButterworthFilter::bandpass(
                ILS_90HZ_BPF_LOW,
                ILS_90HZ_BPF_HIGH,
                audio_rate,
                ILS_90HZ_BPF_ORDER,
            ),
            bpf_150: ButterworthFilter::bandpass(
                ILS_150HZ_BPF_LOW,
                ILS_150HZ_BPF_HIGH,
                audio_rate,
                ILS_150HZ_BPF_ORDER,
            ),
            // Note: 900–1100 Hz Morse ident filtering is done with filtfilt_bandpass
            // in decode_ident(), not with a stateful BPF here.
        }
    }

    /// Create a demodulator that operates directly on audio-rate samples (e.g. from a WAV file).
    ///
    /// Bypasses the I/Q decimation path: all filters are built at `audio_rate` directly,
    /// and the pre-decimation lowpass is a trivial placeholder.
    pub fn new_at_audio_rate(audio_rate: f64) -> Self {
        Self {
            sample_rate: audio_rate,
            audio_rate,
            decim_factor: 1,
            // Not used in the WAV path, but must be initialised
            baseband_lpf: ButterworthFilter::lowpass(audio_rate / 2.0 * 0.9, audio_rate, 1),
            bpf_90: ButterworthFilter::bandpass(
                ILS_90HZ_BPF_LOW,
                ILS_90HZ_BPF_HIGH,
                audio_rate,
                ILS_90HZ_BPF_ORDER,
            ),
            bpf_150: ButterworthFilter::bandpass(
                ILS_150HZ_BPF_LOW,
                ILS_150HZ_BPF_HIGH,
                audio_rate,
                ILS_150HZ_BPF_ORDER,
            ),
        }
    }

    /// Expose the audio (post-decimation) sample rate.
    pub fn audio_rate(&self) -> f64 {
        self.audio_rate
    }

    /// Process pre-demodulated audio samples (e.g. from a WAV file) and return the
    /// 90 Hz and 150 Hz tone envelopes, bypassing the I/Q decimation path.
    ///
    /// The input is the raw signed AM audio (bipolar signal).
    /// Returns `(env_90, env_150)`.
    pub fn filter_audio_envelopes(&mut self, audio: &[f64]) -> (Vec<f64>, Vec<f64>) {
        // Use IIR filtfilt bandpass instead of the broken ButterworthFilter::bandpass.
        //
        // The FIR bandpass (ButterworthFilter::bandpass) has only ~65 taps at 48 kHz;
        // it subtracts two independently-normalised lowpass filters, producing near-zero
        // response (~0.0001 instead of 0.04–0.10) due to normalization mismatch.
        // The IIR filtfilt_bandpass correctly extracts the 90/150 Hz tones.
        let tone_90 = filtfilt_bandpass(
            audio,
            ILS_90HZ_BPF_LOW,
            ILS_90HZ_BPF_HIGH,
            self.audio_rate,
            ILS_90HZ_BPF_ORDER,
        );
        let analytic_90 = hilbert_transform(&tone_90);
        let env_90 = envelope(&analytic_90);

        let tone_150 = filtfilt_bandpass(
            audio,
            ILS_150HZ_BPF_LOW,
            ILS_150HZ_BPF_HIGH,
            self.audio_rate,
            ILS_150HZ_BPF_ORDER,
        );
        let analytic_150 = hilbert_transform(&tone_150);
        let env_150 = envelope(&analytic_150);

        (env_90, env_150)
    }

    /// Decode Morse ident from pre-demodulated audio samples (WAV path).
    pub fn decode_ident_audio(
        &mut self,
        audio: &[f64],
    ) -> (Option<String>, Vec<String>, Vec<morse::MorseDecodeAttempt>) {
        self.decode_ident(audio)
    }

    /// Pre-compute the full 1020 Hz Morse envelope for an entire WAV file.
    ///
    /// **Note**: This method is deprecated. For WAV file processing, prefer using
    /// the `sources::wav` module which handles carrier removal, timestamp parsing,
    /// and uses the shared DSP pipeline from the `sources::common` module.
    ///
    /// Because the Morse envelope lowpass (`filtfilt`) needs context on both
    /// sides of each sample to work correctly (zero-phase filtering), processing
    /// the entire file at once produces far better results than filtering
    /// successive 15-second chunks.  Call this once during source initialisation
    /// and slice the returned vector per-window during decoding.
    ///
    /// Returns a vector of the same length as `audio` containing the smoothed
    /// 1020 Hz tone envelope at the WAV sample rate.
    #[allow(dead_code)]
    pub fn precompute_morse_envelope(&mut self, audio: &[f64]) -> Vec<f64> {
        // Use IIR filtfilt bandpass instead of the FIR bpf_ident.
        //
        // The FIR bandpass (ButterworthFilter::bandpass) has only ~65 taps at
        // 48 kHz; its transition bands are far too wide to isolate 1020 Hz, and
        // the passband output is dominated by out-of-band noise.  The IIR
        // filtfilt_bandpass has a Q = 1000/200 = 5, giving a sharp, narrow band
        // centred on 1000 Hz that correctly extracts the 1020 Hz ident tone even
        // at modest SNR.
        let tone = filtfilt_bandpass(
            audio,
            ILS_MORSE_BPF_LOW,
            ILS_MORSE_BPF_HIGH,
            self.audio_rate,
            ILS_MORSE_BPF_ORDER,
        );
        let analytic = hilbert_transform(&tone);
        let env_raw = envelope(&analytic);
        // The Morse envelope lowpass with stated 16 Hz cutoff.
        // The cascaded-biquad filtfilt in this codebase with order=4 has an
        // effective -3 dB at approximately 0.5 × stated cutoff (~8 Hz effective).
        // This is aggressive enough to suppress the ~36 Hz Hilbert envelope ripple
        // while still resolving individual 100 ms dots and 120 ms inter-element gaps.
        // Processing the full file at once (rather than rolling chunks) avoids filtfilt edge artefacts.
        filtfilt_lowpass(
            &env_raw,
            ILS_MORSE_ENV_LPF_CUTOFF,
            self.audio_rate,
            ILS_MORSE_ENV_LPF_ORDER,
        )
    }

    /// Demodulate a chunk of I/Q samples.
    ///
    /// Performs the full demodulation pipeline on I/Q samples:
    /// frequency shift → baseband filtering → AM demodulation →
    /// decimation → dual bandpass filtering → envelope extraction.
    ///
    /// # Arguments
    ///
    /// - `iq_samples`: Raw I/Q samples at the configured input sample rate
    /// - `freq_offset`: Frequency shift to apply before baseband filtering (Hz)
    ///
    /// # Returns
    ///
    /// A tuple of three vectors, all at the audio rate (see [`audio_rate`](Self::audio_rate)):
    ///
    /// - `env_90`: Instantaneous envelope of the 90 Hz tone (Hilbert magnitude)
    /// - `env_150`: Instantaneous envelope of the 150 Hz tone (Hilbert magnitude)
    /// - `audio_envelope`: Full AM envelope at audio rate (used for signal strength + ident)
    ///
    /// Returns empty vectors if input is empty.
    pub fn demodulate(
        &mut self,
        iq_samples: &[Complex<f32>],
        freq_offset: f64,
    ) -> (Vec<f64>, Vec<f64>, Vec<f64>) {
        if iq_samples.is_empty() {
            return (Vec::new(), Vec::new(), Vec::new());
        }

        // 1. Frequency-shift the carrier to DC
        let iq_shifted: Vec<Complex<f32>> = iq_samples
            .iter()
            .enumerate()
            .map(|(i, &s)| {
                let t = i as f64 / self.sample_rate;
                let shift = Complex::new(
                    (-2.0 * PI * freq_offset * t).cos() as f32,
                    (-2.0 * PI * freq_offset * t).sin() as f32,
                );
                s * shift
            })
            .collect();

        // 2. Lowpass filter to remove off-channel energy before decimation
        let iq_filtered = self.baseband_lpf.filter_complex(&iq_shifted);

        // 3. AM envelope detection: |IQ|
        let am_envelope: Vec<f64> = iq_filtered.iter().map(|c| c.norm() as f64).collect();

        // 4. Decimate to audio rate
        let audio: Vec<f64> = am_envelope
            .iter()
            .step_by(self.decim_factor)
            .copied()
            .collect();

        if audio.is_empty() {
            return (Vec::new(), Vec::new(), Vec::new());
        }

        // 5 & 6. Extract 90 Hz and 150 Hz tone envelopes
        let (env_90, env_150) = self.extract_tones_and_audio(&audio);

        (env_90, env_150, audio)
    }

    /// Extract 90 Hz and 150 Hz tone envelopes from audio samples.
    ///
    /// This is the core tone extraction logic, factored out so it can be
    /// reused by both I/Q demodulation and direct audio processing paths.
    ///
    /// # Arguments
    ///
    /// - `audio`: Audio samples already at the target audio rate (e.g., 48 kHz)
    ///
    /// # Returns
    ///
    /// A tuple of:
    /// - `env_90`: Envelope of the 90 Hz modulation tone
    /// - `env_150`: Envelope of the 150 Hz modulation tone
    ///
    /// Both outputs are at the audio rate. Returns empty vectors if input is empty.
    pub fn extract_tones_and_audio(&mut self, audio: &[f64]) -> (Vec<f64>, Vec<f64>) {
        if audio.is_empty() {
            return (Vec::new(), Vec::new());
        }

        // Extract 90 Hz tone envelope
        let tone_90 = self.bpf_90.filter(audio);
        let analytic_90 = hilbert_transform(&tone_90);
        let env_90 = envelope(&analytic_90);

        // Extract 150 Hz tone envelope
        let tone_150 = self.bpf_150.filter(audio);
        let analytic_150 = hilbert_transform(&tone_150);
        let env_150 = envelope(&analytic_150);

        (env_90, env_150)
    }

    /// Demodulate pre-decimated audio to extract ILS 90/150 Hz tone envelopes.
    ///
    /// This method is used when audio is already available (e.g., from a WAV file)
    /// and doesn't need frequency shifting or the full I/Q demodulation pipeline.
    ///
    /// # Arguments
    ///
    /// - `audio_samples`: Audio samples at the configured audio rate
    ///
    /// # Returns
    ///
    /// A tuple of:
    /// - `env_90`: Envelope of the 90 Hz modulation tone
    /// - `env_150`: Envelope of the 150 Hz modulation tone
    /// - `audio_out`: The input audio (returned unchanged for pipeline compatibility)
    ///
    /// All outputs are at the audio rate. Returns empty vectors if input is empty.
    pub fn demodulate_audio(&mut self, audio_samples: &[f64]) -> (Vec<f64>, Vec<f64>, Vec<f64>) {
        if audio_samples.is_empty() {
            return (Vec::new(), Vec::new(), Vec::new());
        }

        let (env_90, env_150) = self.extract_tones_and_audio(audio_samples);
        (env_90, env_150, audio_samples.to_vec())
    }

    /// Decode the 1020 Hz Morse ident from audio-rate samples.
    ///
    /// Applies a zero-phase IIR 900–1100 Hz bandpass (`filtfilt_bandpass`),
    /// Hilbert envelope, and zero-phase lowpass, then delegates to
    /// [`morse::decode_morse_ident`].
    ///
    /// Using `filtfilt_bandpass` (IIR, Q ≈ 5, 0 dB peak gain) instead of the
    /// FIR `bpf_ident` is critical: the FIR filter has too few taps at 9 kHz to
    /// adequately narrow the passband, so its output is dominated by out-of-band
    /// noise.  The IIR filtfilt applies twice (forward + backward) but with the
    /// corrected 0 dB normalisation, so the total gain stays near unity.
    pub fn decode_ident(
        &mut self,
        audio: &[f64],
    ) -> (Option<String>, Vec<String>, Vec<morse::MorseDecodeAttempt>) {
        if audio.len() < (self.audio_rate as usize / 2) {
            return (None, Vec::new(), Vec::new());
        }

        // IIR zero-phase bandpass: sharper and better-normalised than the FIR path.
        let tone = filtfilt_bandpass(
            audio,
            ILS_MORSE_BPF_LOW,
            ILS_MORSE_BPF_HIGH,
            self.audio_rate,
            ILS_MORSE_BPF_ORDER,
        );
        let analytic = hilbert_transform(&tone);
        let env_raw = envelope(&analytic);

        // LP cutoff with stated value; actual effective cutoff is ~0.5 × stated
        // with cascaded-biquad filtfilt order=4.
        let env = filtfilt_lowpass(
            &env_raw,
            ILS_MORSE_ENV_LPF_CUTOFF,
            self.audio_rate,
            ILS_MORSE_ENV_LPF_ORDER,
        );

        morse::decode_morse_ident(&env, self.audio_rate)
    }
}

/// Compute DDM from mean envelopes over a window.
///
/// Returns `(ddm, mod_90_pct, mod_150_pct, signal_strength)`.
/// Returns `None` if the window is empty or the sum is too small (noise floor).
/// Compute ILS Difference in Depth of Modulation (DDM) from demodulated signals.
///
/// Returns structured result containing DDM, modulation depths, and carrier strength,
/// with proper error handling for edge cases.
pub fn compute_ddm(
    env_90: &[f64],
    env_150: &[f64],
    audio_envelope: &[f64],
) -> Result<IlsDdmResult> {
    let n = env_90.len().min(env_150.len()).min(audio_envelope.len());
    if n == 0 {
        return Err(Error::EmptyInput);
    }

    // Validate input signals don't contain NaN/Inf
    if env_90[..n].iter().any(|&x| !x.is_finite())
        || env_150[..n].iter().any(|&x| !x.is_finite())
        || audio_envelope[..n].iter().any(|&x| !x.is_finite())
    {
        return Err(Error::NonFiniteSignal);
    }

    let mean_90 = env_90[..n].iter().sum::<f64>() / n as f64;
    let mean_150 = env_150[..n].iter().sum::<f64>() / n as f64;
    let mean_env = audio_envelope[..n].iter().sum::<f64>() / n as f64;

    let sum_90_150 = mean_90 + mean_150;
    if sum_90_150 < ILS_DDM_SUM_MIN {
        return Err(Error::DdmComputationFailed {
            reason: "both 90 Hz and 150 Hz modulation depths are near zero".to_string(),
        });
    }

    let ddm = (mean_90 - mean_150) / sum_90_150;

    // For WAV files with pre-demodulated audio, the envelope values from
    // filtfilt_bandpass may have a different scale than expected. Clamp modulation
    // percentages to a reasonable range [0, 100].
    let (mod_90_pct, mod_150_pct) = if mean_env > MODULATION_DEPTH_MIN {
        let pct_90 = (mean_90 / mean_env * 100.0).min(100.0);
        let pct_150 = (mean_150 / mean_env * 100.0).min(100.0);
        (pct_90, pct_150)
    } else {
        (0.0, 0.0)
    };

    // Signal strength: normalised carrier level (clamp to 0..1)
    let carrier_strength = (mean_env / ILS_CARRIER_NORMALIZATION).clamp(0.0, 1.0);

    Ok(IlsDdmResult {
        ddm,
        mod_90_hz: mod_90_pct,
        mod_150_hz: mod_150_pct,
        carrier_strength,
    })
}

// ── Internal DSP helpers ──────────────────────────────────────────────────────

// ── Public constructor for IlsFrame ──────────────────────────────────────────

impl IlsFrame {
    /// Build an `IlsFrame` from pre-computed DDM components.
    pub fn new(
        timestamp: f64,
        frequency_mhz: f64,
        ddm: f64,
        mod_90_pct: f64,
        mod_150_pct: f64,
        signal_strength: f64,
        ident: Option<String>,
    ) -> Self {
        let side = IlsSide::from_ddm(ddm);
        Self {
            timestamp,
            frequency_mhz,
            ddm,
            mod_90_pct,
            mod_150_pct,
            signal_strength,
            ident,
            morse_debug: None,
            side,
        }
    }

    pub fn with_morse_debug(mut self, debug: Option<IlsMorseDebugInfo>) -> Self {
        self.morse_debug = debug;
        self
    }
}