undervoice 0.1.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
undervoice/__init__.py ADDED
@@ -0,0 +1,31 @@
1
+ """A slow data channel hidden under FM voice.
2
+
3
+ Basic use::
4
+
5
+ from undervoice import Message, decode, encode, read_wav
6
+
7
+ speech = read_wav("speech.wav")
8
+ mixed = encode(speech, Message("W1AW", "FN31pr", "ON THE AIR"))
9
+ for frame in decode(mixed):
10
+ print(frame.message)
11
+ """
12
+
13
+ from ._audio import read_wav, write_wav
14
+ from ._channel import fm_channel
15
+ from ._demodulator import Frame, decode
16
+ from ._message import Message
17
+ from ._modulator import Modulator, encode
18
+ from ._waveform import FRAME, RATE
19
+
20
+ __all__ = [
21
+ "FRAME",
22
+ "RATE",
23
+ "Frame",
24
+ "Message",
25
+ "Modulator",
26
+ "decode",
27
+ "encode",
28
+ "fm_channel",
29
+ "read_wav",
30
+ "write_wav",
31
+ ]
undervoice/__main__.py ADDED
@@ -0,0 +1,5 @@
1
+ import sys
2
+
3
+ from ._cli import main
4
+
5
+ sys.exit(main())
undervoice/_audio.py ADDED
@@ -0,0 +1,81 @@
1
+ """WAV files and sample rate conversion."""
2
+
3
+ from __future__ import annotations
4
+
5
+ from math import gcd
6
+ from pathlib import Path
7
+
8
+ import numpy as np
9
+ from scipy.io import wavfile
10
+ from scipy.signal import firwin, lfilter, resample_poly
11
+
12
+ from ._waveform import RATE
13
+
14
+ TAPS = 16
15
+
16
+
17
+ def read_wav(path: str | Path) -> np.ndarray:
18
+ """Read a WAV file as mono float audio at RATE."""
19
+ rate, x = wavfile.read(path)
20
+ if x.dtype == np.uint8:
21
+ x = (x.astype(np.float64) - 128) / 128
22
+ elif np.issubdtype(x.dtype, np.integer):
23
+ x = x / float(np.iinfo(x.dtype).max + 1)
24
+ x = np.asarray(x, np.float64)
25
+ if x.ndim == 2:
26
+ x = x.mean(axis=1)
27
+ if rate != RATE:
28
+ g = gcd(rate, RATE)
29
+ x = resample_poly(x, RATE // g, rate // g)
30
+ return x
31
+
32
+
33
+ def to_int16(x: np.ndarray) -> np.ndarray:
34
+ return (np.clip(x, -1.0, 1.0) * 32767).astype(np.int16)
35
+
36
+
37
+ def write_wav(path: str | Path, x: np.ndarray) -> None:
38
+ """Write 16-bit mono audio at RATE."""
39
+ wavfile.write(path, RATE, to_int16(x))
40
+
41
+
42
+ def resample_at(x: np.ndarray, t: np.ndarray) -> np.ndarray:
43
+ """Values of x at fractional sample times t, by windowed sinc interpolation."""
44
+ padded = np.concatenate([np.zeros(TAPS), x, np.zeros(TAPS + 1)])
45
+ k = np.arange(-TAPS + 1, TAPS + 1)
46
+ out = np.empty(len(t))
47
+ # chunked so a long file does not need a len(t) x 32 array all at once
48
+ for start in range(0, len(t), 65536):
49
+ tt = t[start : start + 65536]
50
+ base = np.floor(tt).astype(np.int64)
51
+ d = k - (tt - base)[:, None]
52
+ w = np.sinc(d) * (0.5 + 0.5 * np.cos(np.pi * d / TAPS))
53
+ idx = np.clip(base[:, None] + k + TAPS, 0, len(padded) - 1)
54
+ out[start : start + len(tt)] = np.sum(padded[idx] * w, axis=1)
55
+ return out
56
+
57
+
58
+ class Decimator:
59
+ """48 kHz to RATE, block by block, for blocks that are a multiple of 6 samples."""
60
+
61
+ def __init__(self) -> None:
62
+ self.h = firwin(121, 3600, fs=6 * RATE)
63
+ self.zi = np.zeros(len(self.h) - 1)
64
+
65
+ def __call__(self, x: np.ndarray) -> np.ndarray:
66
+ y, self.zi = lfilter(self.h, 1.0, x, zi=self.zi)
67
+ return y[::6]
68
+
69
+
70
+ class Interpolator:
71
+ """RATE to 48 kHz, block by block."""
72
+
73
+ def __init__(self) -> None:
74
+ self.h = 6 * firwin(121, 3600, fs=6 * RATE)
75
+ self.zi = np.zeros(len(self.h) - 1)
76
+
77
+ def __call__(self, x: np.ndarray) -> np.ndarray:
78
+ up = np.zeros(6 * len(x))
79
+ up[::6] = x
80
+ y, self.zi = lfilter(self.h, 1.0, up, zi=self.zi)
81
+ return y
undervoice/_channel.py ADDED
@@ -0,0 +1,62 @@
1
+ """A simulated narrowband FM voice path: radio audio filters, emphasis, limiter and noise."""
2
+
3
+ from __future__ import annotations
4
+
5
+ import numpy as np
6
+ from scipy.signal import bilinear, butter, lfilter, sosfilt
7
+
8
+ from ._audio import resample_at
9
+ from ._waveform import RATE
10
+
11
+ TX_HPF = butter(4, 300, "highpass", fs=RATE, output="sos")
12
+ SPLATTER = butter(6, 3000, "lowpass", fs=RATE, output="sos")
13
+ RX_HPF = butter(4, 300, "highpass", fs=RATE, output="sos")
14
+ RX_LPF = butter(4, 3000, "lowpass", fs=RATE, output="sos")
15
+ # 750 us emphasis; the second pole sits far up so it only keeps the digital filter proper
16
+ PRE = bilinear([750e-6, 1.0], [1 / (2 * np.pi * 20000), 1.0], fs=RATE)
17
+ DE = (PRE[1], PRE[0])
18
+
19
+
20
+ def _transmit(x: np.ndarray, drive_db: float) -> np.ndarray:
21
+ x = lfilter(*PRE, sosfilt(TX_HPF, x))
22
+ # mic gain set so speech peaks run drive_db into the deviation limiter
23
+ peak = np.percentile(np.abs(x), 99.9)
24
+ x = np.clip(x * 10 ** (drive_db / 20) / max(peak, 1e-12), -1.0, 1.0)
25
+ return sosfilt(SPLATTER, x)
26
+
27
+
28
+ def _receive(x: np.ndarray) -> np.ndarray:
29
+ return sosfilt(RX_LPF, sosfilt(RX_HPF, lfilter(*DE, x)))
30
+
31
+
32
+ def _noise(n: int, snr_db: float, rng: np.random.Generator) -> np.ndarray:
33
+ # discriminator noise rises with frequency; scale it so a full-deviation 1 kHz tone at
34
+ # the receiver output sits snr_db above it
35
+ noise = np.diff(rng.standard_normal(n + 1))
36
+ tone = _receive(np.sin(2 * np.pi * 1000 * np.arange(RATE) / RATE))[RATE // 2 :]
37
+ out = _receive(noise)
38
+ scale = np.sqrt(np.mean(tone**2) / np.mean(out**2) / 10 ** (snr_db / 10))
39
+ return scale * noise
40
+
41
+
42
+ def fm_channel(
43
+ audio: np.ndarray,
44
+ snr_db: float = 30.0,
45
+ drive_db: float = 3.0,
46
+ hops: int = 1,
47
+ ppm: float = 0.0,
48
+ rng: np.random.Generator | None = None,
49
+ ) -> np.ndarray:
50
+ """Pass audio through `hops` FM links (1 = simplex, 2 = through a repeater).
51
+
52
+ ppm offsets the receiving sound card's clock from the sending one.
53
+ """
54
+ rng = rng or np.random.default_rng()
55
+ x = audio
56
+ for _ in range(hops):
57
+ sent = _transmit(x, drive_db)
58
+ x = _receive(sent + _noise(len(sent), snr_db, rng))
59
+ if ppm:
60
+ step = 1 + ppm * 1e-6
61
+ x = resample_at(x, np.arange(int(len(x) / step)) * step)
62
+ return 0.5 * x / max(float(np.max(np.abs(x))), 1e-12)
undervoice/_cli.py ADDED
@@ -0,0 +1,180 @@
1
+ """Hide a slow data channel under FM voice: encode, decode, simulate and run live."""
2
+
3
+ from __future__ import annotations
4
+
5
+ import argparse
6
+ import glob
7
+ import sys
8
+
9
+ import numpy as np
10
+
11
+ from . import _live
12
+ from ._audio import read_wav, write_wav
13
+ from ._channel import fm_channel
14
+ from ._demodulator import decode
15
+ from ._message import Message
16
+ from ._modulator import LEVEL_DB, SHAPE, encode
17
+ from ._sweep import sweep
18
+ from ._waveform import RATE
19
+
20
+
21
+ def _device(value: str) -> int | str:
22
+ return int(value) if value.isdigit() else value
23
+
24
+
25
+ def shape_value(value: str) -> float:
26
+ shape = float(value)
27
+ if not 0.0 <= shape < 1.0:
28
+ raise argparse.ArgumentTypeError(
29
+ f"shape must be from 0 up to (not including) 1, got {value}"
30
+ )
31
+ return shape
32
+
33
+
34
+ def level_list(value: str) -> list[float]:
35
+ return [float(v) for v in value.split(",")]
36
+
37
+
38
+ def _message(args: argparse.Namespace) -> Message:
39
+ try:
40
+ return Message(args.call, args.grid, args.status)
41
+ except ValueError as e:
42
+ sys.exit(str(e))
43
+
44
+
45
+ def _add_message(p: argparse.ArgumentParser) -> None:
46
+ p.add_argument("--call", required=True, help="callsign, up to 10 characters")
47
+ p.add_argument("--grid", default="", help="4 or 6 character Maidenhead locator")
48
+ p.add_argument("--status", default="", help="up to 32 characters of A-Z 0-9 / - . and space")
49
+
50
+
51
+ def _add_tuning(p: argparse.ArgumentParser, level: bool = True) -> None:
52
+ if level:
53
+ p.add_argument(
54
+ "--level",
55
+ type=float,
56
+ default=LEVEL_DB,
57
+ help=f"data level in dB relative to the speech (default {LEVEL_DB})",
58
+ )
59
+ p.add_argument(
60
+ "--shape",
61
+ type=shape_value,
62
+ default=SHAPE,
63
+ help=f"how closely the data follows the speech spectrum, 0 to below 1 "
64
+ f"(default {SHAPE}); transmitter and receiver must match",
65
+ )
66
+
67
+
68
+ def _parser() -> argparse.ArgumentParser:
69
+ parser = argparse.ArgumentParser(prog="undervoice", description=__doc__)
70
+ sub = parser.add_subparsers(dest="command", required=True)
71
+
72
+ p = sub.add_parser("encode", help="mix data under the speech in a WAV file")
73
+ p.add_argument("input")
74
+ p.add_argument("output")
75
+ _add_message(p)
76
+ _add_tuning(p)
77
+
78
+ p = sub.add_parser("decode", help="print the frames found in a WAV file")
79
+ p.add_argument("input")
80
+ _add_tuning(p, level=False)
81
+
82
+ p = sub.add_parser("channel", help="pass a WAV file through the simulated FM channel")
83
+ p.add_argument("input")
84
+ p.add_argument("output")
85
+ p.add_argument(
86
+ "--snr",
87
+ type=float,
88
+ default=40.0,
89
+ help="full-deviation 1 kHz tone to noise ratio at the receiver, dB (default 40)",
90
+ )
91
+ p.add_argument(
92
+ "--drive",
93
+ type=float,
94
+ default=3.0,
95
+ help="how far speech peaks push into the deviation limiter, dB (default 3)",
96
+ )
97
+ p.add_argument("--hops", type=int, default=1, help="1 for simplex, 2 through a repeater")
98
+ p.add_argument("--ppm", type=float, default=0.0, help="sound card clock offset")
99
+ p.add_argument("--seed", type=int)
100
+
101
+ p = sub.add_parser("sweep", help="frame loss against data level through the FM channel")
102
+ p.add_argument("speech", nargs="+", help="WAV files of speech")
103
+ p.add_argument("--levels", type=level_list, default=level_list("-36,-30,-24,-18,-12"))
104
+ p.add_argument("--trials", type=int, default=3)
105
+ p.add_argument("--snr", type=float, default=40.0)
106
+ p.add_argument("--hops", type=int, default=1)
107
+ p.add_argument("--ppm", type=float, default=0.0)
108
+ p.add_argument("--seed", type=int, default=0)
109
+ _add_tuning(p, level=False)
110
+
111
+ p = sub.add_parser("tx", help="send data under live mic audio, or under a WAV file")
112
+ _add_message(p)
113
+ _add_tuning(p)
114
+ p.add_argument("--play", metavar="WAV", help="play this recording instead of using the mic")
115
+ p.add_argument("--input", type=_device, help="mic device (number or name)")
116
+ p.add_argument("--output", type=_device, help="radio audio device (number or name)")
117
+
118
+ p = sub.add_parser("rx", help="listen on a sound card and print frames")
119
+ _add_tuning(p, level=False)
120
+ p.add_argument("--input", type=_device, help="radio audio device (number or name)")
121
+ p.add_argument("--record", metavar="WAV", help="also save the received audio to this file")
122
+
123
+ sub.add_parser("devices", help="list sound devices")
124
+ return parser
125
+
126
+
127
+ def main(argv: list[str] | None = None) -> int:
128
+ args = _parser().parse_args(argv)
129
+ try:
130
+ return _run(args)
131
+ except KeyboardInterrupt:
132
+ return 130
133
+
134
+
135
+ def _run(args: argparse.Namespace) -> int:
136
+ if args.command == "encode":
137
+ write_wav(args.output, encode(read_wav(args.input), _message(args), args.level, args.shape))
138
+ elif args.command == "decode":
139
+ frames = decode(read_wav(args.input), args.shape)
140
+ for f in frames:
141
+ print(f"{f.start / RATE:8.2f} s {f.message} (snr {f.snr_db:.1f} dB)")
142
+ if not frames:
143
+ print("no frames decoded")
144
+ return 1
145
+ elif args.command == "channel":
146
+ rng = np.random.default_rng(args.seed)
147
+ out = fm_channel(read_wav(args.input), args.snr, args.drive, args.hops, args.ppm, rng)
148
+ write_wav(args.output, out)
149
+ elif args.command == "sweep":
150
+ # PowerShell hands patterns like speech/*.wav over unexpanded
151
+ paths = [p for pattern in args.speech for p in sorted(glob.glob(pattern)) or [pattern]]
152
+ speech = [read_wav(path) for path in paths]
153
+ message = Message("N0CALL", "FN31pr", "SWEEP TEST 0123456789 ABCDEFGHIJ")
154
+ print(f"shape {args.shape}, snr {args.snr} dB, hops {args.hops}, ppm {args.ppm}")
155
+ print("level_db frames received loss_pct wrong")
156
+ for r in sweep(
157
+ speech,
158
+ args.levels,
159
+ message,
160
+ args.shape,
161
+ args.trials,
162
+ args.snr,
163
+ args.hops,
164
+ args.ppm,
165
+ args.seed,
166
+ ):
167
+ print(
168
+ f"{r.level_db:8.1f} {r.sent:6d} {r.received:8d} {100 * r.loss:8.1f} {r.wrong:5d}",
169
+ flush=True,
170
+ )
171
+ elif args.command == "tx":
172
+ if args.play:
173
+ _live.play(read_wav(args.play), _message(args), args.level, args.shape, args.output)
174
+ else:
175
+ _live.transmit(_message(args), args.level, args.shape, args.input, args.output)
176
+ elif args.command == "rx":
177
+ _live.receive(args.shape, args.input, args.record)
178
+ elif args.command == "devices":
179
+ _live.devices()
180
+ return 0
@@ -0,0 +1,154 @@
1
+ """Receive side: find frames in audio and pull the bits out from under the speech."""
2
+
3
+ from __future__ import annotations
4
+
5
+ from dataclasses import dataclass
6
+
7
+ import numpy as np
8
+ from scipy.ndimage import maximum_filter1d
9
+ from scipy.signal import correlate, fftconvolve, find_peaks, firwin, hilbert
10
+
11
+ from ._audio import resample_at
12
+ from ._fec import decode_bits
13
+ from ._lpc import HOP, match, whiten
14
+ from ._message import Message, unpack
15
+ from ._modulator import SHAPE
16
+ from ._waveform import FRAME, FRAME_SYMBOLS, PILOTS, RATE, SYMBOL, reference
17
+
18
+ BLOCKS = 12
19
+ BLOCK = FRAME // BLOCKS
20
+ MAX_PPM = 200
21
+ # a block only counts towards timing if its pilots stand this far above the noise
22
+ BLOCK_DETECT = 4.0
23
+ PILOT_MASK = np.repeat(PILOTS, SYMBOL)
24
+ EDGES = [300, 700, 1200, 1900, 3000]
25
+ SUBBANDS = [firwin(101, [lo, hi], pass_zero=False, fs=RATE) for lo, hi in zip(EDGES, EDGES[1:])]
26
+
27
+
28
+ @dataclass(frozen=True)
29
+ class Frame:
30
+ start: float
31
+ message: Message
32
+ snr_db: float
33
+
34
+
35
+ def _reach(block: int) -> int:
36
+ return int(np.ceil((block + 1) * BLOCK * MAX_PPM * 1e-6))
37
+
38
+
39
+ def _block_scores(xw: np.ndarray) -> list[np.ndarray]:
40
+ # per block of pilots, normalized correlation against every possible frame start
41
+ n = len(xw) - FRAME + 1
42
+ ref = hilbert(reference())
43
+ scores = []
44
+ for b in range(BLOCKS):
45
+ lo = b * BLOCK
46
+ mask = PILOT_MASK[lo : lo + BLOCK].astype(float)
47
+ tmpl = ref[lo : lo + BLOCK] * mask
48
+ span = xw[lo : lo + n + BLOCK - 1]
49
+ c = np.abs(correlate(span, tmpl, mode="valid", method="fft"))
50
+ power = correlate(span**2, mask, mode="valid", method="fft") / mask.sum()
51
+ scores.append(c / np.sqrt(np.maximum(power, 1e-20) * np.sum(tmpl.real**2)))
52
+ return scores
53
+
54
+
55
+ def _candidates(scores: list[np.ndarray]) -> np.ndarray:
56
+ # each block may sit a little early or late from clock drift, so take its best nearby score
57
+ total = sum(maximum_filter1d(s, 2 * _reach(b) + 1) for b, s in enumerate(scores))
58
+ peaks, _ = find_peaks(np.concatenate([[0.0], total, [0.0]]), distance=int(0.9 * FRAME))
59
+ return peaks - 1
60
+
61
+
62
+ def _timing(tau: int, scores: list[np.ndarray]) -> tuple[float, float] | None:
63
+ # fractional frame start and clock rate error, from where each block's pilots line up
64
+ xs, ys, ws = [], [], []
65
+ for b, s in enumerate(scores):
66
+ lo, hi = max(tau - _reach(b), 0), min(tau + _reach(b) + 1, len(s))
67
+ i = lo + int(np.argmax(s[lo:hi]))
68
+ if s[i] < BLOCK_DETECT:
69
+ continue
70
+ frac = 0.0
71
+ if 0 < i < len(s) - 1:
72
+ y0, y1, y2 = s[i - 1 : i + 2]
73
+ curve = y0 - 2 * y1 + y2
74
+ if curve < 0:
75
+ frac = 0.5 * (y0 - y2) / curve
76
+ xs.append(b * BLOCK + BLOCK / 2)
77
+ ys.append(i + frac)
78
+ ws.append(s[i])
79
+ if not xs:
80
+ return None
81
+ x, y, w = np.array(xs), np.array(ys), np.array(ws)
82
+ while len(x) >= 3:
83
+ alpha, start = np.polyfit(x, y, 1, w=w)
84
+ # a weak block can lock onto a noise peak a few samples away; drop it and refit
85
+ keep = np.abs(y - start - alpha * x) < 1.0
86
+ if keep.all():
87
+ if abs(alpha) <= 1.5 * MAX_PPM * 1e-6:
88
+ return float(start), float(alpha)
89
+ break
90
+ if not keep.any():
91
+ break
92
+ x, y, w = x[keep], y[keep], w[keep]
93
+ return float(np.average(y, weights=w)), 0.0
94
+
95
+
96
+ def _band(x: np.ndarray, taps: np.ndarray) -> np.ndarray:
97
+ return fftconvolve(x, taps, mode="same")[HOP:].reshape(FRAME_SYMBOLS, SYMBOL)
98
+
99
+
100
+ def _mean(x: np.ndarray, width: int, lead: int | None = None) -> np.ndarray:
101
+ # running mean over x[i - width + 1 + lead : i + lead + 1], centered by default, that
102
+ # only averages what is there at the ends of the frame
103
+ lead = (width - 1) // 2 if lead is None else lead
104
+ ones = np.ones(width)
105
+ total = np.convolve(x, ones)[lead : lead + len(x)]
106
+ return total / np.convolve(np.ones(len(x)), ones)[lead : lead + len(x)]
107
+
108
+
109
+ def _soft(audio: np.ndarray, start: float, alpha: float, shape: float) -> tuple[np.ndarray, float]:
110
+ # soft values for the frame's data symbols, and the mean pilot SNR in dB
111
+ x = resample_at(audio, start + np.arange(-HOP, FRAME) * (1 + alpha))
112
+ xw, coeffs = whiten(x)
113
+ ref = match(np.concatenate([np.zeros(HOP), reference()]), coeffs, shape)
114
+ ra = hilbert(ref)
115
+
116
+ # each sub-band gets its own phase and signal level from the pilots, so bands where
117
+ # channel noise drowns the data get little weight
118
+ soft = np.zeros(int(np.count_nonzero(~PILOTS)))
119
+ snr = 0.0
120
+ for taps in SUBBANDS:
121
+ xs = _band(xw, taps)
122
+ rs = _band(ref, taps)
123
+ z = np.sum(xs * np.conj(_band(ra, taps)), axis=1)
124
+ z /= np.sqrt(np.mean(xs**2, axis=1) * np.sum(rs**2, axis=1)) + 1e-20
125
+ phase = np.angle(np.convolve(z[PILOTS], np.ones(13), mode="same"))
126
+ rotated = z * np.exp(-1j * np.repeat(phase, 4))
127
+ y = rotated.real
128
+ # once rotated the signal is all in the real part, so the imaginary part measures noise
129
+ noise = _mean(rotated.imag**2, 25) + 1e-12
130
+ # pilots either side of each group of three data symbols: the group after pilot g
131
+ # sits between pilots g and g+1, so average g-1 to g+2
132
+ level = _mean(y[PILOTS], 4, lead=2)
133
+ level = np.repeat(np.maximum(level, 0.0), 4)
134
+ soft += (2 * level * y / noise)[~PILOTS]
135
+ snr += float(np.mean(level**2 / noise))
136
+ return soft, 10 * np.log10(max(snr, 1e-6))
137
+
138
+
139
+ def decode(audio: np.ndarray, shape: float = SHAPE) -> list[Frame]:
140
+ """Every frame that decodes cleanly in audio (mono, RATE), in order of start time."""
141
+ if len(audio) < FRAME:
142
+ return []
143
+ xw, _ = whiten(audio)
144
+ scores = _block_scores(xw)
145
+ frames = []
146
+ for tau in _candidates(scores):
147
+ timing = _timing(int(tau), scores)
148
+ if timing is None:
149
+ continue
150
+ soft, snr = _soft(audio, *timing, shape)
151
+ message = unpack(decode_bits(soft))
152
+ if message is not None:
153
+ frames.append(Frame(timing[0], message, snr))
154
+ return frames
undervoice/_fec.py ADDED
@@ -0,0 +1,55 @@
1
+ """Rate 1/2, K=7 convolutional code (171, 133 octal), Viterbi decoder and interleaver."""
2
+
3
+ from __future__ import annotations
4
+
5
+ import numpy as np
6
+
7
+ POLYS = (0o171, 0o133)
8
+ TAIL = 7
9
+ STRIDE = 37
10
+
11
+ # OUT[reg] = the two coded bits for a 7 bit register whose bit 6 is the newest input
12
+ OUT = np.array([[bin(reg & p).count("1") & 1 for p in POLYS] for reg in range(128)], np.uint8)
13
+
14
+ _STATE = np.arange(64)
15
+ _PREV0 = (_STATE & 31) << 1
16
+ _PREV1 = _PREV0 | 1
17
+ _SIGN0 = 1.0 - 2.0 * OUT[((_STATE >> 5) << 6) | _PREV0]
18
+ _SIGN1 = 1.0 - 2.0 * OUT[((_STATE >> 5) << 6) | _PREV1]
19
+
20
+
21
+ def _positions(n: int) -> np.ndarray:
22
+ return (np.arange(n) * STRIDE) % n
23
+
24
+
25
+ def encode_bits(data: bytes) -> np.ndarray:
26
+ """Encode bytes into interleaved coded bits (0/1), terminated with TAIL zero bits."""
27
+ bits = np.concatenate([np.unpackbits(np.frombuffer(data, np.uint8)), np.zeros(TAIL, np.uint8)])
28
+ coded = np.empty(2 * len(bits), np.uint8)
29
+ state = 0
30
+ for i, b in enumerate(bits):
31
+ reg = (int(b) << 6) | state
32
+ coded[2 * i : 2 * i + 2] = OUT[reg]
33
+ state = reg >> 1
34
+ out = np.empty_like(coded)
35
+ out[_positions(len(coded))] = coded
36
+ return out
37
+
38
+
39
+ def decode_bits(soft: np.ndarray) -> bytes:
40
+ """Viterbi-decode interleaved soft values (positive means bit 0) back to bytes."""
41
+ pairs = soft[_positions(len(soft))].reshape(-1, 2)
42
+ metric = np.full(64, -np.inf)
43
+ metric[0] = 0.0
44
+ choice = np.empty((len(pairs), 64), bool)
45
+ for t, pair in enumerate(pairs):
46
+ m0 = metric[_PREV0] + _SIGN0 @ pair
47
+ m1 = metric[_PREV1] + _SIGN1 @ pair
48
+ choice[t] = m1 > m0
49
+ metric = np.where(choice[t], m1, m0)
50
+ bits = np.empty(len(pairs), np.uint8)
51
+ state = 0
52
+ for t in range(len(pairs) - 1, -1, -1):
53
+ bits[t] = state >> 5
54
+ state = ((state & 31) << 1) | int(choice[t, state])
55
+ return np.packbits(bits[:-TAIL]).tobytes()