- OCaml 100%
rtlsdr #
RTL-SDR IQ sample reader for OCaml.
An RTL-SDR is a cheap USB TV dongle that works as a software-defined radio
receiver. Tools such as rtl_sdr record what it hears as a raw file of
interleaved unsigned 8-bit I and Q samples. Each IQ sample is one complex
value of the signal after the receiver has shifted it down by the centre
frequency, with I the in-phase (real) part and Q the quadrature (imaginary)
part. rtl_sdr writes each part as an unsigned byte where 127.5 stands for
zero, and rtlsdr maps a byte v to (v − 127.5) / 127.5, which lies in
[−1, 1].
rtlsdr reads these files, or the same bytes already in memory, as complex
float samples for the dsp and demod packages. A raw file does not record
its centre frequency, sample rate or gain, so the caller attaches them. To
test a demodulation pipeline without hardware, the library can also generate
pure tones and BPSK signals. BPSK (binary phase-shift keying) sends one bit
per symbol by turning the carrier's phase through 180 degrees. The library
never talks to the dongle itself.
Installation #
Install with opam:
$ opam install rtlsdr
If opam cannot find the package, it has probably not reached the public
opam-repository yet. Add the overlay repository and install from there:
$ opam repo add samoht https://tangled.org/gazagnaire.org/opam-overlay.git
$ opam update
$ opam install rtlsdr
Usage #
(* Read IQ samples from a raw capture file. *)
let read_capture path =
let src = Rtlsdr.of_file path in
Fmt.pr "%d samples available@." (Rtlsdr.sample_count src);
let samples = Rtlsdr.read src 4096 in
Rtlsdr.close src;
samples
(* Attach recording metadata. *)
let with_meta path =
Rtlsdr.of_file path
|> Rtlsdr.with_metadata
{ center_freq = 137.5e6; sample_rate = 2.4e6; gain = 40.0 }
(* Generate a synthetic BPSK signal for testing. *)
let bits = Bytes.of_string "\x01\x00\x01\x01\x00"
let iq =
Rtlsdr.generate_bpsk ~sample_rate:48000.0 ~symbol_rate:1200.0 ~bits
~carrier_freq:1800.0
Licence #
ISC. See LICENSE.md.