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 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 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_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 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