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
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
"""
Type stubs for the orion-sdr native extension module.
All classes live in the flat ``orion_sdr`` namespace. IQ arrays use
``numpy.complex64``; audio arrays use ``numpy.float32``; bit arrays use
``numpy.uint8`` (one bit per byte, value 0 or 1). Every ``process()``
call returns a new 1-D array; arrays must be 1-D and C-contiguous, or a
``ValueError`` is raised.
"""
# ---------------------------------------------------------------------------
# Demodulators (IQ complex64 → audio float32)
# ---------------------------------------------------------------------------
"""One-pole envelope detector for CW signals.
Tracks the instantaneous magnitude of the IQ input with a low-pass
time constant derived from *env_bw_hz*. *tone_hz* is accepted for
API symmetry but is not used internally (pre-tune the signal before
passing it in).
"""
...
...
...
"""AM envelope demodulator with 4th-order IIR low-pass and DC blocker.
Two envelope methods are available:
* ``abs_approx=False`` (default) — ``sqrt(I² + Q²)`` after the LP filter
(*PowerSqrt*); highest fidelity.
* ``abs_approx=True`` — ``k1·|I| + k2·|Q|`` approximation (*AbsApprox*,
k1=0.9482, k2=0.3920); slightly faster with a small amplitude error.
"""
...
...
"""SSB product detector with BFO rotator and 4th-order IIR audio filter.
Set *bfo_hz* to 0 for a signal already tuned to baseband, or to a small
offset to shift the recovered audio pitch.
"""
...
...
"""FM quadrature (phase-difference) discriminator.
Output is scaled so that ±*dev_hz* of instantaneous frequency deviation
maps to roughly ±1.0. A 4th-order IIR low-pass at *audio_bw_hz* follows
the discriminator.
"""
...
...
"""PM quadrature demodulator (instantaneous phase difference).
*k* scales the recovered phase difference to the output audio level.
A 4th-order IIR low-pass at *audio_bw_hz* follows the discriminator.
"""
...
...
# ---------------------------------------------------------------------------
# Digital demodulators (IQ complex64 → bits uint8)
# ---------------------------------------------------------------------------
"""BPSK demodulator: hard-decision slicer.
Input: complex64 IQ array, carrier-removed baseband, 1 sample per symbol.
Output: uint8 bit array — one bit (0 or 1) per input symbol.
Decision rule: Re(z) ≥ 0 → 0, Re(z) < 0 → 1.
*gain* scales the soft metric before slicing (use 1.0 for normalized input).
"""
...
...
...
"""QPSK demodulator: hard-decision slicer.
Input: complex64 IQ array, carrier-removed baseband, 1 sample per symbol.
Output: uint8 bit array — two bits per input symbol, interleaved as
``[b0_I, b0_Q, b1_I, b1_Q, …]``. Matches the Gray coding of ``QpskMod``.
*gain* scales the soft metric before slicing (use 1.0 for normalized input).
"""
...
...
...
"""QAM demodulator: hard-decision slicer for square QAM constellations.
*order* must be 16, 64, or 256 (raises ``ValueError`` otherwise).
Input: complex64 IQ array, carrier-removed baseband, 1 sample per symbol.
Output: uint8 bit array — ``log2(order)`` bits per input symbol, laid out
as ``log2(order)/2`` I-axis bits then ``log2(order)/2`` Q-axis bits
(MSB-first within each axis Gray index). Matches ``QamMod`` bit layout.
*gain* scales the soft metric before slicing (use 1.0 for normalized input).
"""
...
...
...
# ---------------------------------------------------------------------------
# Modulators (audio float32 → IQ complex64)
# ---------------------------------------------------------------------------
"""AM double-sideband modulator with optional carrier and RF upconversion.
* *carrier_level* — 1.0 produces full carrier (A3E); 0.0 gives DSB-SC.
* *modulation_index* — values ≤ 1.0 are recommended to avoid
over-modulation.
* *rf_hz* — set to 0.0 for baseband IQ output.
"""
...
...
"""Clamp the modulated envelope to ±1 to prevent over-modulation."""
...
...
"""CW keyed carrier modulator with shaped rise/fall envelope.
The input array is a **keying envelope** in the range 0..1 (not raw
audio): 1.0 = key down, 0.0 = key up. Rise and fall times are
smoothed by one-pole filters with time constants *rise_ms* / *fall_ms*
to suppress key clicks.
"""
...
...
...
"""FM modulator using a phasor-recurrence phase accumulator.
Each sample multiplies a running phasor by ``exp(j·2π·kf·x/fs)``
where ``kf = deviation_hz``. The phasor is renormalized every 1024
samples to prevent amplitude drift. Set *rf_hz* to 0.0 for baseband
output.
"""
...
...
...
...
"""PM modulator: instantaneous phase φ = kp · x[n].
*kp_rad_per_unit* maps ±1.0 audio to ±kp radians of carrier phase.
Set *rf_hz* to 0.0 for baseband output.
"""
...
...
"""Update the phase sensitivity (rad per unit input amplitude)."""
...
...
"""SSB phasing modulator (Weaver-style IIR variant).
Audio is up-converted to *audio_if_hz* via a complex rotator, split
into I and Q paths through matched 4th-order IIR low-pass filters,
then combined to select the desired sideband. Set *usb=True* for
upper sideband, *usb=False* for lower sideband. Set *rf_hz* to 0.0
for baseband IQ output.
"""
...
...
# ---------------------------------------------------------------------------
# Digital modulators (bits uint8 → IQ complex64)
# ---------------------------------------------------------------------------
"""BPSK modulator: Gray-coded constellation mapper + waveform stage.
Input: uint8 bit array (LSB of each byte used), one bit per symbol.
Output: complex64 IQ array of the same length.
Constellation: bit 0 → (+1, 0), bit 1 → (−1, 0).
Set *rf_hz* to 0.0 for baseband output; non-zero upconverts via an
internal ``Rotator`` (phasor recurrence, no per-sample trig).
"""
...
...
...
"""QPSK modulator: Gray-coded constellation mapper + waveform stage.
Input: uint8 bit array (LSB of each byte); consumed in pairs ``[b0, b1]``.
Output: complex64 IQ array of length ``len(bits) // 2``.
Constellation is normalized to unit energy (each axis ±1/√2).
Set *rf_hz* to 0.0 for baseband output.
"""
...
...
...
"""Square QAM modulator: Gray-coded constellation mapper + waveform stage.
*order* must be 16, 64, or 256 (raises ``ValueError`` otherwise).
Input: uint8 bit array (LSB of each byte); consumed ``log2(order)`` bytes
per symbol. Output: complex64 IQ array of length
``len(bits) // log2(order)``.
Constellation is Gray-coded on each axis independently and normalized to
unit average symbol energy. Set *rf_hz* to 0.0 for baseband output.
"""
...
...
...
# ---------------------------------------------------------------------------
# FT8/FT4 waveform classes
# ---------------------------------------------------------------------------
"""FT8 frame modulator: 8-FSK CPFSK, 79 symbols, 151 680 samples at 12 kHz.
Input: uint8 array of 58 tone indices (0–7).
Output: complex64 IQ array of shape (151680,).
"""
...
...
"""FT8 frame demodulator: Goertzel/dot-product tone detector.
Input: complex64 IQ array of at least 151 680 samples.
Output: uint8 array of 58 tone indices.
Raises ``ValueError`` if input is too short or demodulation fails.
"""
...
...
"""FT8 channel codec: CRC-14 + LDPC(174,91) + Gray code.
All methods are static; no per-instance state.
"""
...
"""Encode a 10-byte payload → uint8[58] Gray-coded tone indices."""
...
"""Hard-decision decode 58 tone indices → bytes[10], or None on failure."""
...
"""Soft-decision decode float32[174] LLRs → bytes[10], or None on failure."""
...
"""FT4 frame modulator: 4-FSK CPFSK, 105 symbols, 60 480 samples at 12 kHz.
Input: uint8 array of 87 tone indices (0–3).
Output: complex64 IQ array of shape (60480,).
"""
...
...
"""FT4 frame demodulator: Goertzel/dot-product tone detector.
Input: complex64 IQ array of at least 60 480 samples.
Output: uint8 array of 87 tone indices.
Raises ``ValueError`` if input is too short or demodulation fails.
"""
...
...
"""FT4 channel codec: XOR scramble + CRC-14 + LDPC(174,91) + Gray code.
All methods are static; no per-instance state.
"""
...
"""Encode a 10-byte payload → uint8[87] Gray-coded tone indices."""
...
"""Hard-decision decode 87 tone indices → bytes[10], or None on failure."""
...
"""Soft-decision decode float32[174] LLRs → bytes[10], or None on failure."""
...
# ---------------------------------------------------------------------------
# FT8/FT4 sync functions
# ---------------------------------------------------------------------------
"""Synchronise an FT8 IQ buffer and return up to *max_cand* frame candidates.
Each candidate is a dict::
{
"time_sym": int, # symbol offset of frame start
"freq_bin": int, # frequency bin of tone-0
"score": float, # Costas match score
"llr": float32[174], # soft LLRs for Ft8Codec.decode_soft
}
Pass each result's ``"llr"`` to ``Ft8Codec.decode_soft`` to recover the
77-bit payload.
"""
...
"""Synchronise an FT4 IQ buffer and return up to *max_cand* frame candidates.
Same return shape as ``ft8_sync``.
"""
...
# ---------------------------------------------------------------------------
# FT8/FT4 message packing functions
# ---------------------------------------------------------------------------
"""Pack a standard FT8/FT4 message → bytes[10].
*extra* may be a Maidenhead grid (``"FN31"``), signal report (``"+07"``,
``"-12"``), R-prefixed report (``"R+05"``), or token
(``"RRR"``, ``"RR73"``, ``"73"``). Pass ``""`` for no extra field.
Raises ``ValueError`` if the callsigns cannot be encoded.
"""
...
"""Pack a free-text FT8/FT4 message (up to 13 chars) → bytes[10].
Raises ``ValueError`` if the text is too long or contains invalid characters.
"""
...
"""Pack a telemetry FT8/FT4 message (exactly 9 bytes) → bytes[10].
Raises ``ValueError`` if *data* is not exactly 9 bytes.
"""
...
"""Unpack a 10-byte FT8/FT4 payload → dict.
The ``"type"`` key indicates the message type:
* ``"standard"`` — ``{"type", "call_to", "call_de", "extra"}``
* ``"free_text"`` — ``{"type", "text"}``
* ``"telemetry"`` — ``{"type", "data"}`` (bytes[9])
* ``"nonstd"`` — ``{"type", "call_to", "call_de", "extra"}``
* ``"unknown"`` — ``{"type", "payload"}`` (bytes[10])
Raises ``ValueError`` if *payload* is not exactly 10 bytes.
"""
...
# ---------------------------------------------------------------------------
# PSK31 codec classes
# ---------------------------------------------------------------------------
"""PSK31 Varicode encoder: push bytes, drain bit stream."""
...
"""Append *n* zero bits as preamble."""
...
"""Encode byte *b* and append its Varicode bits."""
...
"""Append *n* zero bits as postamble."""
...
"""Drain all pending bits into a uint8 array."""
...
...
"""PSK31 Varicode decoder: push bits, pop decoded bytes."""
...
"""Feed a uint8 array of bits (0/1) into the decoder."""
...
"""Drain all decoded bytes."""
...
# ---------------------------------------------------------------------------
# PSK31 modulators / demodulators
# ---------------------------------------------------------------------------
"""BPSK31 modulator: differential phase encoding with Hann pulse shaping."""
...
...
...
"""Encode text via Varicode and modulate to IQ."""
...
"""Modulate raw differential bits to IQ."""
...
"""BPSK31 demodulator: matched-filter symbol detection."""
...
...
...
"""Demodulate IQ to soft bits (one float per symbol)."""
...
"""BPSK31 hard-decision slicer: threshold soft bits at 0."""
...
"""Threshold soft bits to hard decisions."""
...
"""QPSK31 modulator: convolutional encoding + DQPSK + Hann pulse shaping."""
...
...
...
"""Encode text via Varicode, convolutional-encode, and modulate to IQ."""
...
"""Modulate raw bits (convolutional encoding + DQPSK) to IQ."""
...
"""QPSK31 demodulator with integrated Viterbi decider.
Call ``process()`` to feed IQ samples (returns soft dibits for inspection),
then ``flush()`` to run Viterbi and get decoded bits.
"""
...
...
...
"""Demodulate IQ to soft dibits (interleaved Re/Im pairs)."""
...
"""Run Viterbi on accumulated dibits and return decoded bits."""
...
# ---------------------------------------------------------------------------
# PSK31 streaming decoder
# ---------------------------------------------------------------------------
"""Streaming PSK31 decoder: demod → decider/Viterbi → Varicode in one step.
Use ``mode="bpsk"`` for BPSK31 or ``mode="qpsk"`` for QPSK31.
"""
...
"""Feed IQ samples and return any newly decoded text."""
...
"""Flush the decoder and return any remaining text."""
...
# ---------------------------------------------------------------------------
# PSK31 sync functions
# ---------------------------------------------------------------------------
"""Scan for PSK31 carriers in an IQ buffer.
Returns a list of candidate dicts::
{
"time_sym": int,
"freq_bin": int,
"carrier_hz": float,
"score": float,
"soft_bits": float32[N],
}
"""
...
"""Pick the best PSK31 sync result nearest to *carrier_hz*.
Takes the list returned by ``psk31_sync()`` and returns the best
candidate dict, or ``None`` if no candidate is within 2×baud.
"""
...
# ---------------------------------------------------------------------------
# OFDM
# ---------------------------------------------------------------------------
"""OFDM waveform configuration: carrier plan + RF/constellation parameters.
*data_carriers* and *pilot_carrier_indices* use the signed carrier-index
convention (bin 0 = DC; e.g. ``-26..=26``). *pilot_carrier_indices* and
*pilot_carrier_values* are parallel arrays of the same length — pass
empty arrays for no pilots. *constellation* is one of ``"bpsk"``,
``"qpsk"``, ``"qam16"``, ``"qam64"``, ``"qam256"``.
Raises ``ValueError`` for an unknown constellation or an invalid carrier
plan (overlapping data/pilot carriers, out-of-range indices, or an empty
data set).
"""
...
...
...
"""OFDM transmitter: fused mapper + resource-grid mapping + IFFT + cyclic
prefix + optional RF upconversion.
Input: uint8 array of bits (LSB of each byte); consumed
``bits_per_ofdm_symbol`` at a time, zero-padding a final partial symbol.
Output: complex64 IQ array, ``samples_per_ofdm_symbol`` samples/symbol.
"""
...
...
"""OFDM receiver: fused cyclic-prefix removal + FFT + channel
equalization + resource-grid extraction + hard-decision decoding.
*equalizer* selects the channel-estimation method:
* ``"training_symbol"`` (the default) — one estimate per packet, held
constant. Call ``estimate_channel()`` once with a demodulated training
symbol before the first ``demodulate()``/``demodulate_soft()`` call.
* ``"pilot_interp"`` — re-estimated every symbol from in-band pilots
(frequency-domain linear interpolation); no separate estimation call
needed.
Input: complex64 IQ array, ``samples_per_ofdm_symbol`` samples/symbol.
Output: uint8 array of bits, ``bits_per_ofdm_symbol`` per symbol.
Raises ``ValueError`` if input is shorter than one OFDM symbol.
"""
...
"""Estimate and hold the channel from a demodulated training symbol.
Only meaningful for the ``"training_symbol"`` equalizer; a no-op
under ``"pilot_interp"``.
"""
...
...
"""Like ``demodulate()``, but also returns the pre-decision soft
symbols (post-equalization, post-grid-extract) as
``(soft_symbols, bits)``, for building an ``OfdmRxFrame`` via
``build_ofdm_rx_frame``.
"""
...
"""Per-packet OFDM RX diagnostics.
Fields that require acquisition or equalization stay ``None`` until the
caller has actually run those stages.
"""
...
...
...
...
...
...
"""Build an ``OfdmRxFrame`` from demodulated soft symbols and their
corresponding hard-decided bits (as returned by
``OfdmDemod.demodulate_soft``, concatenated across all symbols in the
packet). ``evm_db`` is always populated; ``cfo_hz``,
``timing_offset_samples``, and ``channel_mse`` require acquisition/
equalization info not carried by this function alone and are ``None``.
"""
...
"""Search an OFDM IQ buffer for a repeated-segment (Schmidl & Cox-style)
preamble match.
Pass *training_n_fft*/*training_cp_len* to enable wide-range integer-CFO
recovery via a dedicated training symbol (must match the values used
with ``generate_ofdm_preamble``); omit both for fractional-CFO-only
acquisition (capture range ±½ the subcarrier spacing).
Returns a list of dicts, sorted by descending score::
{
"start_sample": int, # sample offset of the preamble start
"cfo_hz": float, # fractional CFO estimate (Hz)
"integer_cfo_bins": int, # whole subcarrier-spacing units
"score": float, # normalized timing-metric score
}
Total CFO is ``cfo_hz + integer_cfo_bins * (fs / n_fft)``.
``integer_cfo_bins`` is always 0 unless a training symbol was supplied.
"""
...
"""Generate a repeated-segment preamble (plus training symbol, if
*training_n_fft*/*training_cp_len* are given) for prepending before OFDM
data symbols. See ``ofdm_sync`` for the matching acquisition function.
"""
...