orion-sdr 0.0.52

DSP/SDR block library targeting HF-to-EHF, satellites, and Python bindings. Roadmap inside.
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
// Copyright (c) 2026 G & R Associates LLC
// SPDX-License-Identifier: MIT OR Apache-2.0

// src/python/dvb_t_frame.rs — PyO3 bindings for the conformant, preamble-less
// DVB-T on-air frame (ETSI EN 300 744).
//
// Exposes the stateful frame/super-frame modulator and demodulator objects
// (`modulate::{DvbTFrameMod, DvbTSuperFrameMod}` /
// `demodulate::{DvbTFrameDemod, DvbTSuperFrameDemod}`) and the streaming receiver,
// the shared `DvbTFrameParams`, the recovered `DvbTRxFrame`/`TpsWord`, and the
// `NbBandwidth` sample-rate helper. Transmission parameters (guard interval,
// constellation, code rate) are passed as strings, matching the convention used
// by the config-level DVB-T bindings in `python/ofdm.rs`. Integer-CFO correction
// is a construction-time flag on the demod objects (`with_integer_cfo_correction`).

use num_complex::Complex32;
use numpy::{IntoPyArray, PyArray1, PyReadonlyArray1};
use pyo3::exceptions::PyValueError;
use pyo3::prelude::*;
use pyo3::types::PyBytes;

use crate::demodulate::{DvbTFrameDemod, DvbTFrameStreamDemod, DvbTSuperFrameDemod};
use crate::fec::PunctureRate;
use crate::modulate::{ConstellationOrder, DvbTFrameMod, DvbTSuperFrameMod, DvbTSuperFrameParams};
use crate::waveform::dvb_t::{DvbTFrameParams, DvbTLinkParams, GuardInterval, NbBandwidth};
use crate::waveform::dvb_t_tps::TpsWord;

// ── String <-> enum helpers (crate Python convention) ───────────────────────

fn parse_guard(s: &str) -> PyResult<GuardInterval> {
    match s {
        "1/32" => Ok(GuardInterval::G1_32),
        "1/16" => Ok(GuardInterval::G1_16),
        "1/8" => Ok(GuardInterval::G1_8),
        "1/4" => Ok(GuardInterval::G1_4),
        other => Err(PyValueError::new_err(format!(
            "unknown guard interval {other:?} (expected 1/32, 1/16, 1/8, 1/4)"
        ))),
    }
}

fn guard_str(g: GuardInterval) -> &'static str {
    match g {
        GuardInterval::G1_32 => "1/32",
        GuardInterval::G1_16 => "1/16",
        GuardInterval::G1_8 => "1/8",
        GuardInterval::G1_4 => "1/4",
    }
}

fn parse_dvb_t_constellation(s: &str) -> PyResult<ConstellationOrder> {
    match s {
        "qpsk" => Ok(ConstellationOrder::Qpsk),
        "qam16" => Ok(ConstellationOrder::Qam16),
        "qam64" => Ok(ConstellationOrder::Qam64),
        other => Err(PyValueError::new_err(format!(
            "unknown DVB-T constellation {other:?} (expected qpsk, qam16, qam64)"
        ))),
    }
}

fn constellation_str(c: ConstellationOrder) -> PyResult<&'static str> {
    match c {
        ConstellationOrder::Qpsk => Ok("qpsk"),
        ConstellationOrder::Qam16 => Ok("qam16"),
        ConstellationOrder::Qam64 => Ok("qam64"),
        other => Err(PyValueError::new_err(format!(
            "{other:?} is not a DVB-T constellation"
        ))),
    }
}

fn parse_rate(s: &str) -> PyResult<PunctureRate> {
    match s {
        "1/2" => Ok(PunctureRate::R1_2),
        "2/3" => Ok(PunctureRate::R2_3),
        "3/4" => Ok(PunctureRate::R3_4),
        "5/6" => Ok(PunctureRate::R5_6),
        "7/8" => Ok(PunctureRate::R7_8),
        other => Err(PyValueError::new_err(format!(
            "unknown code rate {other:?} (expected 1/2, 2/3, 3/4, 5/6, 7/8)"
        ))),
    }
}

fn rate_str(r: PunctureRate) -> &'static str {
    match r {
        PunctureRate::R1_2 => "1/2",
        PunctureRate::R2_3 => "2/3",
        PunctureRate::R3_4 => "3/4",
        PunctureRate::R5_6 => "5/6",
        PunctureRate::R7_8 => "7/8",
    }
}

// ── DvbTFrameParams ─────────────────────────────────────────────────────────

/// Transmission parameters for a conformant DVB-T frame: guard interval,
/// constellation, code rate, and the TPS-signalled frame number and cell id.
/// `guard`/`constellation`/`code_rate` are strings (e.g. `"1/8"`, `"qpsk"`,
/// `"1/2"`).
#[pyclass(name = "DvbTFrameParams", skip_from_py_object)]
#[derive(Clone)]
pub struct PyDvbTFrameParams {
    inner: DvbTFrameParams,
}

#[pymethods]
impl PyDvbTFrameParams {
    #[new]
    #[pyo3(signature = (guard, constellation, code_rate, frame_number = 0, cell_id = 0))]
    fn new(
        guard: &str,
        constellation: &str,
        code_rate: &str,
        frame_number: u8,
        cell_id: u8,
    ) -> PyResult<Self> {
        Ok(Self {
            inner: DvbTFrameParams {
                link: DvbTLinkParams {
                    guard: parse_guard(guard)?,
                    constellation: parse_dvb_t_constellation(constellation)?,
                    code_rate: parse_rate(code_rate)?,
                },
                frame_number,
                cell_id,
            },
        })
    }

    #[getter]
    fn guard(&self) -> &'static str {
        guard_str(self.inner.guard())
    }
    #[getter]
    fn constellation(&self) -> PyResult<&'static str> {
        constellation_str(self.inner.constellation())
    }
    #[getter]
    fn code_rate(&self) -> &'static str {
        rate_str(self.inner.code_rate())
    }
    #[getter]
    fn frame_number(&self) -> u8 {
        self.inner.frame_number
    }
    #[getter]
    fn cell_id(&self) -> u8 {
        self.inner.cell_id
    }

    fn __repr__(&self) -> PyResult<String> {
        Ok(format!(
            "DvbTFrameParams(guard={:?}, constellation={:?}, code_rate={:?}, frame_number={}, cell_id={})",
            guard_str(self.inner.guard()),
            constellation_str(self.inner.constellation())?,
            rate_str(self.inner.code_rate()),
            self.inner.frame_number,
            self.inner.cell_id,
        ))
    }
}

// ── TpsWord (recovered) ─────────────────────────────────────────────────────

/// The transmission parameters recovered from a frame's TPS carriers.
#[pyclass(name = "TpsWord", skip_from_py_object)]
#[derive(Clone)]
pub struct PyTpsWord {
    inner: TpsWord,
}

#[pymethods]
impl PyTpsWord {
    #[getter]
    fn frame_number(&self) -> u8 {
        self.inner.frame_number
    }
    #[getter]
    fn constellation(&self) -> PyResult<&'static str> {
        constellation_str(self.inner.constellation)
    }
    #[getter]
    fn code_rate(&self) -> &'static str {
        rate_str(self.inner.code_rate_hp)
    }
    #[getter]
    fn guard(&self) -> &'static str {
        guard_str(self.inner.guard)
    }
    #[getter]
    fn cell_id(&self) -> u8 {
        self.inner.cell_id
    }

    fn __repr__(&self) -> PyResult<String> {
        Ok(format!(
            "TpsWord(frame_number={}, constellation={:?}, code_rate={:?}, guard={:?}, cell_id={})",
            self.inner.frame_number,
            constellation_str(self.inner.constellation)?,
            rate_str(self.inner.code_rate_hp),
            guard_str(self.inner.guard),
            self.inner.cell_id,
        ))
    }
}

// ── DvbTFrame (modulated) ───────────────────────────────────────────────────

/// A modulated DVB-T frame: the time-domain IQ plus the numerology a receiver
/// needs to acquire it.
#[pyclass(name = "DvbTFrame")]
pub struct PyDvbTFrame {
    iq: Vec<Complex32>,
    #[pyo3(get)]
    n_symbols: usize,
    #[pyo3(get)]
    samples_per_symbol: usize,
}

#[pymethods]
impl PyDvbTFrame {
    /// The time-domain baseband IQ (no preamble; a whole number of OFDM symbols).
    #[getter]
    fn iq<'py>(&self, py: Python<'py>) -> Bound<'py, PyArray1<Complex32>> {
        self.iq.clone().into_pyarray(py)
    }
}

// ── DvbTRxFrame (demodulated) ───────────────────────────────────────────────

/// The recovered contents of a DVB-T frame: the TS payload and the TPS word.
#[pyclass(name = "DvbTRxFrame")]
pub struct PyDvbTRxFrame {
    payload: Vec<u8>,
    #[pyo3(get)]
    tps: PyTpsWord,
}

#[pymethods]
impl PyDvbTRxFrame {
    /// The recovered TS payload bytes (depacketized, trimmed to `payload_len`).
    #[getter]
    fn payload<'py>(&self, py: Python<'py>) -> Bound<'py, PyBytes> {
        PyBytes::new(py, &self.payload)
    }
}

// ── DvbTFrameMod / DvbTFrameDemod ───────────────────────────────────────────

/// A conformant, preamble-less DVB-T frame modulator. Built from
/// `DvbTFrameParams`; `modulate(payload)` produces one `DvbTFrame` per call.
#[pyclass(name = "DvbTFrameMod")]
pub struct PyDvbTFrameMod {
    inner: DvbTFrameMod,
}

#[pymethods]
impl PyDvbTFrameMod {
    #[new]
    fn new(params: &PyDvbTFrameParams) -> Self {
        Self {
            inner: DvbTFrameMod::new(params.inner),
        }
    }

    /// Modulates `payload` (the MPEG-TS payload bytes) into one conformant,
    /// preamble-less DVB-T frame. Returns a `DvbTFrame` (`.iq`, `.n_symbols`,
    /// `.samples_per_symbol`).
    fn modulate(&self, payload: PyReadonlyArray1<'_, u8>) -> PyResult<PyDvbTFrame> {
        let frame = self.inner.modulate(payload.as_slice()?);
        Ok(PyDvbTFrame {
            iq: frame.iq,
            n_symbols: frame.n_symbols,
            samples_per_symbol: frame.samples_per_symbol,
        })
    }
}

/// A conformant, preamble-less DVB-T frame demodulator. Built from
/// `DvbTFrameParams`; `decode(iq, n_symbols, payload_len)` recovers one frame.
/// Integer-CFO correction is off by default — enable it with
/// `with_integer_cfo_correction(True)` (a link-constant builder that returns a new
/// demod).
#[pyclass(name = "DvbTFrameDemod")]
pub struct PyDvbTFrameDemod {
    inner: DvbTFrameDemod,
}

#[pymethods]
impl PyDvbTFrameDemod {
    #[new]
    fn new(params: &PyDvbTFrameParams) -> Self {
        Self {
            inner: DvbTFrameDemod::new(params.inner),
        }
    }

    /// Returns a demod with internal integer-CFO correction enabled (or disabled).
    /// A link-constant knob: when on, `decode` estimates the whole-subcarrier
    /// offset from the continual pilots and rotates it out before demapping.
    fn with_integer_cfo_correction(&self, on: bool) -> Self {
        Self {
            inner: self.inner.clone().with_integer_cfo_correction(on),
        }
    }

    /// Whether internal integer-CFO correction is enabled.
    #[getter]
    fn integer_cfo_correction(&self) -> bool {
        self.inner.integer_cfo_correction()
    }

    /// Demodulates one conformant DVB-T frame from `iq`, acquiring the symbol grid
    /// from the guard interval (no preamble). `n_symbols` is the frame's symbol
    /// count (from the paired `DvbTFrameMod.modulate` result's `DvbTFrame`);
    /// `payload_len` is the original payload byte count for trimming. Raises
    /// `ValueError` on any acquisition/decode failure. The returned `DvbTRxFrame`
    /// exposes `.payload` (bytes) and `.tps` (the recovered `TpsWord`).
    fn decode(
        &self,
        iq: PyReadonlyArray1<'_, Complex32>,
        n_symbols: usize,
        payload_len: usize,
    ) -> PyResult<PyDvbTRxFrame> {
        let rx = self
            .inner
            .decode(iq.as_slice()?, n_symbols, payload_len)
            .map_err(|e| PyValueError::new_err(e.to_string()))?;
        Ok(PyDvbTRxFrame {
            payload: rx.payload,
            tps: PyTpsWord { inner: rx.tps },
        })
    }
}

// ── NB bandwidth helpers ────────────────────────────────────────────────────

/// The sample rate (S/s) for a narrowband DVB-T bandwidth mode: `"333khz"`,
/// `"1mhz"`, or `"2mhz"`. NB-DVB-T is a pure fs-scaling of the fixed 2K
/// structure — `fs = occupied_hz · 2048/1705`.
#[pyfunction]
#[pyo3(name = "nb_bandwidth_fs")]
fn nb_bandwidth_fs(mode: &str) -> PyResult<f32> {
    Ok(parse_nb_bandwidth(mode)?.fs())
}

/// The nominal occupied RF bandwidth (Hz) for a narrowband DVB-T mode
/// (`"333khz"`, `"1mhz"`, `"2mhz"`).
#[pyfunction]
#[pyo3(name = "nb_bandwidth_occupied_hz")]
fn nb_bandwidth_occupied_hz(mode: &str) -> PyResult<f32> {
    Ok(parse_nb_bandwidth(mode)?.occupied_hz())
}

fn parse_nb_bandwidth(s: &str) -> PyResult<NbBandwidth> {
    match s {
        "333khz" | "333k" => Ok(NbBandwidth::Bw333kHz),
        "1mhz" | "1m" => Ok(NbBandwidth::Bw1MHz),
        "2mhz" | "2m" => Ok(NbBandwidth::Bw2MHz),
        other => Err(PyValueError::new_err(format!(
            "unknown NB bandwidth {other:?} (expected 333khz, 1mhz, 2mhz)"
        ))),
    }
}

// ── Super-frame ─────────────────────────────────────────────────────────────

/// Transmission parameters for a conformant DVB-T super-frame (four frames).
/// Like [`DvbTFrameParams`] but with the **full 16-bit** cell id, which is split
/// across the frames (b15..b8 in frames 1 & 3, b7..b0 in frames 2 & 4).
#[pyclass(name = "DvbTSuperFrameParams", skip_from_py_object)]
#[derive(Clone)]
pub struct PyDvbTSuperFrameParams {
    inner: DvbTSuperFrameParams,
}

#[pymethods]
impl PyDvbTSuperFrameParams {
    #[new]
    #[pyo3(signature = (guard, constellation, code_rate, cell_id = 0))]
    fn new(guard: &str, constellation: &str, code_rate: &str, cell_id: u16) -> PyResult<Self> {
        Ok(Self {
            inner: DvbTSuperFrameParams {
                link: DvbTLinkParams {
                    guard: parse_guard(guard)?,
                    constellation: parse_dvb_t_constellation(constellation)?,
                    code_rate: parse_rate(code_rate)?,
                },
                cell_id,
            },
        })
    }

    #[getter]
    fn guard(&self) -> &'static str {
        guard_str(self.inner.guard())
    }
    #[getter]
    fn constellation(&self) -> PyResult<&'static str> {
        constellation_str(self.inner.constellation())
    }
    #[getter]
    fn code_rate(&self) -> &'static str {
        rate_str(self.inner.code_rate())
    }
    #[getter]
    fn cell_id(&self) -> u16 {
        self.inner.cell_id
    }
}

/// A modulated DVB-T super-frame: the IQ of four consecutive frames plus the
/// numerology a receiver needs to re-slice them.
#[pyclass(name = "DvbTSuperFrame")]
pub struct PyDvbTSuperFrame {
    iq: Vec<Complex32>,
    #[pyo3(get)]
    symbols_per_frame: usize,
    #[pyo3(get)]
    samples_per_symbol: usize,
    frame_payload_lens: [usize; 4],
}

#[pymethods]
impl PyDvbTSuperFrame {
    /// The time-domain IQ of all four frames, concatenated (no preamble).
    #[getter]
    fn iq<'py>(&self, py: Python<'py>) -> Bound<'py, PyArray1<Complex32>> {
        self.iq.clone().into_pyarray(py)
    }
    /// The payload byte count carried by each of the four frames, in order.
    #[getter]
    fn frame_payload_lens(&self) -> [usize; 4] {
        self.frame_payload_lens
    }
    /// OFDM symbols across the whole super-frame (`4 · symbols_per_frame`).
    #[getter]
    fn n_symbols(&self) -> usize {
        4 * self.symbols_per_frame
    }
}

/// The recovered contents of a DVB-T super-frame: the concatenated payload and
/// the reassembled 16-bit cell id.
#[pyclass(name = "DvbTRxSuperFrame")]
pub struct PyDvbTRxSuperFrame {
    payload: Vec<u8>,
    #[pyo3(get)]
    cell_id: u16,
}

#[pymethods]
impl PyDvbTRxSuperFrame {
    /// The four frames' payloads, concatenated.
    #[getter]
    fn payload<'py>(&self, py: Python<'py>) -> Bound<'py, PyBytes> {
        PyBytes::new(py, &self.payload)
    }
}

/// A conformant DVB-T super-frame modulator (four frames, alternating TPS sync +
/// a 16-bit cell id split across them). Built from `DvbTSuperFrameParams`;
/// `modulate(payload)` produces one `DvbTSuperFrame` per call.
#[pyclass(name = "DvbTSuperFrameMod")]
pub struct PyDvbTSuperFrameMod {
    inner: DvbTSuperFrameMod,
}

#[pymethods]
impl PyDvbTSuperFrameMod {
    #[new]
    fn new(params: &PyDvbTSuperFrameParams) -> Self {
        Self {
            inner: DvbTSuperFrameMod::new(params.inner),
        }
    }

    /// Modulates `payload` into one conformant DVB-T super-frame. Returns a
    /// `DvbTSuperFrame` (`.iq`, `.symbols_per_frame`, `.samples_per_symbol`,
    /// `.frame_payload_lens`).
    fn modulate(&self, payload: PyReadonlyArray1<'_, u8>) -> PyResult<PyDvbTSuperFrame> {
        let sf = self.inner.modulate(payload.as_slice()?);
        Ok(PyDvbTSuperFrame {
            iq: sf.iq,
            symbols_per_frame: sf.symbols_per_frame,
            samples_per_symbol: sf.samples_per_symbol,
            frame_payload_lens: sf.frame_payload_lens,
        })
    }
}

/// A conformant DVB-T super-frame demodulator. Built from `DvbTSuperFrameParams`;
/// `decode(iq, symbols_per_frame, frame_payload_lens)` recovers one super-frame.
/// Integer-CFO correction is off by default — enable it with
/// `with_integer_cfo_correction(True)` (delegated to each constituent frame).
#[pyclass(name = "DvbTSuperFrameDemod")]
pub struct PyDvbTSuperFrameDemod {
    inner: DvbTSuperFrameDemod,
}

#[pymethods]
impl PyDvbTSuperFrameDemod {
    #[new]
    fn new(params: &PyDvbTSuperFrameParams) -> Self {
        Self {
            inner: DvbTSuperFrameDemod::new(params.inner),
        }
    }

    /// Returns a super-frame demod with internal integer-CFO correction enabled
    /// (or disabled) on every constituent frame.
    fn with_integer_cfo_correction(&self, on: bool) -> Self {
        Self {
            inner: self.inner.clone().with_integer_cfo_correction(on),
        }
    }

    /// Whether internal integer-CFO correction is enabled.
    #[getter]
    fn integer_cfo_correction(&self) -> bool {
        self.inner.integer_cfo_correction()
    }

    /// Demodulates one conformant DVB-T super-frame from `iq`. `symbols_per_frame`
    /// and `frame_payload_lens` come from the paired `DvbTSuperFrameMod.modulate`
    /// result. Verifies the frame-number sequence 0,1,2,3, reassembles the 16-bit
    /// cell id, and concatenates the payloads. Raises `ValueError` on failure. The
    /// returned `DvbTRxSuperFrame` exposes `.payload` (bytes) and `.cell_id`.
    fn decode(
        &self,
        iq: PyReadonlyArray1<'_, Complex32>,
        symbols_per_frame: usize,
        frame_payload_lens: [usize; 4],
    ) -> PyResult<PyDvbTRxSuperFrame> {
        let rx = self
            .inner
            .decode(iq.as_slice()?, symbols_per_frame, frame_payload_lens)
            .map_err(|e| PyValueError::new_err(e.to_string()))?;
        Ok(PyDvbTRxSuperFrame {
            payload: rx.payload,
            cell_id: rx.cell_id,
        })
    }
}

// ── Streaming receiver ──────────────────────────────────────────────────────

/// A streaming DVB-T receiver. Push IQ with `feed()`; it guard-interval-acquires
/// and decodes each fixed-size frame as its samples arrive, returning the
/// completed ones. `flush()` runs a final pass over the residual buffer. Pass
/// `integer_cfo_correction=True` to enable internal integer-CFO correction on each
/// decoded frame (a link-constant knob, set once here).
#[pyclass(name = "DvbTFrameStreamDemod")]
pub struct PyDvbTFrameStreamDemod {
    inner: DvbTFrameStreamDemod,
    integer_cfo: bool,
}

#[pymethods]
impl PyDvbTFrameStreamDemod {
    /// Builds a receiver for a link whose frames are `n_symbols` OFDM symbols
    /// carrying `payload_len` payload bytes each, under `params`. When
    /// `integer_cfo_correction` is `True`, each frame's whole-subcarrier CFO is
    /// estimated and removed internally before decoding.
    #[new]
    #[pyo3(signature = (params, n_symbols, payload_len, integer_cfo_correction = false))]
    fn new(
        params: &PyDvbTFrameParams,
        n_symbols: usize,
        payload_len: usize,
        integer_cfo_correction: bool,
    ) -> Self {
        Self {
            inner: DvbTFrameStreamDemod::new(params.inner, n_symbols, payload_len)
                .with_integer_cfo_correction(integer_cfo_correction),
            integer_cfo: integer_cfo_correction,
        }
    }

    /// Whether internal integer-CFO correction is enabled.
    #[getter]
    fn integer_cfo_correction(&self) -> bool {
        self.integer_cfo
    }

    /// Feeds IQ and returns the frames that completed. Frames that failed to
    /// decode are omitted; use `feed_with_errors` to see the reasons.
    fn feed(&mut self, iq: PyReadonlyArray1<'_, Complex32>) -> PyResult<Vec<PyDvbTRxFrame>> {
        Ok(self
            .inner
            .feed(iq.as_slice()?)
            .into_iter()
            .filter_map(|r| {
                r.ok().map(|f| PyDvbTRxFrame {
                    payload: f.payload,
                    tps: PyTpsWord { inner: f.tps },
                })
            })
            .collect())
    }

    /// Like `feed`, but returns a list of `(frame_or_None, error_or_None)` tuples
    /// so callers can observe decode failures.
    fn feed_with_errors(
        &mut self,
        iq: PyReadonlyArray1<'_, Complex32>,
    ) -> PyResult<Vec<(Option<PyDvbTRxFrame>, Option<String>)>> {
        Ok(self
            .inner
            .feed(iq.as_slice()?)
            .into_iter()
            .map(|r| match r {
                Ok(f) => (
                    Some(PyDvbTRxFrame {
                        payload: f.payload,
                        tps: PyTpsWord { inner: f.tps },
                    }),
                    None,
                ),
                Err(e) => (None, Some(e.to_string())),
            })
            .collect())
    }

    /// Runs a final decode pass over the residual buffer.
    fn flush(&mut self) -> Vec<PyDvbTRxFrame> {
        self.inner
            .flush()
            .into_iter()
            .filter_map(|r| {
                r.ok().map(|f| PyDvbTRxFrame {
                    payload: f.payload,
                    tps: PyTpsWord { inner: f.tps },
                })
            })
            .collect()
    }

    /// Number of accumulated (not-yet-consumed) samples.
    #[getter]
    fn buffered(&self) -> usize {
        self.inner.len()
    }

    /// Discards the accumulated buffer.
    fn clear(&mut self) {
        self.inner.clear();
    }
}

pub(crate) fn register(m: &Bound<'_, PyModule>) -> PyResult<()> {
    m.add_class::<PyDvbTFrameParams>()?;
    m.add_class::<PyDvbTFrame>()?;
    m.add_class::<PyDvbTRxFrame>()?;
    m.add_class::<PyTpsWord>()?;
    m.add_class::<PyDvbTFrameMod>()?;
    m.add_class::<PyDvbTFrameDemod>()?;
    m.add_class::<PyDvbTSuperFrameParams>()?;
    m.add_class::<PyDvbTSuperFrame>()?;
    m.add_class::<PyDvbTRxSuperFrame>()?;
    m.add_class::<PyDvbTSuperFrameMod>()?;
    m.add_class::<PyDvbTSuperFrameDemod>()?;
    m.add_class::<PyDvbTFrameStreamDemod>()?;
    m.add_function(wrap_pyfunction!(nb_bandwidth_fs, m)?)?;
    m.add_function(wrap_pyfunction!(nb_bandwidth_occupied_hz, m)?)?;
    Ok(())
}