bandsaunter/bandsaunter/images.py
The Dust Council 3d7f76118e Read the pictures, the aircraft, the meters and the sensors
Hexadecimal is a true answer to "what did that say" and not a useful one.
This is the work of turning the rest of what a receiver hears into
something a person can read, and most of it is pictures.

PICTURES

Three of the things on the air are images rather than sounds, and all three
arrive as the audio a scan already records:

  SSTV     14.230 and 144.5 MHz   Martin M1/M2, Scottie S1/S2/DX, Robot 36/72
  APT      137-138 MHz            the NOAA weather satellites
  HF fax   2-20 MHz, sideband     the marine weather charts

Each is written from its published specification, and the generators used to
test them are written from the same specification without reference to the
decoders -- so a picture that comes back matching the one that went in is
evidence about the format.  Every SSTV mode reproduces its published line
time exactly, which is worth failing a test over: a line a few milliseconds
long walks the picture off the screen inside ten lines.  Against synthetic
transmissions at 30 dB SNR, SSTV is 96-98% of pixels exact, APT correlates
at 0.97 and fax at 0.998; all three still read at 6-12 dB.

None of the three is guessed at, and that is what makes it safe to try them
on every recording.  SSTV needs its VIS header, APT needs both line syncs at
the right distance from each other, fax needs the phasing signal.  No false
pictures in 295 attempts over noise, tones, speech and swept whistles.

Two things had to be got right beyond the arithmetic.  A band-pass does not
switch between two tones, it slides between them, so every edge is measured
at the midpoint of the slide rather than at the first sample past a
threshold -- the earlier version was reading the coarse search stride back
as the edge and shifting Martin M1 sideways by a whole colour bar.  And a
picture now keeps its capture whatever the content check made of it: a
satellite is a steady tone with a wobble on it and SSTV is a whistle, so
both were being discarded as "no signal content" having already been
recognised.

PNG is written here rather than pulled in from Pillow.  A scanner that
cannot start because an imaging library is missing is worse than one that
cannot draw.

saunterbrowse marks a picture in the list, gives its path in full -- wrapped
rather than cut off, because half a path opens nothing -- and moves or
deletes the PNGs with the recording.  o prints the picture's path, not the
audio's.

GRIB is not a modulation and is not pretended to be one.  It is the format
weather models are published in and it travels by satellite link and by
e-mail; where a decoded byte stream begins with its magic number it is
named, and that is all.

AIRCRAFT

`bandsaunter adsb` parks the receiver on 1090 MHz and reads Mode S extended
squitter: address, callsign, altitude, position, speed.  A command of its
own because a megabit a second will not go through a channel twelve and a
half kilohertz wide.  Every frame carries a 24-bit checksum so there is no
threshold anywhere in it -- with one trap, which is that a frame of all
zeros satisfies that checksum and silence is exactly that.  Positions round
trip exactly through compact position reporting, and a pair straddling a
longitude-zone boundary is refused rather than resolved against two grids.

METERS AND SENSORS

Itron ERT utility meters on 900 MHz and AcuRite weather sensors on 433 MHz
are named rather than reported as hex, and neither is believed without its
own checksum -- BCH(255,239) for the meter, a checksum and four parity bits
for the sensor.  Both are implemented from published descriptions and
checked against frames built from the same descriptions, which proves the
framing and the arithmetic and is not the same as having held a meter.

HEX INTO WORDS

Everything else that decodes to bits now gets its fields named where the
shape is standard, its text read out where there is text, and its bytes laid
out in groups with the printable characters beside them.

The text search is where the care went, because printability is not
evidence.  Forty framings of each packet, and seven-bit values printable
three in four, meant a bar set on printability called 64% of random payloads
text.  Real text is nearly all one case where random letters are half and
half, two fifths vowels where random is a fifth, and mostly alphanumeric
where random draws punctuation one time in four.  Together: under 0.5%,
measured in the suite.

CALLSIGNS

The licensed address is recorded in full -- the street, not merely the town
-- and goes into the KML with everything else.  US amateur records are
public by law and carry it; holding it and not saying so is worse than
either showing it or not asking, and --no-lookup asks for none of it.

Also here: Morse is decoded again from the whole recording where the capture
was made in cw mode.  The first pass works from the classifier's buffer,
which holds a few seconds -- enough to say "this is Morse", not enough to
catch a callsign whole between two word gaps, so a beacon repeating every
eight seconds through an eight-second window was never identified.

And classify._psk_order took the logarithm of zero on a silent block.

1318 tests, up from 1161.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_016PsWPTweCT6pwxKngvVxcg
2026-08-29 19:38:37 -07:00

211 lines
8.1 KiB
Python

"""Pictures off the air, and the file they get written to.
Three of the things a receiver can hear are images rather than sound or
bits: weather satellites in the 137 MHz band sending APT, amateurs sending
SSTV on 14.230 MHz and 144.5 MHz, and the marine weather fax stations that
have been sending charts on shortwave since before any of this was digital.
All three are analogue -- brightness is a frequency, and a picture is a very
long single tone that wobbles -- so all three come out of the same place:
the audio the scanner already records.
This module is what they have in common. The decoders themselves are in
:mod:`sstv`, :mod:`apt` and :mod:`fax`.
PNG is written here rather than pulled in from Pillow. A scanner that
cannot start because an imaging library is missing is worse than one that
cannot draw, the format's own container is a dozen lines of zlib and
struct, and this way the pictures work on a machine with nothing installed
but numpy.
"""
from __future__ import annotations
import struct
import zlib
from dataclasses import dataclass, field
from pathlib import Path
import numpy as np
from scipy import signal as sps
__all__ = ["ImageDecode", "write_png", "instantaneous_frequency",
"tone_amplitude", "resample_to", "PNG_SIGNATURE"]
PNG_SIGNATURE = b"\x89PNG\r\n\x1a\n"
# ---------------------------------------------------------------------------
# What a decoder gives back
# ---------------------------------------------------------------------------
@dataclass
class ImageDecode:
"""One picture recovered from a capture."""
ok: bool = False
kind: str = "" # APT / SSTV / HF fax
mode: str = "" # NOAA APT, Martin M1, 120 lpm IOC 576, ...
width: int = 0
height: int = 0
pixels: np.ndarray | None = field(default=None, repr=False)
path: str = "" # where it was written, once it has been
complete: bool = True # False when the capture ran out partway
seconds: float = 0.0 # of signal that went into it
confidence: float = 0.0
channels: dict[str, str] = field(default_factory=dict) # extra images
note: str = ""
@property
def name(self) -> str:
return f"{self.kind} ({self.mode})" if self.mode else self.kind
def summary(self) -> str:
if not self.ok:
return self.note or "no image decoded"
size = f"{self.width}x{self.height}"
bits = [self.name, size]
if not self.complete:
bits.append("partial")
if self.path:
bits.append(Path(self.path).name)
return " ".join(bits)
def save(self, path) -> str:
"""Write the picture, and remember where it went."""
if self.pixels is None:
return ""
written = write_png(path, self.pixels)
self.path = str(written)
return self.path
# ---------------------------------------------------------------------------
# PNG
# ---------------------------------------------------------------------------
def _chunk(tag: bytes, data: bytes) -> bytes:
return (struct.pack(">I", len(data)) + tag + data
+ struct.pack(">I", zlib.crc32(tag + data) & 0xFFFFFFFF))
def write_png(path, pixels: np.ndarray) -> Path:
"""Write a greyscale or RGB array as a PNG.
``pixels`` is ``(height, width)`` for grey or ``(height, width, 3)`` for
colour, in any numeric type; it is clipped into a byte rather than scaled,
because a decoder that has already decided what is black and what is white
should not have that decision quietly changed here.
"""
data = np.asarray(pixels)
if data.ndim == 2:
colour, depth = 0, 1
elif data.ndim == 3 and data.shape[2] == 3:
colour, depth = 2, 3
else:
raise ValueError(f"cannot write an array of shape {data.shape} as PNG")
if data.dtype != np.uint8:
data = np.clip(data, 0, 255).astype(np.uint8)
height, width = data.shape[0], data.shape[1]
raw = bytearray()
flat = data.reshape(height, width * depth)
for row in flat:
raw.append(0) # filter: none
raw.extend(row.tobytes())
out = bytearray(PNG_SIGNATURE)
out += _chunk(b"IHDR", struct.pack(">IIBBBBB", width, height, 8, colour,
0, 0, 0))
out += _chunk(b"IDAT", zlib.compress(bytes(raw), 6))
out += _chunk(b"IEND", b"")
path = Path(path)
tmp = path.with_suffix(path.suffix + ".tmp")
tmp.write_bytes(bytes(out))
tmp.replace(path) # never a half-written picture
return path
# ---------------------------------------------------------------------------
# The measurements every one of these decoders needs
# ---------------------------------------------------------------------------
def instantaneous_frequency(audio: np.ndarray, rate: float,
low: float = 800.0,
high: float = 2800.0) -> np.ndarray:
"""The frequency of an audio tone, sample by sample, in hertz.
All three picture modes encode brightness as a frequency, so this is the
whole of the demodulation: band-pass to the range the tone lives in, take
the analytic signal, and differentiate its phase.
"""
audio = np.asarray(audio, dtype=np.float64).ravel()
if audio.size < 64:
return np.zeros(0)
nyquist = rate / 2.0
lo = max(1e-4, low / nyquist)
hi = min(0.999, high / nyquist)
if hi <= lo:
return np.zeros(0)
taps = sps.firwin(129, [lo, hi], pass_zero=False)
filtered = sps.lfilter(taps, [1.0], audio)
# Undo the filter's delay. A linear-phase FIR of 129 taps holds its
# output back by 64 samples, which is four milliseconds at 16 kHz -- and
# four milliseconds is most of an SSTV sync pulse and a fifteen-pixel
# shift across a Martin M2 line. It cancels only if every time in the
# decode is measured from this same trace, and the line timings are not:
# they come from the mode specification, in real seconds.
delay = 64
filtered = np.concatenate((filtered[delay:],
np.zeros(delay, dtype=filtered.dtype)))
analytic = sps.hilbert(filtered)
phase = np.unwrap(np.angle(analytic))
freq = np.diff(phase) * rate / (2.0 * np.pi)
# The first samples are the filter filling up and mean nothing.
freq[:65] = freq[65] if freq.size > 65 else 0.0
return freq
def tone_amplitude(audio: np.ndarray, rate: float, centre: float,
width: float = 1200.0) -> np.ndarray:
"""The envelope of an amplitude-modulated subcarrier.
APT is a 2400 Hz tone whose loudness is the brightness of the picture, so
for that one the frequency is fixed and it is the envelope that carries
everything.
"""
audio = np.asarray(audio, dtype=np.float64).ravel()
if audio.size < 64:
return np.zeros(0)
nyquist = rate / 2.0
lo = max(1e-4, (centre - width) / nyquist)
hi = min(0.999, (centre + width) / nyquist)
if hi <= lo:
return np.abs(sps.hilbert(audio))
taps = sps.firwin(129, [lo, hi], pass_zero=False)
return np.abs(sps.hilbert(sps.lfilter(taps, [1.0], audio)))
def resample_to(values: np.ndarray, count: int) -> np.ndarray:
"""Stretch or squash a run of samples to exactly ``count`` of them.
Linear rather than a proper resampler: a picture line is a few hundred
samples wide and the difference is invisible, while a polyphase filter
per line over a fifteen-minute pass is not.
"""
values = np.asarray(values, dtype=np.float64)
if values.size == 0 or count <= 0:
return np.zeros(max(0, count))
if values.size == count:
return values
source = np.linspace(0.0, 1.0, values.size)
target = np.linspace(0.0, 1.0, count)
return np.interp(target, source, values)
def stretch(values: np.ndarray, low: float, high: float) -> np.ndarray:
"""Map a frequency range onto 0-255, clipped at both ends."""
if high <= low:
return np.zeros_like(values, dtype=np.uint8)
scaled = (np.asarray(values, dtype=np.float64) - low) / (high - low)
return np.clip(scaled * 255.0, 0, 255).astype(np.uint8)