# should only happen for audio channel
        print "I didn't get any samples; is your microphone or speaker OFF?"
        sys.exit(1)
#################################

    # process the received samples
    # make receiver
    r = Receiver(fc, opt.samplerate, opt.spb)
    demod_samples = r.demodulate(samples_rx)
    one, zero, thresh = r.detect_threshold(demod_samples)
    barker_start = r.detect_preamble(demod_samples, thresh, one)
    rcdbits = r.demap_and_check(demod_samples, barker_start)
    decoded_bits = r.decode(rcdbits)

    # push into sink
    sink = Sink()
    rcd_payload = sink.process(decoded_bits)

    if len(rcd_payload) > 0:
        hd, err = common_srcsink.hamming(decoded_bits, databits)
        print 'Hamming distance for payload at frequency', fc,'Hz:', hd, 'BER:', err
    else:
        print 'Could not recover transmission.'

    if opt.graph:
                len_mod = len(mod_samples) - opt.spb*opt.silence
                len_demod = len_mod - opt.spb*(len(src_payload) - len(rcd_payload))
                plot_graphs(mod_samples, samples_rx[barker_start:], demod_samples[barker_start:barker_start + len_demod], opt.spb, src.srctype, opt.silence)


Example #2
0
        # should only happen for audio channel
        print "I didn't get any samples; is your microphone or speaker OFF?"
        sys.exit(1)
#################################

    # process the received samples
    # make receiver
    r = Receiver(fc, opt.samplerate, opt.spb)
    demod_samples = r.demodulate(samples_rx)
    one, zero, thresh = r.detect_threshold(demod_samples)
    barker_start = r.detect_preamble(demod_samples, thresh, one)
    rcdbits = r.demap_and_check(demod_samples, barker_start)
    decoded_bits = r.decode(rcdbits)

    # push into sink
    sink = Sink()
    rcd_payload = sink.process(databits)#sink.process(decoded_bits)
    
    if len(rcd_payload) > 0:
        hd, err = common_srcsink.hamming(decoded_bits, databits)
        print 'Hamming distance for payload at frequency', fc,'Hz:', hd, 'BER:', err
    else:
        print 'Could not recover transmission.'

    if opt.graph:
                len_mod = len(mod_samples) - opt.spb*opt.silence 
                len_demod = len_mod - opt.spb*(len(src_payload) - len(rcd_payload))
                plot_graphs(mod_samples, samples_rx[barker_start:], demod_samples[barker_start:barker_start + len_demod], opt.spb, src.srctype, opt.silence)


Example #3
0
        # should only happen for audio channel
        print "I didn't get any samples; is your microphone or speaker OFF?"
        sys.exit(1)
#################################

    # process the received samples
    # make receiver
    r = Receiver(fc, opt.samplerate, opt.spb)
    demod_samples = r.demodulate(samples_rx)
    one, zero, thresh = r.detect_threshold(demod_samples)
    barker_start = r.detect_preamble(demod_samples, thresh, one)
    rcdbits = r.demap_and_check(demod_samples, barker_start)

    # push into sink
    sink = Sink()

    rcd_payload = sink.process(rcdbits)
    
    if len(rcd_payload) > 0:
        hd, err = common_srcsink.hamming(rcd_payload, src_payload)
        print 'Hamming distance for payload at frequency', fc,'Hz:', hd, 'BER:', err
    else:
        print 'Could not recover transmission.'

    if opt.graph:
                len_mod = len(mod_samples) - opt.spb*opt.silence 
                len_demod = len_mod - opt.spb*(len(src_payload) - len(rcd_payload))
                plot_graphs(mod_samples, samples_rx[barker_start:], demod_samples[barker_start:barker_start + len_demod], opt.spb, src.srctype, opt.silence)


Example #4
0
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)
Example #5
0
from sink import Sink
from source import Source
import numpy as np
from common_srcsink import hamming

compress = True
sou = Source(1, "testfiles/columns.png", compress)
sink = Sink(compress)

a, b, c = sou.process()
srcbits = sink.process(c)


# #testArr = np.array([1, 1, 1, 0, 0, 0, 0, 0])
# testArr = sou.text2bits("testfiles/Time.txt")

# statistics_bits, encoded_bits  = sou.huffman_encode(testArr)
# print len(encoded_bits)

# print "Encoded bits", encoded_bits
# print

# sink = Sink(1)

# srcbits = sink.huffman_decode(encoded_bits, statistics_bits)

# text = sink.bits2text(srcbits)


# print 
# print
Example #6
0
# audiocom library: Source and sink functions
import common_srcsink as common
import Image
from graphs import *
import binascii
import random
import os
import itertools
from source import Source
from sink import Sink

# Some tests

src = Source(1000, "testfiles/32pix.png")
payload, databits = src.process()

print "\n\n------------------\n\n"

sink = Sink()
print sink.process(payload)
Example #7
0
        print "I didn't get any samples; is your microphone or speaker OFF?"
        sys.exit(1)
#################################

    # process the received samples
    # make receiver
    r = Receiver(fc, opt.samplerate, opt.spb)
    demod_samples = r.demodulate(samples_rx)
    one, zero, thresh = r.detect_threshold(demod_samples)
    barker_start = r.detect_preamble(demod_samples, thresh, one)
    rcdbits = r.demap_and_check(demod_samples, barker_start)
    
    # push into sink
    sink = Sink()
    rcd_payload = sink.process(rcdbits)
    """
    sink = Sink()
    rcd_payload = sink.process(src_payload)
    if len(rcd_payload) > 0:
        hd, err = common_srcsink.hamming(rcd_payload, src_payload)
        print 'Hamming distance for payload at frequency', fc,'Hz:', hd, 'BER:', err
    else:
        print 'Could not recover transmission.'

    if opt.graph:
                len_mod = len(mod_samples) - opt.spb*opt.silence 
                len_demod = len_mod - opt.spb*(len(src_payload) - len(rcd_payload))
                plot_graphs(mod_samples, samples_rx[barker_start:], demod_samples[barker_start:barker_start + len_demod], opt.spb, src.srctype, opt.silence)


Example #8
0
    except ZeroDivisionError:
        # should only happen for audio channel
        print "I didn't get any samples; is your microphone or speaker OFF?"
        sys.exit(1)
#################################

# process the received samples
# make receiver
    r = Receiver(fc, opt.samplerate, opt.spb)
    demod_samples = r.demodulate(samples_rx)
    one, zero, thresh = r.detect_threshold(demod_samples)
    barker_start = r.detect_preamble(demod_samples, thresh, one)
    rcdbits = r.demap_and_check(demod_samples, barker_start)

    # push into sink
    sink = Sink()
    rcd_payload = sink.process(rcdbits)

    if len(rcd_payload) > 0:
        hd, err = common_srcsink.hamming(rcd_payload, src_payload)
        print 'Hamming distance for payload at frequency', fc, 'Hz:', hd, 'BER:', err
    else:
        print 'Could not recover transmission.'

    if opt.graph:
        len_mod = len(mod_samples) - opt.spb * opt.silence
        len_demod = len_mod - opt.spb * (len(src_payload) - len(rcd_payload))
        plot_graphs(mod_samples, samples_rx[barker_start:],
                    demod_samples[barker_start:barker_start + len_demod],
                    opt.spb, src.srctype, opt.silence)
Example #9
0
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.
    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)

    # Create the channel
    if config.bypass:
        channel = AbstractChannel(config.bypass_noise, config.bypass_h, config.bypass_lag)
    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)

        # Plot graphs if necessary
        if config.graphs:
            try:
                len_demod = config.spb * (len(received_payload) + preamble.preamble_data_len())
            except:
                # If we didn't receive the payload, make a reasonable guess for the number of bits
                # (won't work for filetype, where n_bits changes)
                len_demod = config.spb * (config.n_bits + preamble.preamble_data_len())

            if config.demod_type == Receiver.QUADRATURE:
                filtered = r.graph_info.demod_samples
                graphs.plot_sig_spectrum(samples_rx, filtered, "received samples", "filtered samples")
            elif config.src_type == Source.U:
                demod_samples = r.graph_info.demod_samples
                plotrange = preamble.preamble_data_len()*config.spb
                graphs.plot_samples(demod_samples[plotrange:len_demod], 'unit-step response', show=True)
            else:
                graphs.plot_graphs(r.graph_info.demod_samples[:len_demod], r.graph_info.hist_samples[:len_demod], config.spb, preamble)
Example #10
0
        print "I didn't get any samples; is your microphone or speaker OFF?"
        sys.exit(1)
#################################

    # process the received samples
    # make receiver
    r = Receiver(fc, opt.samplerate, opt.spb)
    demod_samples = r.demodulate(samples_rx)
    one, zero, thresh = r.detect_threshold(demod_samples)
    barker_start = r.detect_preamble(demod_samples, thresh, one)
    rcdbits = r.demap_and_check(demod_samples, barker_start)
    if opt.cc_len!=0:
        rcdbits = r.decode(rcdbits)

    # push into sink
    sink = Sink(opt.compress)
    sink_bits = sink.process(rcdbits)
    
    if len(sink_bits) > 0:
        hd, err = common_srcsink.hamming(sink_bits, src_bits)
        print 'Hamming distance for payload at frequency', fc,'Hz:', hd, 'BER:', err
    else:
        print 'Could not recover transmission.'

    if opt.graph:
        len_mod = len(mod_samples) - opt.spb*opt.silence 
        len_demod = len_mod - opt.spb*(len(src_payload) - len(rcd_payload))
        plot_graphs(mod_samples, samples_rx[barker_start:], demod_samples[barker_start:barker_start + len_demod], opt.spb, src.srctype, opt.silence)


Example #11
0
        # should only happen for audio channel
        print "I didn't get any samples; is your microphone or speaker OFF?"
        sys.exit(1)
#################################

    # process the received samples
    # make receiver
    r = Receiver(fc, opt.samplerate, opt.spb)
    demod_samples = r.demodulate(samples_rx)

    one, zero, thresh = r.detect_threshold(demod_samples)
    barker_start = r.detect_preamble(demod_samples, thresh, one)
    rcdbits = r.demap_and_check(demod_samples, barker_start)
    if opt.cc_len!=0:
        rcdbits = r.decode(rcdbits)

    # push into sink
    sink = Sink(opt.compress, opt.encrypt)
    sink_bits = sink.process(rcdbits, pubkey)
    
    if len(sink_bits) > 0:
        hd, err = common_srcsink.hamming(sink_bits, src_bits)
        print 'Hamming distance for payload at frequency', fc,'Hz:', hd, 'BER:', err
    else:
        print 'Could not recover transmission.'

    if opt.graph:
        len_mod = len(mod_samples) - opt.spb*opt.silence 
        len_demod = len_mod - opt.spb*(len(src_payload) - len(rcd_payload))
        plot_graphs(mod_samples, samples_rx[barker_start:], demod_samples[barker_start:barker_start + len_demod], opt.spb, src.srctype, opt.silence)
Example #12
0
def run(config):
    '''Primary Audiocom functionality.'''

    # Create the preamble to pre-pend to the transmission
    preamble = Preamble(config)

    # Create a source
    source = Source(config)
    frequency = config.channel
    print "Channel: %d Hz" % frequency
    print "\n".join(["\t%s" % source])

    # Create the Sender for this Source.  Process the bits to get
    # modulated samples.
    sender = Sender(frequency, preamble, config)
    sender.set_source(source)
    modulated_samples = sender.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'

    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)
        sink = Sink(source)
        received_payload = sink.process(bits)
        print "Received %d data bits" % len(received_payload)
 
        if len(received_payload) > 0:
            # Output BER
            hd = util.hamming(received_payload, source.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. ***'

    # Plot graphs if necessary
    if config.graphs:

        try:
            len_demod = config.spb * (len(received_payload) + preamble.preamble_data_len())
        except:
            # If we didn't receive the payload, make a reasonable guess for the number of bits
            len_demod = config.spb * (config.n_bits + preamble.preamble_data_len())

        if config.src_type == Source.U:
            demod_samples = r.graph_info.demod_samples
            plotrange = preamble.preamble_data_len()*config.spb
            graphs.plot_samples(demod_samples[plotrange:len_demod], 'unit-step response', show=True)

        else:
            graphs.plot_graphs(r.graph_info.demod_samples[:len_demod], r.graph_info.hist_samples[:len_demod], config.spb, preamble)
Example #13
0
        sys.exit(1)
#################################

# process the received samples
# make receiver
    r = Receiver(fc, opt.samplerate, opt.spb)
    demod_samples = r.demodulate(samples_rx)

    one, zero, thresh = r.detect_threshold(demod_samples)
    barker_start = r.detect_preamble(demod_samples, thresh, one)
    rcdbits = r.demap_and_check(demod_samples, barker_start)
    if opt.cc_len != 0:
        rcdbits = r.decode(rcdbits)

    # push into sink
    sink = Sink(opt.compress, opt.encrypt)
    sink_bits = sink.process(rcdbits, pubkey)

    if len(sink_bits) > 0:
        hd, err = common_srcsink.hamming(sink_bits, src_bits)
        print 'Hamming distance for payload at frequency', fc, 'Hz:', hd, 'BER:', err
    else:
        print 'Could not recover transmission.'

    if opt.graph:
        len_mod = len(mod_samples) - opt.spb * opt.silence
        len_demod = len_mod - opt.spb * (len(src_payload) - len(rcd_payload))
        plot_graphs(mod_samples, samples_rx[barker_start:],
                    demod_samples[barker_start:barker_start + len_demod],
                    opt.spb, src.srctype, opt.silence)
Example #14
0
        samples_rx = channel.xmit_and_recv(mod_samples)
    except ZeroDivisionError:
        # should only happen for audio channel
        print "I didn't get any samples; is your microphone or speaker OFF?"
        sys.exit(1)
#################################

    # process the received samples
    # make receiver
    r = Receiver(fc, opt.samplerate, opt.spb)
    demod_samples = r.demodulate(samples_rx)
    one, zero, thresh = r.detect_threshold(demod_samples)
    barker_start = r.detect_preamble(demod_samples, thresh, one)
    rcdbits = r.demap_and_check(demod_samples, barker_start)

    # push into sink
    sink = Sink()
    #rcd_payload = sink.process(rcdbits)
    rcd_payload = sink.process(databits)

    if len(rcd_payload) > 0:
        hd, err = common_srcsink.hamming(rcd_payload, src_payload)
        print 'Hamming distance for payload at frequency', fc,'Hz:', hd, 'BER:', err
    else:
        print 'Could not recover transmission.'

    if opt.graph:
                len_mod = len(mod_samples) - opt.spb*opt.silence 
                len_demod = len_mod - opt.spb*(len(src_payload) - len(rcd_payload))
                plot_graphs(mod_samples, samples_rx[barker_start:], demod_samples[barker_start:barker_start + len_demod], opt.spb, src.srctype, opt.silence)