def test_spectrum(random_samples): from wwb_scanner.scan_objects import Spectrum rs = 2.048e6 fc = 600e6 spectrum = Spectrum() freqs, sig, ff = random_samples(rs=rs, fc=fc) for freq, val in zip(freqs, ff): spectrum.add_sample(frequency=freq, iq=val) assert np.array_equal(spectrum.sample_data['frequency'], freqs) assert np.array_equal(spectrum.sample_data['iq'], ff) assert np.array_equal(spectrum.sample_data['magnitude'], np.abs(ff)) assert np.array_equal(spectrum.sample_data['dbFS'], 10 * np.log10(np.abs(ff)))
def on_load(self, *args): node = self.tree_view.selected_node if node is None: return spectrum = Spectrum.from_dbstore(eid=node.eid) self.app.root.plot_container.add_plot(spectrum=spectrum) self.on_cancel()
def test_exporters(data_files, tmpdir): from wwb_scanner.scan_objects import Spectrum for fkey, d in data_files.items(): p = tmpdir.mkdir('-'.join((str(f) for f in fkey))) for skey, fn in d.items(): src_spectrum = Spectrum.import_from_file(fn) assert not np.any(np.isnan(src_spectrum.sample_data['magnitude'])) assert not np.any(np.isnan(src_spectrum.sample_data['dbFS'])) for ext in ['csv', 'sdb2']: exp_fn = p.join('{}_src.{}'.format(skey, ext)) src_spectrum.export_to_file(filename=str(exp_fn)) imp_spectrum = Spectrum.import_from_file(str(exp_fn)) # Account for frequency units in KHz for sdb2 assert np.allclose(src_spectrum.sample_data['frequency'], imp_spectrum.sample_data['frequency']) assert np.array_equal(src_spectrum.sample_data['magnitude'], imp_spectrum.sample_data['magnitude'])
def __init__(self, **kwargs): self._running = threading.Event() self._stopped = threading.Event() self._current_freq = None self._progress = 0. ckwargs = kwargs.get('config') if not ckwargs: ckwargs = db_store.get_scan_config() if not ckwargs: ckwargs = {} self.config = ScanConfig(ckwargs) self.device_config = self.config.device self.sampling_config = self.config.sampling if 'spectrum' in kwargs: self.spectrum = Spectrum.from_json(kwargs['spectrum']) else: self.spectrum = Spectrum() self.spectrum.scan_config = self.config if not kwargs.get('__from_json__'): self.sample_collection = SampleCollection(scanner=self)
def test_exporters(data_files, tmpdir): from wwb_scanner.scan_objects import Spectrum for fkey, d in data_files.items(): p = tmpdir.mkdir('-'.join((str(f) for f in fkey))) for skey, fn in d.items(): src_spectrum = Spectrum.import_from_file(fn) assert not np.any(np.isnan(src_spectrum.sample_data['magnitude'])) assert not np.any(np.isnan(src_spectrum.sample_data['dbFS'])) for ext in ['csv', 'sdb2']: exp_fn = p.join('{}_src.{}'.format(skey, ext)) src_spectrum.export_to_file(filename=str(exp_fn)) imp_spectrum = Spectrum.import_from_file(str(exp_fn)) # Account for frequency units in KHz for sdb2 assert np.allclose(src_spectrum.sample_data['frequency'], imp_spectrum.sample_data['frequency']) # sdb2 stores dB values rounded to the first decimal assert np.array_equal( np.around(src_spectrum.sample_data['dbFS'], 1), np.around(imp_spectrum.sample_data['dbFS'], 1), )
def on_sweep_processed(self, **kwargs): freqs = kwargs.get('frequencies') powers = kwargs.get('powers') fc = kwargs.get('sample_set').center_frequency / 1e6 fc = float(fc) spectrum = self.current_spectrum new_spectrum = False if spectrum is None or fc != self.scan_controls.current_freq: spectrum = Spectrum() new_spectrum = True try: self.scan_controls.current_freq = fc except: print(type(fc), repr(fc)) self.cancel_scan() raise spectrum.add_sample_set(frequency=freqs, iq=powers, center_frequency=fc) if new_spectrum: self.current_spectrum = spectrum elif self.scan_controls.live_view_visible: sg = self.scan_controls.live_spectrum_graph plot = sg.spectrum_plot_container.children[0] plot.update_data()
def on_sweep_processed(self, **kwargs): freqs = kwargs.get('frequencies') powers = kwargs.get('powers') fc = kwargs.get('sample_set').center_frequency / 1e6 fc = float(fc) spectrum = self.current_spectrum new_spectrum = False if spectrum is None or fc != self.scan_controls.current_freq: spectrum = Spectrum() new_spectrum = True try: self.scan_controls.current_freq = fc except: print type(fc), repr(fc) self.cancel_scan() raise for f, val in zip(freqs, powers): spectrum.add_sample(frequency=f, dbFS=val, force_magnitude=True) if new_spectrum: self.current_spectrum = spectrum elif self.scan_controls.live_view_visible: sg = self.scan_controls.live_spectrum_graph plot = sg.spectrum_plot_container.children[0] plot.update_data()
def test_io(tmpdir, random_samples): from wwb_scanner.scan_objects import Spectrum rs = 1.024e6 nsamp = 256 step_size = 0.5e6 freq_range = [572e6, 636e6] def build_data(fc): freqs, sig, Pxx = random_samples(n=nsamp, rs=rs, fc=fc) return freqs, Pxx spectrum = Spectrum(step_size=step_size) spectrum.color['r'] = 1. fc = freq_range[0] while True: freqs, ff = build_data(fc) spectrum.add_sample_set(frequency=freqs, iq=ff) if spectrum.sample_data['frequency'].max() >= freq_range[1] / 1e6: break fc += step_size dB = np.around(spectrum.sample_data['dbFS'], decimals=1) p = tmpdir.mkdir('test_io') for ext in ['csv', 'sdb2']: fn = p.join('foo.{}'.format(ext)) spectrum.export_to_file(filename=str(fn)) imp_spectrum = Spectrum.import_from_file(str(fn)) assert np.allclose(spectrum.sample_data['frequency'], imp_spectrum.sample_data['frequency']) imp_dB = np.around(imp_spectrum.sample_data['dbFS'], decimals=1) assert np.allclose(dB, imp_dB)
def test_dbstore(tmp_db_store, data_files, random_samples): from wwb_scanner.file_handlers import BaseImporter from wwb_scanner.scan_objects import Spectrum from wwb_scanner.utils.dbstore import db_store spec = {} for fkey, d in data_files.items(): for skey, fn in d.items(): spectrum = BaseImporter.import_file(fn) spectrum.save_to_dbstore() assert spectrum.eid is not None spec[spectrum.eid] = spectrum rs = 1.024e6 nsamp = 256 step_size = 0.5e6 freq_range = [572e6, 636e6] spectrum = Spectrum() fc = freq_range[0] while True: freqs, sig, Pxx = random_samples(n=nsamp, rs=rs, fc=fc) spectrum.add_sample_set(frequency=freqs, iq=Pxx) if spectrum.sample_data['frequency'].max() >= freq_range[1] / 1e6: break fc += step_size spectrum.save_to_dbstore() assert spectrum.eid is not None spec[spectrum.eid] = spectrum for eid, spectrum in spec.items(): db_spectrum = Spectrum.from_dbstore(eid=spectrum.eid) for attr in Spectrum._serialize_attrs: assert getattr(spectrum, attr) == getattr(db_spectrum, attr) assert np.array_equal(spectrum.sample_data, db_spectrum.sample_data) db_data = db_store.get_all_scans() assert set(db_data.keys()) == set(spec.keys())
def test_add_sample_set(random_samples): from wwb_scanner.scan_objects import Spectrum rs = 2.048e6 def build_data(fc): freqs, sig, Pxx = random_samples(n=256, rs=rs, fc=fc) return freqs, Pxx fc = 600e6 spectrum = Spectrum() freqs, ff = build_data(fc) spectrum.add_sample_set(frequency=freqs, iq=ff, center_frequency=fc, force_lower_freq=True) assert np.array_equal(spectrum.sample_data['frequency'], freqs) assert np.array_equal(spectrum.sample_data['iq'], ff) assert np.array_equal(spectrum.sample_data['magnitude'], np.abs(ff)) assert np.array_equal(spectrum.sample_data['dbFS'], 10 * np.log10(np.abs(ff))) fc += 1e6 freqs2, ff2 = build_data(fc) assert np.any(np.in1d(freqs, freqs2)) print('in1d: ', np.nonzero(np.in1d(spectrum.sample_data['frequency'], freqs2))) print('spectrum size: ', spectrum.sample_data['frequency'].size) spectrum.add_sample_set(frequency=freqs2, iq=ff2, center_frequency=fc, force_lower_freq=True) print('spectrum size: ', spectrum.sample_data['frequency'].size) assert np.unique(spectrum.sample_data['frequency']).size == spectrum.sample_data['frequency'].size for freq, val in zip(freqs2, ff2): sample = spectrum.samples[freq] ix = sample.spectrum_index iq = spectrum.sample_data['iq'][ix] m = spectrum.sample_data['magnitude'][ix] dB = spectrum.sample_data['dbFS'][ix] assert iq == val == sample.iq assert m == np.abs(val) == sample.magnitude assert dB == 10 * np.log10(np.abs(val)) == sample.dbFS fc = 800e6 freqs3, ff3 = build_data(fc) assert not np.any(np.in1d(spectrum.sample_data['frequency'], freqs3)) spectrum.add_sample_set(frequency=freqs3, iq=ff3, center_frequency=fc, force_lower_freq=True) print('spectrum size: ', spectrum.sample_data['frequency'].size) assert np.unique(spectrum.sample_data['frequency']).size == spectrum.sample_data['frequency'].size assert spectrum.sample_data['frequency'].size == spectrum.sample_data['iq'].size assert spectrum.sample_data['frequency'].size == spectrum.sample_data['magnitude'].size for freq, val in zip(freqs3, ff3): sample = spectrum.samples[freq] ix = sample.spectrum_index iq = spectrum.sample_data['iq'][ix] m = spectrum.sample_data['magnitude'][ix] dB = spectrum.sample_data['dbFS'][ix] assert iq == val == sample.iq assert m == np.abs(val) == sample.magnitude assert dB == 10 * np.log10(np.abs(val)) == sample.dbFS
class ScannerBase(JSONMixin): WINDOW_TYPES = WINDOW_TYPES def __init__(self, **kwargs): self._running = threading.Event() self._stopped = threading.Event() self._current_freq = None self._progress = 0. ckwargs = kwargs.get('config') if not ckwargs: ckwargs = db_store.get_scan_config() if not ckwargs: ckwargs = {} self.config = ScanConfig(ckwargs) self.device_config = self.config.device self.sampling_config = self.config.sampling if 'spectrum' in kwargs: self.spectrum = Spectrum.from_json(kwargs['spectrum']) else: self.spectrum = Spectrum() self.spectrum.scan_config = self.config if not kwargs.get('__from_json__'): self.sample_collection = SampleCollection(scanner=self) @property def current_freq(self): return self._current_freq @current_freq.setter def current_freq(self, value): self._current_freq = value if value is not None: f_min, f_max = self.config.scan_range self.progress = (value - f_min) / (f_max - f_min) self.on_current_freq(value) def on_current_freq(self, value): print('scanning %s' % (value)) @property def progress(self): return self._progress @progress.setter def progress(self, value): if value == self._progress: return self._progress = value self.on_progress(value) def on_progress(self, value): print('%s%%' % (int(value * 100))) def build_sample_sets(self): freq, end_freq = self.config.scan_range sample_collection = self.sample_collection while freq <= end_freq: sample_set = sample_collection.build_sample_set(mhz_to_hz(freq)) freq += self.step_size def run_scan(self): self.build_sample_sets() running = self._running running.set() self.sample_collection.scan_all_freqs() self.sample_collection.stopped.wait() if running.is_set(): self.save_to_dbstore() running.clear() self._stopped.set() def stop_scan(self): self._running.clear() self.sample_collection.cancel() self._stopped.wait() def scan_freq(self, freq): pass def save_to_dbstore(self): self.spectrum.save_to_dbstore() def _serialize(self): d = dict( config=self.config._serialize(), spectrum=self.spectrum._serialize(), sample_collection=self.sample_collection._serialize(), ) return d def _deserialize(self, **kwargs): data = kwargs.get('sample_collection') self.sample_collection = SampleCollection.from_json(data, scanner=self)
class ScannerBase(JSONMixin): WINDOW_TYPES = WINDOW_TYPES def __init__(self, **kwargs): self._running = threading.Event() self._stopped = threading.Event() self._current_freq = None self._progress = 0. ckwargs = kwargs.get('config') if not ckwargs: ckwargs = db_store.get_scan_config() if not ckwargs: ckwargs = {} self.config = ScanConfig(ckwargs) self.device_config = self.config.device self.sampling_config = self.config.sampling if 'spectrum' in kwargs: self.spectrum = Spectrum.from_json(kwargs['spectrum']) else: self.spectrum = Spectrum() self.spectrum.scan_config = self.config if not kwargs.get('__from_json__'): self.sample_collection = SampleCollection(scanner=self) @property def current_freq(self): return self._current_freq @current_freq.setter def current_freq(self, value): self._current_freq = value if value is not None: f_min, f_max = self.config.scan_range self.progress = (value - f_min) / (f_max - f_min) self.on_current_freq(value) def on_current_freq(self, value): print 'scanning %s' % (value) @property def progress(self): return self._progress @progress.setter def progress(self, value): if value == self._progress: return self._progress = value self.on_progress(value) def on_progress(self, value): print '%s%%' % (int(value * 100)) def build_sample_sets(self): freq, end_freq = self.config.scan_range sample_collection = self.sample_collection while freq <= end_freq: sample_set = sample_collection.build_sample_set(mhz_to_hz(freq)) freq += self.step_size def run_scan(self): self.build_sample_sets() running = self._running running.set() self.sample_collection.scan_all_freqs() self.sample_collection.stopped.wait() if running.is_set(): self.save_to_dbstore() running.clear() self._stopped.set() def stop_scan(self): self._running.clear() self.sample_collection.cancel() self._stopped.wait() def scan_freq(self, freq): pass def save_to_dbstore(self): self.spectrum.save_to_dbstore() def _serialize(self): d = dict( config=self.config._serialize(), spectrum=self.spectrum._serialize(), sample_collection=self.sample_collection._serialize(), ) return d def _deserialize(self, **kwargs): data = kwargs.get('sample_collection') self.sample_collection = SampleCollection.from_json(data, scanner=self)
def test_add_sample_set(random_samples): from wwb_scanner.scan_objects import Spectrum rs = 2.048e6 def build_data(fc): freqs, sig, Pxx = random_samples(n=256, rs=rs, fc=fc) return freqs, Pxx fc = 600e6 spectrum = Spectrum() freqs, ff = build_data(fc) spectrum.add_sample_set(frequency=freqs, iq=ff, center_frequency=fc, force_lower_freq=True) assert np.array_equal(spectrum.sample_data['frequency'], freqs) assert np.array_equal(spectrum.sample_data['iq'], ff) assert np.array_equal(spectrum.sample_data['magnitude'], np.abs(ff)) assert np.array_equal(spectrum.sample_data['dbFS'], 10 * np.log10(np.abs(ff))) fc += 1e6 freqs2, ff2 = build_data(fc) assert np.any(np.in1d(freqs, freqs2)) print('in1d: ', np.nonzero(np.in1d(spectrum.sample_data['frequency'], freqs2))) print('spectrum size: ', spectrum.sample_data['frequency'].size) spectrum.add_sample_set(frequency=freqs2, iq=ff2, center_frequency=fc, force_lower_freq=True) print('spectrum size: ', spectrum.sample_data['frequency'].size) assert np.unique(spectrum.sample_data['frequency'] ).size == spectrum.sample_data['frequency'].size for freq, val in zip(freqs2, ff2): sample = spectrum.samples[freq] ix = sample.spectrum_index iq = spectrum.sample_data['iq'][ix] m = spectrum.sample_data['magnitude'][ix] dB = spectrum.sample_data['dbFS'][ix] assert iq == val == sample.iq assert m == np.abs(val) == sample.magnitude assert dB == 10 * np.log10(np.abs(val)) == sample.dbFS fc = 800e6 freqs3, ff3 = build_data(fc) assert not np.any(np.in1d(spectrum.sample_data['frequency'], freqs3)) spectrum.add_sample_set(frequency=freqs3, iq=ff3, center_frequency=fc, force_lower_freq=True) print('spectrum size: ', spectrum.sample_data['frequency'].size) assert np.unique(spectrum.sample_data['frequency'] ).size == spectrum.sample_data['frequency'].size assert spectrum.sample_data['frequency'].size == spectrum.sample_data[ 'iq'].size assert spectrum.sample_data['frequency'].size == spectrum.sample_data[ 'magnitude'].size for freq, val in zip(freqs3, ff3): sample = spectrum.samples[freq] ix = sample.spectrum_index iq = spectrum.sample_data['iq'][ix] m = spectrum.sample_data['magnitude'][ix] dB = spectrum.sample_data['dbFS'][ix] assert iq == val == sample.iq assert m == np.abs(val) == sample.magnitude assert dB == 10 * np.log10(np.abs(val)) == sample.dbFS
def test_add_sample_set(random_samples): from wwb_scanner.scan_objects import Spectrum from wwb_scanner.scan_objects import SampleArray rs = 2.000e6 def build_data(fc): freqs, sig, Pxx = random_samples(n=256, rs=rs, fc=fc) return freqs, Pxx def build_struct_data(freqs, ff): data = np.zeros(freqs.size, dtype=SampleArray.dtype) data['frequency'] = freqs data['iq'] = ff data['magnitude'] = np.abs(ff) data['dbFS'] = 10 * np.log10(np.abs(ff)) return data def get_overlap_arrays(data1, data2): freqs1 = data1['frequency'] freqs2 = data2['frequency'] overlap_data1 = data1[np.flatnonzero(np.in1d(freqs1, freqs2))] overlap_data2 = data2[np.flatnonzero(np.in1d(freqs2, freqs1))] avg_data = np.zeros(overlap_data1.size, dtype=overlap_data1.dtype) avg_data['frequency'] = overlap_data1['frequency'] for key in ['iq', 'magnitude', 'dbFS']: avg_data[key] = np.mean([overlap_data1[key], overlap_data2[key]], axis=0) non_overlap_data2 = data2[np.flatnonzero(np.in1d(freqs2, freqs1, invert=True))] assert np.array_equal(overlap_data1['frequency'], overlap_data2['frequency']) return avg_data, non_overlap_data2 fc = 600e6 spectrum = Spectrum() freqs, ff = build_data(fc) data1 = build_struct_data(freqs, ff) spectrum.add_sample_set(frequency=freqs, iq=ff, center_frequency=fc, force_lower_freq=True) assert np.array_equal(spectrum.sample_data['frequency'], freqs) assert np.array_equal(spectrum.sample_data['iq'], ff) assert np.array_equal(spectrum.sample_data['magnitude'], np.abs(ff)) assert np.array_equal(spectrum.sample_data['dbFS'], 10 * np.log10(np.abs(ff))) fc += 1e6 freqs2, ff2 = build_data(fc) assert np.any(np.in1d(freqs, freqs2)) print('in1d: ', np.nonzero(np.in1d(spectrum.sample_data['frequency'], freqs2))) print('spectrum size: ', spectrum.sample_data['frequency'].size) spectrum.add_sample_set(frequency=freqs2, iq=ff2, center_frequency=fc, force_lower_freq=True) print('spectrum size: ', spectrum.sample_data['frequency'].size) assert np.unique(spectrum.sample_data['frequency']).size == spectrum.sample_data['frequency'].size data2 = build_struct_data(freqs2, ff2) avg_data, non_overlap_data2 = get_overlap_arrays(data1, data2) assert data2.size == avg_data.size + non_overlap_data2.size assert spectrum.sample_data.size == data1.size + non_overlap_data2.size for freq in freqs2: mask = np.isin([freq], avg_data['frequency']) if np.any(mask): val = avg_data[np.searchsorted(avg_data['frequency'], freq)] else: val = non_overlap_data2[np.searchsorted(non_overlap_data2['frequency'], freq)] sample = spectrum.samples[freq] ix = sample.spectrum_index iq = spectrum.sample_data['iq'][ix] m = spectrum.sample_data['magnitude'][ix] dB = spectrum.sample_data['dbFS'][ix] assert iq == val['iq'] == sample.iq assert m == val['magnitude'] == sample.magnitude assert dB == val['dbFS'] == sample.dbFS fc = 800e6 freqs3, ff3 = build_data(fc) assert not np.any(np.in1d(spectrum.sample_data['frequency'], freqs3)) spectrum.add_sample_set(frequency=freqs3, iq=ff3, center_frequency=fc, force_lower_freq=True) print('spectrum size: ', spectrum.sample_data['frequency'].size) assert np.unique(spectrum.sample_data['frequency']).size == spectrum.sample_data['frequency'].size assert spectrum.sample_data['frequency'].size == spectrum.sample_data['iq'].size assert spectrum.sample_data['frequency'].size == spectrum.sample_data['magnitude'].size data3 = build_struct_data(freqs3, ff3) avg_data2, non_overlap_data3 = get_overlap_arrays(data2, data3) assert data3.size == avg_data2.size + non_overlap_data3.size assert spectrum.sample_data.size == data1.size + non_overlap_data2.size + non_overlap_data3.size for freq in freqs3: mask = np.isin([freq], avg_data2['frequency']) if np.any(mask): val = avg_data2[np.searchsorted(avg_data2['frequency'], freq)] else: val = non_overlap_data3[np.searchsorted(non_overlap_data3['frequency'], freq)] sample = spectrum.samples[freq] ix = sample.spectrum_index iq = spectrum.sample_data['iq'][ix] m = spectrum.sample_data['magnitude'][ix] dB = spectrum.sample_data['dbFS'][ix] assert iq == val['iq'] == sample.iq assert m == val['magnitude'] == sample.magnitude assert dB == val['dbFS'] == sample.dbFS
def _deserialize(self, **kwargs): spdata = kwargs.get('spectrum_data') self.spectrum = Spectrum.from_json(spdata)
def __init__(self, **kwargs): self.spectrum = Spectrum() self.filename = kwargs.get('filename')