def build_modulator_from_args(arguments: argparse.Namespace): if arguments.raw: return None if arguments.parameter_zero is None: raise ValueError("You need to give a modulation parameter for zero (-p0, --parameter-zero)") if arguments.parameter_one is None: raise ValueError("You need to give a modulation parameter for one (-p1, --parameter-one)") result = Modulator("CLI Modulator") result.carrier_freq_hz = float(arguments.carrier_frequency) result.carrier_amplitude = float(arguments.carrier_amplitude) result.carrier_phase_deg = float(arguments.carrier_phase) result.samples_per_bit = int(arguments.bit_length) if arguments.modulation_type == "ASK": if arguments.parameter_zero.endswith("%"): param_zero = float(arguments.parameter_zero[:-1]) else: param_zero = float(arguments.parameter_zero) * 100 if arguments.parameter_one.endswith("%"): param_one = float(arguments.parameter_one[:-1]) else: param_one = float(arguments.parameter_one) * 100 else: param_zero = float(arguments.parameter_zero) param_one = float(arguments.parameter_one) result.param_for_zero = param_zero result.param_for_one = param_one result.modulation_type_str = arguments.modulation_type result.sample_rate = arguments.sample_rate return result
def test_plot(self): modulator = Modulator("gfsk") modulator.modulation_type_str = "GFSK" modulator.samples_per_bit = 100 modulator.sample_rate = 1e6 modulator.param_for_one = 20e3 modulator.param_for_zero = 10e3 modulator.carrier_freq_hz = 15e3 modulator.carrier_phase_deg = 90 modulator.modulate([True, False, True, False, False], 77) data = copy.deepcopy(modulator.modulated_samples) modulator.modulate([False, True, True, True, True, False, True], 100, start=len(data)) data = np.concatenate((data, modulator.modulated_samples)) plt.subplot(2, 1, 1) axes = plt.gca() axes.set_ylim([-2, 2]) plt.plot(data.real) plt.title("Modulated Wave") plt.subplot(2, 1, 2) qad = signalFunctions.afp_demod(np.ascontiguousarray(data), 0, 1) plt.plot(qad) plt.title("Quad Demod") plt.show()
def test_plot(self): modulator = Modulator("gfsk") modulator.modulation_type_str = "GFSK" modulator.samples_per_bit = 100 modulator.sample_rate = 1e6 modulator.param_for_one = 20e3 modulator.param_for_zero = 10e3 modulator.carrier_freq_hz = 15e3 modulator.carrier_phase_deg = 90 modulated_samples = modulator.modulate([True, False, True, False, False], 77) data = copy.deepcopy(modulated_samples) modulated_samples = modulator.modulate([False, True, True, True, True, False, True], 100, start=len(data)) data = np.concatenate((data, modulated_samples)) plt.subplot(2, 1, 1) axes = plt.gca() axes.set_ylim([-2,2]) plt.plot(data.real) plt.title("Modulated Wave") plt.subplot(2, 1, 2) qad = signal_functions.afp_demod(np.ascontiguousarray(data), 0, 1) plt.plot(qad) plt.title("Quad Demod") plt.show()
def build_modulator_from_args(arguments: argparse.Namespace): if arguments.raw: return None if arguments.bits_per_symbol is None: arguments.bits_per_symbol = 1 n = 2 ** int(arguments.bits_per_symbol) if arguments.parameters is None or len(arguments.parameters) != n: raise ValueError("You need to give {} parameters for {} bits per symbol".format(n, int(arguments.bits_per_symbol))) result = Modulator("CLI Modulator") result.carrier_freq_hz = float(arguments.carrier_frequency) result.carrier_amplitude = float(arguments.carrier_amplitude) result.carrier_phase_deg = float(arguments.carrier_phase) result.samples_per_symbol = int(arguments.samples_per_symbol) result.bits_per_symbol = int(arguments.bits_per_symbol) result.modulation_type = arguments.modulation_type result.sample_rate = arguments.sample_rate for i, param in enumerate(arguments.parameters): if result.is_amplitude_based and param.endswith("%"): result.parameters[i] = float(param[:-1]) elif result.is_amplitude_based and not param.endswith("%"): result.parameters[i] = float(param) * 100 else: result.parameters[i] = float(param) return result
def test_channels(self): sample_rate = 10**6 channel1_freq = 40 * 10**3 channel2_freq = 240 * 10**3 channel1_data = array.array("B", [1, 0, 1, 0, 1, 0, 0, 1]) channel2_data = array.array("B", [1, 1, 0, 0, 1, 1, 0, 1]) channel3_data = array.array("B", [1, 0, 0, 1, 0, 1, 1, 1]) filter_bw = 0.1 filter_freq1_high = 1.5 * channel1_freq filter_freq1_low = 0.5 * channel1_freq filter_freq2_high = 1.5 * channel2_freq filter_freq2_low = 0.5 * channel2_freq modulator1, modulator2, modulator3 = Modulator("test"), Modulator( "test2"), Modulator("test3") modulator1.carrier_freq_hz = channel1_freq modulator2.carrier_freq_hz = channel2_freq modulator3.carrier_freq_hz = -channel2_freq modulator1.sample_rate = modulator2.sample_rate = modulator3.sample_rate = sample_rate data1 = modulator1.modulate(channel1_data) data2 = modulator2.modulate(channel2_data) data3 = modulator3.modulate(channel3_data) mixed_signal = data1 + data2 + data3 mixed_signal.tofile("/tmp/three_channels.complex") plt.subplot("221") plt.title("Signal") plt.plot(mixed_signal) spectrogram = Spectrogram(mixed_signal) plt.subplot("222") plt.title("Spectrogram") plt.imshow(np.transpose(spectrogram.data), aspect="auto", cmap="magma") plt.ylim(0, spectrogram.freq_bins) chann1_filtered = Filter.apply_bandpass_filter( mixed_signal, filter_freq1_low / sample_rate, filter_freq1_high / sample_rate, filter_bw) plt.subplot("223") plt.title("Channel 1 Filtered ({})".format("".join( map(str, channel1_data)))) plt.plot(chann1_filtered) chann2_filtered = Filter.apply_bandpass_filter( mixed_signal, filter_freq2_low / sample_rate, filter_freq2_high / sample_rate, filter_bw) plt.subplot("224") plt.title("Channel 2 Filtered ({})".format("".join( map(str, channel2_data)))) plt.plot(chann2_filtered) plt.show()
def test_channels(self): sample_rate = 10 ** 6 channel1_freq = 40 * 10 ** 3 channel2_freq = 240 * 10 ** 3 channel1_data = array.array("B", [1, 0, 1, 0, 1, 0, 0, 1]) channel2_data = array.array("B", [1, 1, 0, 0, 1, 1, 0, 1]) channel3_data = array.array("B", [1, 0, 0, 1, 0, 1, 1, 1]) filter_bw = 0.1 filter_freq1_high = 1.5 * channel1_freq filter_freq1_low = 0.5 * channel1_freq filter_freq2_high = 1.5*channel2_freq filter_freq2_low = 0.5 * channel2_freq modulator1, modulator2, modulator3 = Modulator("test"), Modulator("test2"), Modulator("test3") modulator1.carrier_freq_hz = channel1_freq modulator2.carrier_freq_hz = channel2_freq modulator3.carrier_freq_hz = -channel2_freq modulator1.sample_rate = modulator2.sample_rate = modulator3.sample_rate = sample_rate data1 = modulator1.modulate(channel1_data) data2 = modulator2.modulate(channel2_data) data3 = modulator3.modulate(channel3_data) mixed_signal = data1 + data2 + data3 mixed_signal.tofile("/tmp/three_channels.complex") plt.subplot("221") plt.title("Signal") plt.plot(mixed_signal) spectrogram = Spectrogram(mixed_signal) plt.subplot("222") plt.title("Spectrogram") plt.imshow(np.transpose(spectrogram.data), aspect="auto", cmap="magma") plt.ylim(0, spectrogram.freq_bins) chann1_filtered = Filter.apply_bandpass_filter(mixed_signal, filter_freq1_low / sample_rate, filter_freq1_high / sample_rate, filter_bw) plt.subplot("223") plt.title("Channel 1 Filtered ({})".format("".join(map(str, channel1_data)))) plt.plot(chann1_filtered) chann2_filtered = Filter.apply_bandpass_filter(mixed_signal, filter_freq2_low / sample_rate, filter_freq2_high / sample_rate, filter_bw) plt.subplot("224") plt.title("Channel 2 Filtered ({})".format("".join(map(str, channel2_data)))) plt.plot(chann2_filtered) plt.show()
def read_modulators_from_file(self, filename: str): if not filename: return [] tree = ET.parse(filename) root = tree.getroot() result = [] for mod_tag in root.iter("modulator"): mod = Modulator(mod_tag.attrib["name"]) mod.carrier_freq_hz = float(mod_tag.attrib["carrier_freq_hz"]) mod.carrier_amplitude = float(mod_tag.attrib["carrier_amplitude"]) mod.carrier_phase_deg = float(mod_tag.attrib["carrier_phase_deg"]) mod.modulation_type = int(mod_tag.attrib["modulation_type"]) mod.sample_rate = float(mod_tag.attrib["sample_rate"]) mod.param_for_one = float(mod_tag.attrib["param_for_one"]) mod.param_for_zero = float(mod_tag.attrib["param_for_zero"]) result.append(mod) return result
def test_performance(self): self.form = MainController() self.cfc = self.form.compare_frame_controller self.stc = self.form.simulator_tab_controller self.gtc = self.form.generator_tab_controller self.form.add_signalfile(get_path_for_data_file("esaver.complex16s")) self.sframe = self.form.signal_tab_controller.signal_frames[0] self.sim_frame = self.form.simulator_tab_controller self.form.ui.tabWidget.setCurrentIndex(3) self.cfc.proto_analyzer.auto_assign_labels() self.network_sdr_plugin_sender = NetworkSDRInterfacePlugin(raw_mode=True) part_a = Participant("Device A", shortname="A", color_index=0) part_b = Participant("Device B", shortname="B", color_index=1) part_b.simulate = True self.form.project_manager.participants.append(part_a) self.form.project_manager.participants.append(part_b) self.form.project_manager.project_updated.emit() sniffer = ProtocolSniffer(100, 0.01, 0.01, 0.1, 5, "FSK", 1, NetworkSDRInterfacePlugin.NETWORK_SDR_NAME, BackendHandler(), network_raw_mode=True) sender = EndlessSender(BackendHandler(), NetworkSDRInterfacePlugin.NETWORK_SDR_NAME) simulator = Simulator(self.stc.simulator_config, self.gtc.modulators, self.stc.sim_expression_parser, self.form.project_manager, sniffer=sniffer, sender=sender) pause = 100 msg_a = SimulatorMessage(part_b, [1, 0] * 16 + [1, 1, 0, 0] * 8 + [0, 0, 1, 1] * 8 + [1, 0, 1, 1, 1, 0, 0, 1, 1, 1] * 4, pause=pause, message_type=MessageType("empty_message_type"), source=part_a) msg_b = SimulatorMessage(part_a, [1, 0] * 16 + [1, 1, 0, 0] * 8 + [1, 1, 0, 0] * 8 + [1, 0, 1, 1, 1, 0, 0, 1, 1, 1] * 4, pause=pause, message_type=MessageType("empty_message_type"), source=part_b) self.stc.simulator_config.add_items([msg_a, msg_b], 0, None) self.stc.simulator_config.update_active_participants() port = self.get_free_port() sniffer = simulator.sniffer sniffer.rcv_device.set_server_port(port) self.network_sdr_plugin_sender.client_port = port sender = simulator.sender port = self.get_free_port() sender.device.set_client_port(port) sender.device._VirtualDevice__dev.name = "simulator_sender" current_index = Value("L") elapsed = Value("f") target_num_samples = 13600 + pause receive_process = Process(target=receive, args=(port, current_index, target_num_samples, elapsed)) receive_process.daemon = True receive_process.start() # Ensure receiver is running time.sleep(2) # spy = QSignalSpy(self.network_sdr_plugin_receiver.rcv_index_changed) simulator.start() modulator = Modulator("test_modulator") modulator.samples_per_symbol = 100 modulator.carrier_freq_hz = 55e3 # yappi.start() self.network_sdr_plugin_sender.send_raw_data(modulator.modulate(msg_a.encoded_bits), 1) time.sleep(0.5) # send some zeros to simulate the end of a message self.network_sdr_plugin_sender.send_raw_data(np.zeros(self.num_zeros_for_pause, dtype=np.complex64), 1) time.sleep(0.5) receive_process.join(15) logger.info("PROCESS TIME: {0:.2f}ms".format(elapsed.value)) # self.assertEqual(current_index.value, target_num_samples) self.assertLess(elapsed.value, 200) # timeout = spy.wait(2000) # yappi.get_func_stats().print_all() # yappi.get_thread_stats().print_all()
self.ringbuffer.push(data) if __name__ == '__main__': from urh.dev.BackendHandler import BackendHandler from urh.signalprocessing.Message import Message from urh.signalprocessing.MessageType import MessageType from urh.signalprocessing.Modulator import Modulator from urh.util.Logger import logger import time endless_sender = EndlessSender(BackendHandler(), "HackRF") msg = Message([1, 0] * 16 + [1, 1, 0, 0] * 8 + [0, 0, 1, 1] * 8 + [1, 0, 1, 1, 1, 0, 0, 1, 1, 1] * 4, 0, MessageType("empty_message_type")) modulator = Modulator("test_modulator") modulator.samples_per_bit = 1000 modulator.carrier_freq_hz = 55e3 logger.debug("Starting endless sender") endless_sender.start() time.sleep(1) logger.debug("Pushing data") endless_sender.push_data(modulator.modulate(msg.encoded_bits)) logger.debug("Pushed data") time.sleep(5) logger.debug("Stopping endless sender") endless_sender.stop() time.sleep(1) logger.debug("bye")
def test_performance(self): self.form = MainController() self.cfc = self.form.compare_frame_controller self.stc = self.form.simulator_tab_controller self.gtc = self.form.generator_tab_controller self.form.add_signalfile(get_path_for_data_file("esaver.coco")) self.sframe = self.form.signal_tab_controller.signal_frames[0] self.sim_frame = self.form.simulator_tab_controller self.form.ui.tabWidget.setCurrentIndex(3) self.cfc.proto_analyzer.auto_assign_labels() self.network_sdr_plugin_sender = NetworkSDRInterfacePlugin(raw_mode=True) part_a = Participant("Device A", shortname="A", color_index=0) part_b = Participant("Device B", shortname="B", color_index=1) part_b.simulate = True self.form.project_manager.participants.append(part_a) self.form.project_manager.participants.append(part_b) self.form.project_manager.project_updated.emit() sniffer = ProtocolSniffer(100, 0.01, 0.1, 5, 1, NetworkSDRInterfacePlugin.NETWORK_SDR_NAME, BackendHandler(), network_raw_mode=True) sender = EndlessSender(BackendHandler(), NetworkSDRInterfacePlugin.NETWORK_SDR_NAME) simulator = Simulator(self.stc.simulator_config, self.gtc.modulators, self.stc.sim_expression_parser, self.form.project_manager, sniffer=sniffer, sender=sender) pause = 100 msg_a = SimulatorMessage(part_b, [1, 0] * 16 + [1, 1, 0, 0] * 8 + [0, 0, 1, 1] * 8 + [1, 0, 1, 1, 1, 0, 0, 1, 1, 1] * 4, pause=pause, message_type=MessageType("empty_message_type"), source=part_a) msg_b = SimulatorMessage(part_a, [1, 0] * 16 + [1, 1, 0, 0] * 8 + [1, 1, 0, 0] * 8 + [1, 0, 1, 1, 1, 0, 0, 1, 1, 1] * 4, pause=pause, message_type=MessageType("empty_message_type"), source=part_b) self.stc.simulator_config.add_items([msg_a, msg_b], 0, None) self.stc.simulator_config.update_active_participants() port = self.get_free_port() sniffer = simulator.sniffer sniffer.rcv_device.set_server_port(port) self.network_sdr_plugin_sender.client_port = port sender = simulator.sender port = self.get_free_port() sender.device.set_client_port(port) sender.device._VirtualDevice__dev.name = "simulator_sender" current_index = Value("L") elapsed = Value("f") target_num_samples = 13600 + pause receive_process = Process(target=receive, args=(port, current_index, target_num_samples, elapsed)) receive_process.daemon = True receive_process.start() # Ensure receiver is running time.sleep(2) # spy = QSignalSpy(self.network_sdr_plugin_receiver.rcv_index_changed) simulator.start() modulator = Modulator("test_modulator") modulator.samples_per_bit = 100 modulator.carrier_freq_hz = 55e3 # yappi.start() self.network_sdr_plugin_sender.send_raw_data(modulator.modulate(msg_a.encoded_bits), 1) time.sleep(0.5) # send some zeros to simulate the end of a message self.network_sdr_plugin_sender.send_raw_data(np.zeros(self.num_zeros_for_pause, dtype=np.complex64), 1) time.sleep(0.5) receive_process.join(15) logger.info("PROCESS TIME: {0:.2f}ms".format(elapsed.value)) # self.assertEqual(current_index.value, target_num_samples) self.assertLess(elapsed.value, 200) # timeout = spy.wait(2000) # yappi.get_func_stats().print_all() # yappi.get_thread_stats().print_all()
def push_data(self, data: np.ndarray): self.ringbuffer.push(data) if __name__ == '__main__': from urh.dev.BackendHandler import BackendHandler from urh.signalprocessing.Message import Message from urh.signalprocessing.MessageType import MessageType from urh.signalprocessing.Modulator import Modulator from urh.util.Logger import logger import time endless_sender = EndlessSender(BackendHandler(), "HackRF") msg = Message([1, 0] * 16 + [1, 1, 0, 0] * 8 + [0, 0, 1, 1] * 8 + [1, 0, 1, 1, 1, 0, 0, 1, 1, 1] * 4, 0, MessageType("empty_message_type")) modulator = Modulator("test_modulator") modulator.samples_per_bit = 1000 modulator.carrier_freq_hz = 55e3 logger.debug("Starting endless sender") endless_sender.start() time.sleep(1) logger.debug("Pushing data") endless_sender.push_data(modulator.modulate(msg.encoded_bits)) logger.debug("Pushed data") time.sleep(5) logger.debug("Stopping endless sender") endless_sender.stop() time.sleep(1) logger.debug("bye")
def test_performance(self): part_a = Participant("Device A", shortname="A", color_index=0) part_b = Participant("Device B", shortname="B", color_index=1) part_b.simulate = True self.form.project_manager.participants.append(part_a) self.form.project_manager.participants.append(part_b) self.form.project_manager.project_updated.emit() sniffer = ProtocolSniffer(100, 0.01, 0.001, 5, 1, NetworkSDRInterfacePlugin.NETWORK_SDR_NAME, BackendHandler(), network_raw_mode=True, real_time=True) sender = EndlessSender(BackendHandler(), NetworkSDRInterfacePlugin.NETWORK_SDR_NAME) simulator = Simulator(self.stc.simulator_config, self.gtc.modulators, self.stc.sim_expression_parser, self.form.project_manager, sniffer=sniffer, sender=sender) msg_a = SimulatorMessage(part_b, [1, 0] * 16 + [1, 1, 0, 0] * 8 + [0, 0, 1, 1] * 8 + [1, 0, 1, 1, 1, 0, 0, 1, 1, 1] * 4, 100000, MessageType("empty_message_type"), source=part_a) msg_b = SimulatorMessage(part_a, [1, 0] * 16 + [1, 1, 0, 0] * 8 + [1, 1, 0, 0] * 8 + [1, 0, 1, 1, 1, 0, 0, 1, 1, 1] * 4, 100000, MessageType("empty_message_type"), source=part_b) self.stc.simulator_config.add_items([msg_a, msg_b], 0, None) self.stc.simulator_config.update_active_participants() port = self.__get_free_port() sniffer = simulator.sniffer sniffer.rcv_device.set_server_port(port) self.network_sdr_plugin_sender.client_port = port sender = simulator.sender port = self.__get_free_port() sender.device.set_client_port(port) sender.device._VirtualDevice__dev.name = "simulator_sender" current_index = Value("L") elapsed = Value("f") target_num_samples = 113600 receive_process = Process(target=receive, args=(port, current_index, target_num_samples, elapsed)) receive_process.daemon = True receive_process.start() # Ensure receiver is running time.sleep(1) # spy = QSignalSpy(self.network_sdr_plugin_receiver.rcv_index_changed) simulator.start() modulator = Modulator("test_modulator") modulator.samples_per_bit = 100 modulator.carrier_freq_hz = 55e3 modulator.modulate(msg_a.encoded_bits) # yappi.start() self.network_sdr_plugin_sender.send_raw_data( modulator.modulated_samples, 1) QTest.qWait(100) receive_process.join(10) print("PROCESS TIME: {0:.2f}ms".format(elapsed.value)) self.assertEqual(current_index.value, target_num_samples) self.assertLess(elapsed.value, 200)