Sample Formats¶
doppler.cvt converts sample streams between float32 and the
fixed-point / integer formats used at the edges of a DSP chain — ADC codes, Q15
fractions, Q15 in a wide integer word — plus an ideal-quantiser ADC
model for characterisation. Every converter is a tiny stateful object with a
scale (or ADC depth) fixed at construction; steps() runs a whole block
(optionally in-place via out=), step() does one sample.
For the F32To* direction, scale is the gain applied before quantising
(code = round(x · scale)); the default scale = 32768 maps the normalised
range [-1, +1) onto full-scale int16 and saturates beyond it. The *ToF32
direction divides by the same scale to recover the float.
Source:
src/doppler/cvt/__init__.py
For the quantisation theory behind the scaling and the UQ15 unsigned format,
see the Quantization gallery page.
Converter map¶
| Class | In → Out | Use |
|---|---|---|
F32ToI8 / I8ToF32 |
float32 ↔ int8 | signed 8-bit IQ round-trip (HackRF, ci8) |
U8ToF32 |
uint8 → float32 | unsigned 8-bit IQ in (RTL-SDR cu8) |
F32ToI16 / I16ToF32 |
float32 ↔ int16 | signed Q15 / 16-bit PCM round-trip |
F32ToI32 / I32ToF32 |
float32 ↔ int32 | 24/32-bit ADC codes round-trip |
F32ToUQ15 / UQ15ToF32 |
float32 ↔ uint16 | unsigned Q15 (offset-binary) round-trip |
F32ToI16U32 / I16U32ToF32 |
float32 ↔ uint32 | one Q15 in the low 16 bits (wide integer) |
F32ToI16U64 / I16U64ToF32 |
float32 ↔ uint64 | one Q15 in the low 16 bits (wide integer) |
ADC |
float32 → int64 | ideal bits-bit quantiser (codes) |
The F32To* directions expose a clipped flag that latches when an input
exceeded full scale during the last steps().
Every converter's default scale is 2^(N-1) — 128, 32768, 2147483648 —
in both directions, which is the code grid the hardware actually has: one LSB
is 1/2^(N-1), so a dyadic value round-trips exactly. The consequence worth
knowing is at the top of the range: +1.0 maps to 2^(N-1), one code past
the type's maximum, and saturates to it (latching clipped), while -1.0 is
exact. That asymmetry belongs to two's complement, not to the scaling, and
saturating is what makes it safe rather than wrapping to the opposite rail.
doppler.wfm's file and stream writers quantise through these converters, so
a capture's bytes are exactly what F32ToI16().steps() would produce.
Examples¶
float32 ↔ int8 and int32¶
The 8- and 32-bit pairs work exactly like the int16 one; only the default scale differs.
import numpy as np
from doppler.cvt import F32ToI8, F32ToI32, I8ToF32
x = np.array([0.5, -1.0, 1.0], dtype=np.float32)
F32ToI8().steps(x) # array([ 64, -128, 127], dtype=int8)
F32ToI32().steps(x) # array([1073741824, -2147483648, 2147483647])
I8ToF32().steps(np.array([64, -128], dtype=np.int8)) # [0.5, -1.0]
Note +1.0 saturating to 127 / 2147483647 while -1.0 reaches -128 /
-2147483648 exactly — the asymmetry described above.
RTL-SDR cu8 (unsigned 8-bit I/Q)¶
An RTL-SDR streams unsigned, offset-binary bytes centred on 127.5 — not
the signed int8 I8ToF32 reads. U8ToF32 converts the flat interleaved
buffer; view the result as complex64 to get the I/Q samples. The default
mode="shift" is exact and fast and reads 0.5/128 low (a DC term a
down-converter tuned off DC filters out); mode="midpoint" is the unbiased,
symmetric mapping for when DC matters.
from doppler.cvt import U8ToF32
cu8 = np.array([128, 0, 192, 64], dtype=np.uint8) # two I/Q pairs
U8ToF32().steps(cu8).view(np.complex64) # [0-1j, 0.5-0.5j]
U8ToF32(mode="midpoint").steps(np.array([0, 255], dtype=np.uint8)) # [-1, 1]
float32 ↔ int16 round-trip¶
The default scale = 32768 maps [-1, +1) to full-scale int16.
import numpy as np
from doppler.cvt import F32ToI16, I16ToF32
x = np.array([0.0, 0.5, -1.0, 0.999], dtype=np.float32)
enc = F32ToI16() # default scale = 32768
codes = enc.steps(x) # array([0, 16384, -32768, 32735], dtype=int16)
if enc.clipped: # latches when an input exceeded full scale
print("input exceeded full scale")
dec = I16ToF32() # default scale = 32768
back = dec.steps(codes) # ~= x ([0.0, 0.5, -1.0, 0.999])
In-place block conversion¶
Pass a pre-allocated out= to avoid an allocation in a hot loop.
Ideal ADC quantiser (characterisation)¶
ADC(bits, dbfs, dithering) models an ideal converter: a full-scale sine at
dbfs=0.0 spans ±2**(bits-1) codes. Feed its output straight into
ToneMeasure to recover ENOB ≈ bits.
from doppler.cvt import ADC
adc = ADC(12, 0.0, 0) # 12-bit, 0 dBFS, no dither
x = np.array([0.0, 0.5, 0.999, -1.0], dtype=np.float32)
adc.steps(x) # array([0, 1024, 2046, -2048])
Unsigned Q15 (offset binary)¶
F32ToUQ15 maps [-1, 1) onto uint16 centred at 32768 (offset binary), the
convention used by many unsigned ADCs.
from doppler.cvt import F32ToUQ15, UQ15ToF32
enc = F32ToUQ15() # default scale = 32768
u = enc.steps(np.array([-1.0, 0.0, 0.999], dtype=np.float32)) # 0, 32768, 65503
back = UQ15ToF32().steps(u) # ~= input
Q15 in a wide word¶
These pack a single saturated Q15 into the low 16 bits of a uint32/uint64
(upper bits zero), leaving the upper bits as headroom for an integer
accumulator downstream. The CIC filter does not take this format: it accepts
complex64 and encodes to offset-binary UQ16 internally.
Bits, values and symbols¶
The conversions a frame field is built from. int_to_bin is the one to reach
for first — a literal that fits in 64 bits, expanded exactly, with no failure
mode a typo can reach. hex_to_bin is for what it cannot serve: a literal
wider than 64 bits, or one arriving as text from a CLI flag or a JSON record.
Bit order follows numpy's bitorder (0 big, 1 little; big is "as
written"). It is a different axis from the endian a BLUE file takes,
which selects byte order — the EEEI/IEEE header field. A literal's digit
order already fixes which byte comes first; what is left to choose is the
order of bits inside one.
bytes_to_bin is for packed data: a binary file, a byte stream, a
network buffer. Every frame field takes unpacked bits, so packed octets go
through this, by name, rather than being passed as bytes and hoped about.
The same octets unpack exactly as their hex spelling does through
hex_to_bin.
bin_to_nrz maps bits to bipolar symbols as 1 - 2*b — bit 0 to +1, bit 1
to -1. That convention's home is BPSK in
mpsk: M-PSK at m = 2 puts label 0 at +1. The two are
asserted equal in the C tests rather than trusted to stay equal, because a
mapper that disagreed with the receiver's would decode every bit inverted
while looking perfectly locked.