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