diff --git a/.env b/.env new file mode 100644 index 00000000..e69de29b diff --git a/.ipynb_checkpoints/polygon_testing-checkpoint.ipynb b/.ipynb_checkpoints/polygon_testing-checkpoint.ipynb new file mode 100644 index 00000000..363fcab7 --- /dev/null +++ b/.ipynb_checkpoints/polygon_testing-checkpoint.ipynb @@ -0,0 +1,6 @@ +{ + "cells": [], + "metadata": {}, + "nbformat": 4, + "nbformat_minor": 5 +} diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 00000000..be753299 --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,15 @@ + +{ + "terminal.integrated.env.osx": { + "PYTHONPATH": "${workspaceFolder}/" + }, + "terminal.integrated.env.linux": { + "PYTHONPATH": "${workspaceFolder}/" + }, + "python.testing.pytestArgs": [ + "tests" + ], + "python.testing.unittestEnabled": false, + "python.testing.pytestEnabled": true, + "python.testing.cwd": "${workspaceFolder}/" +} diff --git a/requirements.txt b/requirements.txt index 71845918..0555194d 100644 --- a/requirements.txt +++ b/requirements.txt @@ -39,6 +39,7 @@ scipy==1.11.4 shapely==2.0.2 six==1.16.0 tomli==2.0.1 -typing-extensions==4.9.0 +tqdm==4.66.1 +typing_extensions==4.9.0 yarl==1.9.4 zipp==3.16.2 diff --git a/spf/sdrpluto/old/01_phase_sync.py b/spf/sdrpluto/old/01_phase_sync.py deleted file mode 100644 index dc716e32..00000000 --- a/spf/sdrpluto/old/01_phase_sync.py +++ /dev/null @@ -1,125 +0,0 @@ -# Starter code from jon Kraft - -import adi - -import matplotlib.pyplot as plt -import numpy as np -from math import lcm -from spf.rf import ULADetector, beamformer - -c = 3e8 -fc0 = int(200e3) -fs = int(12e6) # must be <=30.72 MHz if both channels are enabled -# rx_lo = int(2.4e9) #4e9 -rx_lo = int(2.5e9) # 4e9 -tx_lo = rx_lo - -rx_mode = "manual" # can be "manual" or "slow_attack" -rx_gain = -30 -tx_gain = -30 - -wavelength = c / rx_lo # wavelength of the RF carrier - -rx_n = int(2**10) - -sdr = adi.ad9361(uri="ip:192.168.3.1") - -# setup RX -sdr.rx_enabled_channels = [0, 1] -sdr.sample_rate = fs -assert sdr.sample_rate == fs -sdr.rx_rf_bandwidth = int(fc0 * 3) -sdr.rx_lo = int(rx_lo) -sdr.gain_control_mode = rx_mode -sdr.rx_hardwaregain_chan0 = int(rx_gain) -sdr.rx_hardwaregain_chan1 = int(rx_gain) -sdr.rx_buffer_size = int(rx_n) -sdr._rxadc.set_kernel_buffers_count( - 1 -) # set buffers to 1 (instead of the default 4) to avoid stale data on Pluto - -# setup TX -sdr.tx_enabled_channels = [0, 1] -sdr.tx_rf_bandwidth = int(fc0) -sdr.tx_lo = int(tx_lo) -sdr.tx_cyclic_buffer = True # this keeps repeating! -sdr.tx_hardwaregain_chan0 = int(-88) # tx_gain) -sdr.tx_hardwaregain_chan1 = int(tx_gain) # use Tx2 for calibration -tx_n = int(min(lcm(fc0, fs), rx_n * 8)) # 1024*1024*1024) # tx for longer than rx -sdr.tx_buffer_size = tx_n * 2 # tx_n - -# since its a cyclic buffer its important to end on a full phase -t = np.arange(0, tx_n) / fs -iq0 = np.exp(1j * 2 * np.pi * t * fc0) * (2**14) -sdr.tx([iq0, iq0]) # Send Tx data. - -detector = ULADetector(fs, 2, wavelength / 2) - - -def sample_phase_offset_rx(iterations=64, calibration=1 + 0j): - steerings = [] - for _ in np.arange(iterations): - signal_matrix = np.vstack(sdr.rx()) - thetas, sds, steering = beamformer( - detector, - signal_matrix, - rx_lo, - spacing=2 * 1024 + 1, - calibration=calibration, - ) - steerings.append(steering[sds.argmax()]) - return np.array(steerings).mean(axis=0) - - -calibration = np.conjugate(sample_phase_offset_rx()) -print("calibration complete", calibration) -calibration_new = np.conjugate(sample_phase_offset_rx(calibration=calibration)) -print("new cal", calibration_new) - -sdr.tx_destroy_buffer() -sdr.rx_destroy_buffer() - -# setup RX -sdr.rx_enabled_channels = [0, 1] -sdr.sample_rate = fs -assert sdr.sample_rate == fs -sdr.rx_rf_bandwidth = int(fc0) -sdr.rx_lo = int(rx_lo) -sdr.gain_control_mode = rx_mode -sdr.rx_hardwaregain_chan0 = int(rx_gain) -sdr.rx_hardwaregain_chan1 = int(rx_gain) -sdr.rx_buffer_size = int(rx_n) -sdr._rxadc.set_kernel_buffers_count( - 1 -) # set buffers to 1 (instead of the default 4) to avoid stale data on Pluto - -# setup TX -sdr.tx_enabled_channels = [0] -sdr.tx_rf_bandwidth = int(fc0) -sdr.tx_lo = int(tx_lo) -sdr.tx_cyclic_buffer = True # this keeps repeating! -sdr.tx_hardwaregain_chan0 = int(tx_gain) # tx_gain) -sdr.tx_hardwaregain_chan1 = int(-80) # use Tx2 for calibration - -tx_n = int(min(lcm(fc0, fs), rx_n * 8)) # 1024*1024*1024) # tx for longer than rx -sdr.tx_buffer_size = tx_n - -# since its a cyclic buffer its important to end on a full phase -t = np.arange(0, tx_n) / fs -iq0 = np.exp(1j * 2 * np.pi * t * fc0) * (2**14) -sdr.tx(iq0) # Send Tx data. - -fig, axs = plt.subplots(2, 1, figsize=(4, 8)) -while True: - axs[0].cla() - signal_matrix = np.vstack(sdr.rx()) - thetas, sds, steering = beamformer( - detector, - signal_matrix, - rx_lo, - spacing=2 * 512 + 1, - calibration=calibration, - ) - axs[0].scatter(360 * thetas / (2 * np.pi), sds, s=0.5) - plt.draw() - plt.pause(0.01) diff --git a/spf/sdrpluto/old/02_wifi_direction.py b/spf/sdrpluto/old/02_wifi_direction.py deleted file mode 100644 index 8069fcbc..00000000 --- a/spf/sdrpluto/old/02_wifi_direction.py +++ /dev/null @@ -1,135 +0,0 @@ -# Starter code from jon Kraft - -import adi - -# ;import matplotlib.pyplot as plt -import numpy as np -from math import lcm - -# from sdr import * -from spf.software.rf import ULADetector, beamformer -import matplotlib.pyplot as plt - -c = 3e8 -fc0 = int(500e3) -fs = int(4e6) # must be <=30.72 MHz if both channels are enabled -rx_lo = int(2.45e9) # 4e9 -tx_lo = rx_lo - -rx_mode = "manual" # can be "manual" or "slow_attack" -rx_gain = -30 -tx_gain = -30 - -wavelength = c / rx_lo # wavelength of the RF carrier - -rx_n = int(2**10) - -sdr = adi.ad9361(uri="ip:192.168.3.1") - -# setup RX -sdr.rx_enabled_channels = [0, 1] -sdr.sample_rate = fs -assert sdr.sample_rate == fs -sdr.rx_rf_bandwidth = int(fc0 * 3) -sdr.rx_lo = int(rx_lo) -sdr.gain_control_mode = rx_mode -sdr.rx_hardwaregain_chan0 = int(rx_gain) -sdr.rx_hardwaregain_chan1 = int(rx_gain) -sdr.rx_buffer_size = int(rx_n) -sdr._rxadc.set_kernel_buffers_count( - 1 -) # set buffers to 1 (instead of the default 4) to avoid stale data on Pluto - -# setup TX -sdr.tx_enabled_channels = [0, 1] -sdr.tx_rf_bandwidth = int(fc0 * 3) -sdr.tx_lo = int(tx_lo) -sdr.tx_cyclic_buffer = True # this keeps repeating! -sdr.tx_hardwaregain_chan0 = int(-88) # tx_gain) -sdr.tx_hardwaregain_chan1 = int(tx_gain) # use Tx2 for calibration -tx_n = int(min(lcm(fc0, fs), rx_n * 8)) # 1024*1024*1024) # tx for longer than rx -sdr.tx_buffer_size = tx_n * 2 # tx_n - -# since its a cyclic buffer its important to end on a full phase -t = np.arange(0, tx_n) / fs -iq0 = np.exp(1j * 2 * np.pi * t * fc0) * (2**14) -sdr.tx([iq0, iq0]) # Send Tx data. - -detector = ULADetector(fs, 2, wavelength / 2) - - -def sample_phase_offset_rx(iterations=64, calibration=1 + 0j): - steerings = [] - for _ in np.arange(iterations): - signal_matrix = np.vstack(sdr.rx()) - thetas, sds, steering = beamformer( - detector, - signal_matrix, - rx_lo, - spacing=2 * 1024 + 1, - calibration=calibration, - ) - steerings.append(steering[sds.argmax()]) - return np.array(steerings).mean(axis=0) - - -calibration = np.conjugate(sample_phase_offset_rx()) -print("calibration complete", calibration) -calibration_new = np.conjugate(sample_phase_offset_rx(calibration=calibration)) -print("new cal", calibration_new) - -sdr.tx_destroy_buffer() -sdr.rx_destroy_buffer() - -# setup RX -sdr.rx_enabled_channels = [0, 1] -sdr.sample_rate = fs -assert sdr.sample_rate == fs -sdr.rx_rf_bandwidth = int(fc0 * 3) -sdr.rx_lo = int(rx_lo) -sdr.gain_control_mode = rx_mode -sdr.rx_hardwaregain_chan0 = int(rx_gain) -sdr.rx_hardwaregain_chan1 = int(rx_gain) -sdr.rx_buffer_size = int(rx_n) -sdr._rxadc.set_kernel_buffers_count( - 1 -) # set buffers to 1 (instead of the default 4) to avoid stale data on Pluto - -# setup TX -sdr.tx_enabled_channels = [] -sdr.tx_rf_bandwidth = int(fc0 * 3) -sdr.tx_lo = int(tx_lo) -sdr.tx_cyclic_buffer = True # this keeps repeating! -sdr.tx_hardwaregain_chan0 = int(tx_gain) # tx_gain) #tx_gain) -sdr.tx_hardwaregain_chan1 = int(-80) # use Tx2 for calibration -# -tx_n = int(min(lcm(fc0, fs), rx_n * 8)) # 1024*1024*1024) # tx for longer than rx -sdr.tx_buffer_size = tx_n - -# since its a cyclic buffer its important to end on a full phase -t = np.arange(0, tx_n) / fs -iq0 = np.exp(1j * 2 * np.pi * t * fc0) * (2**14) -sdr.tx(iq0) # Send Tx data. -import time - -fig, axs = plt.subplots(1, 1, figsize=(4, 4)) - -intervals = 2 * 64 + 1 -counts = np.zeros(intervals - 1) -while True: - signal_matrix = np.vstack(sdr.rx()) - thetas, sds, steering = beamformer( - detector, - signal_matrix, - rx_lo, - spacing=intervals, - calibration=calibration, - ) - if sds.max() > 1000: - print(time.time(), sds.max(), thetas[sds.argmax()]) - counts[sds.argmax() % (intervals - 1)] += 1 - axs.cla() - axs.stairs(counts, 360 * thetas / (2 * np.pi)) - # axs.scatter(360*thetas/(2*np.pi),sds,s=0.5) - plt.draw() - plt.pause(0.01) diff --git a/spf/sdrpluto/phase_cal/01_phase_cal.py b/spf/sdrpluto/standalone_scripts/phase_cal/01_phase_cal.py similarity index 100% rename from spf/sdrpluto/phase_cal/01_phase_cal.py rename to spf/sdrpluto/standalone_scripts/phase_cal/01_phase_cal.py diff --git a/spf/sdrpluto/test_emitter_recv/03_only_emit.py b/spf/sdrpluto/standalone_scripts/test_emitter_recv/03_only_emit.py similarity index 100% rename from spf/sdrpluto/test_emitter_recv/03_only_emit.py rename to spf/sdrpluto/standalone_scripts/test_emitter_recv/03_only_emit.py diff --git a/spf/sdrpluto/test_emitter_recv/04_plot_signal.py b/spf/sdrpluto/standalone_scripts/test_emitter_recv/04_plot_signal.py similarity index 100% rename from spf/sdrpluto/test_emitter_recv/04_plot_signal.py rename to spf/sdrpluto/standalone_scripts/test_emitter_recv/04_plot_signal.py diff --git a/spf/sdrpluto/test_single_phase/02_wifi_direction.py b/spf/sdrpluto/standalone_scripts/test_single_phase/02_wifi_direction.py similarity index 100% rename from spf/sdrpluto/test_single_phase/02_wifi_direction.py rename to spf/sdrpluto/standalone_scripts/test_single_phase/02_wifi_direction.py diff --git a/spf/sdrpluto/test.py b/spf/sdrpluto/test.py deleted file mode 100644 index df8256ed..00000000 --- a/spf/sdrpluto/test.py +++ /dev/null @@ -1,8 +0,0 @@ -import iio - -ctx = iio.Context("ip:pluto.local") -for dev in ctx.devices: - for chan in dev.channels: - for attr in chan.attrs: - print(dev.name, chan.name, attr) - # print(f'{dev.name}: {chan.name}: {attr.name}') diff --git a/tests/test_dataset.py b/tests/test_dataset.py new file mode 100644 index 00000000..8d49134b --- /dev/null +++ b/tests/test_dataset.py @@ -0,0 +1,56 @@ +import argparse +import os +import numpy as np +import tempfile +from spf.dataset.spf_generate import generate_session_and_dump, generate_session +from compress_pickle import dump +import pytest + +from joblib import Parallel, delayed + +class dotdict(dict): + """dot.notation access to dictionary attributes""" + __getattr__ = dict.get + __setattr__ = dict.__setitem__ + __delattr__ = dict.__delitem__ + +@pytest.fixture +def default_args(): + return dotdict({ + 'carrier_frequency':2.4e9, + 'signal_frequency':100e3, + 'sampling_frequency':10e6, + 'array_type':"linear", # "circular"], + 'elements':11, + 'random_silence':False, + 'detector_noise':1e-4, + 'random_emitter_timing':False, + 'sources':2, + 'seed':0, + 'beam_former_spacing':256 + 1, + 'width':128, + 'detector_trajectory':'bounce', + 'detector_speed':10.0, + 'source_speed':0.0, + 'sigma':1.0, + 'time_steps':100, + 'time_interval':0.3, + 'samples_per_snapshot':3, + 'sessions':16, + + 'reference':False, + 'cpus':8, + 'live':False, + 'profile':False, + 'fixed_detector':None # + }) + +def test_data_generation(default_args): + with tempfile.TemporaryDirectory() as tmp: + args = default_args + args.output=tmp + print(args) + + dump(args, "/".join([args.output, "args.pkl"]), compression="lzma") + result = [ generate_session_and_dump((args, session_idx)) + for session_idx in range(args.sessions) ]