Skip to content

Commit

Permalink
use common get_samples()
Browse files Browse the repository at this point in the history
  • Loading branch information
anarkiwi committed Dec 10, 2023
1 parent d52e8ce commit 0c63339
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 59 deletions.
52 changes: 6 additions & 46 deletions augment/augment.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)


Expand Down
6 changes: 3 additions & 3 deletions gamutrf/offline.py
Original file line number Diff line number Diff line change
Expand Up @@ -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():
Expand Down Expand Up @@ -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,
Expand Down
25 changes: 15 additions & 10 deletions gamutrf/sample_reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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

0 comments on commit 0c63339

Please sign in to comment.