BPSK/QPSK demodulation with carrier and timing recovery
README.md

demod #

BPSK and QPSK demodulation with carrier and timing recovery.

Overview #

A software radio receiving a satellite downlink ends up with a stream of complex baseband (IQ) samples. The receiver mixes the radio signal down to around 0 Hz and samples two components, in-phase (I) and quadrature (Q), so each sample is a complex number that gives the signal's amplitude and phase. BPSK sends one bit per symbol as a phase of 0 or 180 degrees; QPSK sends two bits per symbol as one of four phases 90 degrees apart. A residual frequency offset makes these phases rotate, so turning the samples back into bits means correcting the carrier's frequency and phase offset and finding where each symbol starts. demod offers two receivers for that. The tracking receiver pairs a Costas loop for carrier recovery with Mueller-Muller timing recovery and runs continuously. loop_bw is the loop bandwidth normalised to the sample rate (typical values lie between 0.01 and 0.1), and the loop needs on the order of 1 / loop_bw samples to converge: about 20 samples for the example's 0.05, or 5 symbols at 4 samples per symbol. That figure is an order of magnitude from control theory, not a measured bound. The loop loses lock on every dropout.

The feed-forward receiver estimates frequency (Luise and Reggiannini, 1995) and phase (Viterbi and Viterbi, 1983) once per burst and carries no state between bursts, so a link that keeps fading, such as a tumbling spacecraft's antenna sweeping its null past the ground station, still demodulates the next packet. Around both sit frame synchronisation and a spectrogram. A soft symbol is the detector's real-valued output before the bit decision, such as the I value of a BPSK symbol. Plain correlation locates a sync word s in a span of soft symbols ρ by picking the offset that maximises Σ sᵢρᵢ. Massey's optimum rule (IEEE Trans. Commun. COM-20(2), 1972, eqs. 5 and 6) subtracts from each term a correction f(ρᵢ) that depends on the magnitude of the received value and on the channel's signal-to-noise ratio, which accounts for the random data around the sync word and gains about 3 dB in sync-acquisition error probability. The spectrogram is for reading a carrier offset by hand when the automatic estimator cannot reach it.

The package builds on dsp for complex arithmetic and filtering.

Installation #

Install with opam:

$ opam install demod

If opam cannot find the package, it may not yet be released in the public opam-repository. Add the overlay repository, then install it:

$ opam repo add samoht https://tangled.org/gazagnaire.org/opam-overlay.git
$ opam update
$ opam install demod

Usage #

(* Demodulate a BPSK recording. *)
let demod_file path =
  let samples = Dsp.Iq.of_file path in
  let demod = Demod.bpsk_create ~samples_per_symbol:4 ~loop_bw:0.05 in
  Demod.bpsk_demod demod samples

(* Or use the building blocks individually. *)
let demod_step ~costas ~timing sample =
  let corrected = Demod.costas_step costas sample in
  match Demod.timing_step timing corrected with
  | Some _symbol -> `Symbol
  | None -> `Pending

let costas = Demod.costas_create ~order:2 ~loop_bw:0.03
let timing = Demod.timing_create ~samples_per_symbol:4 ~loop_bw:0.02

API #

  • costas_create and costas_step: Costas loop carrier recovery, order 2 for BPSK and 4 for QPSK.
  • timing_create and timing_step: Mueller-Muller timing recovery.
  • bpsk_create and bpsk_demod: the tracking BPSK demodulator, from IQ samples to packed bits.
  • qpsk_create and qpsk_demod: the tracking QPSK demodulator, from IQ samples to packed dibits.
  • feedforward_freq_estimate, feedforward_phase_estimate and feedforward_bpsk_demod: burst-mode BPSK demodulation with no tracking loop.
  • sync_locate, sync_locate_psk and sync_locate_correlation: frame synchronisation by Massey's rule, its PSK variant and plain correlation.
  • spectrogram and hann_window: a windowed DFT for reading a carrier offset off a peak.

References #

  • J. P. Costas, "Synchronous Communications", Proceedings of the IRE, 1956. The original Costas loop.
  • K. Mueller and M. Muller, "Timing Recovery in Digital Synchronous Data Receivers", IEEE Transactions on Communications, 1976. The timing error detector.
  • J. L. Massey, "Optimum Frame Synchronization", IEEE Transactions on Communications, 1972. The frame synchronisation rule.
  • M. Luise and R. Reggiannini, "Carrier Frequency Recovery in All-Digital Modems for Burst-Mode Transmissions", IEEE Transactions on Communications, 1995. The feed-forward frequency estimator.
  • A. J. Viterbi and A. M. Viterbi, "Nonlinear Estimation of PSK-Modulated Carrier Phase with Application to Burst Digital Transmission", IEEE Transactions on Communications, 1983. The feed-forward phase estimator.

Licence #

ISC License. See LICENSE.md for details.