def test_001_t(self): """ test one packet spreading and compare it with python implementation """ sf = 2**10 l = 32 * 2 * 8 seed = 12345 reset_per_symbol = False #init data data_in = np.random.randint(0, 2, size=l) #init tag key = pmt.intern("len_tag") val = pmt.from_long(l) tag = gr.tag_utils.python_to_tag([0, key, val]) # set up fg self.src = blocks.vector_source_b(data_in, False, 1, [tag]) self.spread = lpwan.dsss_spreading_bb("len_tag", sf, seed, reset_per_symbol, 0, 0) self.snk = blocks.vector_sink_b(1) self.tb.connect(self.src, self.spread, self.snk) m = dsss_modulator() goldcode = m.generate_goldcode(seed, l * sf) out_ref = m.spread_goldcode(data_in, int(np.log2(sf)), goldcode, reset_per_symbol) self.tb.run() # check data self.assertFloatTuplesAlmostEqual(out_ref, self.snk.data())
def test_002_t(self): #OQPSK, no frequency offset mod = dsss_modulator() mod.shr_goldcode_reset_per_symbol = True mod.shr_goldcode_seed = np.random.randint(500, 500000) mod.shr_spreading_factor = 8 mod.sfd_present = False mod.preamble_size = 32 mod.init() sps = 2 sf = 2**mod.shr_spreading_factor #init preamble block self.preamble_search = dsss_preamble_search_cc( sf, mod.shr_goldcode_seed, 0, 0, [0], mod.preamble_size, False, sps, 0, 1000000, 'oqpsk', [0]) #generate data with preamble only preamble = mod.oqpsk(mod.encode_header()) n = 6 data_in = [] positions = [] for i in range(n): data_in = np.append(data_in, np.zeros(np.random.randint(10000, 20000))) data_in = np.append(data_in, preamble) positions = np.append(positions, len(data_in) + self.preamble_search.rx_delay - 1) #delay in data signal in filterbank data_in = np.append(data_in, np.zeros(np.random.randint(10000, 20000))) data_in = data_in + np.random.randn(len(data_in)) * 5 # set up fg self.src = blocks.vector_source_c_make(data_in, False, 1, []) self.snk_corr = blocks.vector_sink_c_make(1) self.snk_data = blocks.vector_sink_c_make(1) self.tb.connect(self.src, self.preamble_search) self.tb.connect((self.preamble_search, 0), self.snk_data) self.tb.connect((self.preamble_search, 1), self.snk_corr) self.tb.run() # check data tags = self.snk_data.tags() searched_positions = np.array([]) for i in range(len(tags)): searched_positions = np.append(searched_positions, tags[i].offset + sf) found = np.in1d(positions, searched_positions.astype(int)) self.assertTrue(np.all(found))
def test_006_t(self): #OQPSK, frequency offset, multiple frequency paths mod = dsss_modulator() mod.shr_goldcode_reset_per_symbol = True mod.shr_goldcode_seed = np.random.randint(500, 500000) mod.shr_spreading_factor = 8 mod.sfd_present = False mod.preamble_size = 32 mod.init() sps = 2 sf = 2**mod.shr_spreading_factor chiprate = 2000000 fo = 8000 # Hz #init preamble block self.preamble_search = dsss_preamble_search_cc( sf, mod.shr_goldcode_seed, 0, 0, [0, 7900, -7900], mod.preamble_size, False, sps, 0, chiprate, 'oqpsk', [0]) #generate data with preamble only preamble = mod.oqpsk(mod.encode_header()) n = 20 data_in = [] positions = [] for i in range(n): data_in = np.append(data_in, np.zeros(np.random.randint(10000, 20000))) data_in = np.append(data_in, preamble) positions = np.append(positions, len(data_in) + self.preamble_search.rx_delay - 1) #delay in data signal in filterbank data_in = np.append(data_in, np.zeros(np.random.randint(10000, 20000))) #frequency offset data_in = data_in * np.exp( 1j * 2.0 * np.pi * fo / chiprate * np.arange(0, len(data_in))) #add some noise data_in = data_in + np.random.randn(len(data_in)) * 1 # set up fg self.src = blocks.vector_source_c_make(data_in, False, 1, []) self.snk_corr = blocks.vector_sink_c_make(1) self.snk_data = blocks.vector_sink_c_make(1) self.tb.connect(self.src, self.preamble_search) self.tb.connect((self.preamble_search, 0), self.snk_data) self.tb.connect((self.preamble_search, 1), self.snk_corr) self.tb.run() # check data #time offset estimation tags = self.snk_data.tags() searched_positions = np.array([]) for i in range(len(tags)): searched_positions = np.append(searched_positions, tags[i].offset + sf) found = np.in1d(positions, searched_positions.astype(int)) self.assertTrue(np.all(found)) #freq offset estimation mask = np.in1d(searched_positions.astype(int), positions) v = np.asarray(tags)[mask] for t in v: self.assertTrue(pmt.to_python(t.value)['fo_est'] < fo * 1.2) self.assertTrue(pmt.to_python(t.value)['fo_est'] > fo * 0.8)
# Boston, MA 02110-1301, USA. # import numpy as np import dsss_const import dsss_mod import dsss_demod import matplotlib.pyplot as plt import scipy.signal as sig import numpy.random as rnd if __name__ == "__main__": nframes = 2 m = dsss_mod.dsss_modulator() d = dsss_demod.dsss_demodulator() mod_data = m.modulate_random(nframes) # add some noise bb=mod_data[1] l=len(bb) print "Sigpower BB:", sum(abs(mod_data[0]**2))/len(mod_data[0]) noise_power = 1 bb = bb + rnd.normal(size=l, scale=np.sqrt(noise_power)) + 1j * rnd.normal(size=l, scale=np.sqrt(noise_power)) demod_data = d.demod(bb, nframes, m.psdu_gold_code, m.psdu_ovsf_code) print "Produced", nframes, "Frames" print "Spreading Factor for Payload is", m.psdu_spreading_factor print "Spreading Factor for PHY header is", m.shr_spreading_factor