def run(config): '''Primary Audiocom functionality.''' # Create the preamble to pre-pend to the transmission preamble = Preamble(config) # Create the sources sources = {} for i in range(len(config.channels)): frequency = config.channels[i] source = Source(config, i) print("Channel: %d Hz" % frequency) print("\n".join(["\t%s" % source])) sources[frequency] = source # Create a sender for each source, so we can process the bits to # get the modulated samples. We combine all of the modulated # samples into a single array by adding them. baseband_samples = [] modulated_samples = [] for frequency in sources: src = sources[frequency] sender = Sender(frequency, preamble, config) sender.set_source(src) modulated_samples = util.add_arrays(sender.modulated_samples(), modulated_samples) baseband_samples = util.add_arrays(sender.bits_to_samples(src.payload), baseband_samples) print("sending %d samples" % len(modulated_samples)) # Create the channel if config.bypass: channel = AbstractChannel(config.bypass_noise, config.bypass_h) else: channel = AudioChannel(config) # Transmit and receive data on the channel. The received samples # (samples_rx) have not been processed in any way. samples_rx = channel.xmit_and_recv(modulated_samples) print('Received', len(samples_rx), 'samples') for frequency in config.channels: r = Receiver(frequency, preamble, config) try: # Call the main receiver function. The returned array of bits # EXCLUDES the preamble. bits = r.process(samples_rx) # Push into a Sink so we can convert back to a useful payload # (this step will become more useful when we're transmitting # files or images instead of just bit arrays) src = sources[frequency] sink = Sink(src) received_payload = sink.process(bits) print("Received %d data bits" % len(received_payload)) if src.type == Source.TEXT: print("Received text was:", sink.received_text) if len(received_payload) > 0: # Output BER hd = util.hamming(received_payload, src.payload) ber = float(hd) / len(received_payload) print('BER:', ber) else: print('Could not recover transmission.') except Exception as e: # In general, this is a fatal exception. But we'd still like # to look at the graphs, so we'll continue with that output # print('*** ERROR: Could not detect preamble. ***') print(repr(e)) if config.graphs == "time": graphs.plot_samples(baseband_samples, modulated_samples, r.graph_info.received_samples, stems=False) elif config.graphs == "freq": graphs.plot_sig_spectrum(modulated_samples, r.graph_info.demod_samples) elif config.graphs == "usr": graphs.plot_usr(r.graph_info.demod_samples)
def run(config): # Create the sources sources = {} for i in range(len(config.channels)): frequency = config.channels[i] source = Source(config, i) print("Channel: %d Hz" % frequency) print("\n".join(["\t%s" % source])) sources[frequency] = source # Create a sender for each source, so we can process the bits to # get the modulated samples. We combine all of the modulated # samples into a single array by adding them. modulated_samples = [] baseband_samples = [] for frequency in sources: src = sources[frequency] sender = Sender(frequency, config) sender.set_source(src) modulated_samples = util.add_arrays(sender.modulated_samples(), modulated_samples) baseband_samples = util.add_arrays(sender.gain_samples(), baseband_samples) print("sending %d samples" % len(baseband_samples)) # Create the channel if config.bypass: channel = AbstractChannel(config.bypass_noise, config.bypass_h) else: channel = AudioChannel(config) # Transmit and receive data on the channel. The received samples # (samples_rx) have not been processed in any way. samples_rx = channel.xmit_and_recv(modulated_samples) print('Received', len(samples_rx), 'samples') for frequency in config.channels: r = Receiver(frequency, config) try: # Call the main receiver function. The returned array of bits # EXCLUDES the preamble. chopped_samples = r.process(samples_rx) except Exception as e: print(e) sys.exit() if config.graphs == "time": graphs.plot_samples(baseband_samples, modulated_samples, r.graph_info.received_samples, stems=False) elif config.graphs == "freq": graphs.plot_sig_spectrum(modulated_samples, r.graph_info.received_samples, title="Received Samples") if not config.bypass or (config.bypass and not config.know_h): h = util.recover_h(modulated_samples, chopped_samples, n_samples=1000) else: h = channel.h # filter h_prime = util.recover_h(h, numpy.array([1]), n_samples=1000) if config.graphs == "usr": recovered_samples = numpy.convolve(chopped_samples, h_prime) graphs.plot_usr(chopped_samples, h, recovered_samples, modulated_samples, stems=False)