From 0c6333985f848c13e325a7cc02e57ebd69ec5757 Mon Sep 17 00:00:00 2001 From: Josh Bailey Date: Sun, 10 Dec 2023 20:35:35 +0000 Subject: [PATCH] use common get_samples() --- augment/augment.py | 52 +++++----------------------------------- gamutrf/offline.py | 6 ++--- gamutrf/sample_reader.py | 25 +++++++++++-------- 3 files changed, 24 insertions(+), 59 deletions(-) diff --git a/augment/augment.py b/augment/augment.py index 37e1467b..cb2e7e16 100755 --- a/augment/augment.py +++ b/augment/augment.py @@ -13,62 +13,21 @@ uniform_discrete_distribution, ) from torchsig.utils.types import SignalData, SignalDescription -from gamutrf.sample_reader import read_recording, parse_filename +from gamutrf.sample_reader import read_recording, get_samples -def make_signal(samples, sample_rate, center_frequency): +def make_signal(samples, meta): num_iq_samples = samples.shape[0] desc = SignalDescription( - sample_rate=sample_rate, + sample_rate=meta["sample_rate"], num_iq_samples=num_iq_samples, - center_frequency=center_frequency, + center_frequency=meta["center_frequency"], ) # TODO: subclass SignalData with alternate constructor that can take just numpy array signal = SignalData(samples.tobytes(), np.float32, np.complex128, desc) return signal -def get_nosigmf_file(filename): - meta = parse_filename(filename) - sample_rate = meta["sample_rate"] - sample_dtype = meta["sample_dtype"] - sample_len = meta["sample_len"] - center_frequency = meta["freq_center"] - samples = None - for samples_buffer in read_recording( - filename, sample_rate, sample_dtype, sample_len, max_sample_secs=None - ): - if samples is None: - samples = samples_buffer - else: - samples = np.concatenate([samples, samples_buffer]) - signal = make_signal(samples, sample_rate, center_frequency) - return filename, signal - - -def get_signal(filename): - if not os.path.exists(filename): - raise FileNotFoundError(filename) - meta_ext = filename.find(".sigmf-meta") - if meta_ext == -1: - return get_nosigmf_file(filename) - - meta = sigmf.sigmffile.fromfile(filename) - data_filename = filename[:meta_ext] - meta.set_data_file(data_filename) - # read_samples() always converts to host cf32. - samples = meta.read_samples() - global_meta = meta.get_global_info() - sample_rate = global_meta["core:sample_rate"] - sample_type = global_meta["core:datatype"] - captures_meta = meta.get_captures() - center_frequency = None - if captures_meta: - center_frequency = captures_meta[0].get("core:frequency", None) - signal = make_signal(samples, sample_rate, center_frequency) - return data_filename, signal - - def write_signal(filename, signal, transforms_text): first_desc = signal.signal_description[0] signal.iq_data = signal.iq_data.astype(np.complex64) @@ -133,7 +92,8 @@ def argument_parser(): def main(): options = argument_parser().parse_args() - data_filename, signal = get_signal(options.filename) + data_filename, samples, meta = get_samples(options.filename) + signal = make_signal(samples, meta) augment(signal, data_filename, options.outdir, options.n, options.transforms) diff --git a/gamutrf/offline.py b/gamutrf/offline.py index 62dc80de..408b7354 100644 --- a/gamutrf/offline.py +++ b/gamutrf/offline.py @@ -7,7 +7,7 @@ from gnuradio import iqtlabs from gamutrf.grscan import grscan -from gamutrf.sample_reader import parse_filename +from gamutrf.sample_reader import get_samples def argument_parser(): @@ -63,12 +63,12 @@ def main(): options = argument_parser().parse_args() filename = options.filename out_dir = os.path.dirname(filename) - meta = parse_filename(filename) + meta = get_samples(filename) tb = grscan( db_clamp_floor=-1e9, fft_batch_size=256, freq_end=0, - freq_start=meta["freq_center"], + freq_start=meta["freq_center"] - (meta["sample_rate"] / 2), inference_min_db=-1e9, inference_output_dir=out_dir, iqtlabs=iqtlabs, diff --git a/gamutrf/sample_reader.py b/gamutrf/sample_reader.py index 907ea3d7..8ed9c4ad 100644 --- a/gamutrf/sample_reader.py +++ b/gamutrf/sample_reader.py @@ -143,19 +143,19 @@ def read_recording( def get_nosigmf_samples(filename): meta = parse_filename(filename) - sample_rate = meta["sample_rate"] - sample_dtype = meta["sample_dtype"] - sample_len = meta["sample_len"] - center_frequency = meta["freq_center"] samples = None for samples_buffer in read_recording( - filename, sample_rate, sample_dtype, sample_len, max_sample_secs=None + filename, + meta["sample_rate"], + meta["sample_dtype"], + meta["sample_len"], + max_sample_secs=None, ): if samples is None: samples = samples_buffer else: samples = np.concatenate([samples, samples_buffer]) - return filename, samples, center_frequency + return filename, samples, meta def get_samples(filename): @@ -168,13 +168,18 @@ def get_samples(filename): meta = sigmf.sigmffile.fromfile(filename) data_filename = filename[:meta_ext] meta.set_data_file(data_filename) - # read_samples() always converts to host cf32. samples = meta.read_samples() global_meta = meta.get_global_info() - sample_rate = global_meta["core:sample_rate"] - sample_type = global_meta["core:datatype"] captures_meta = meta.get_captures() center_frequency = None if captures_meta: center_frequency = captures_meta[0].get("core:frequency", None) - return data_filename, samples, center_frequency + meta = { + "sample_rate": global_meta["core:sample_rate"], + "sample_dtype": global_meta["core:datatype"], + "sample_len": samples[ + 0 + ].itemsize, # read_samples() always converts to host cf32. + "center_frequency": center_frequency, + } + return data_filename, samples, meta