def test_channel(channel):
    """
    create a test waveform and plot both it and the output of
    passing the waveform through the channel.
    """
    name = channel.__name__
    message = [1, 0, 1, 0, 1, 0, 1, 0]
    inp = lab2_1.transmit(message, 10, 100)
    out = channel(inp)
    upper = max(max(inp), max(out))
    lower = min(min(inp), min(out))
    upper = upper + abs(upper) * 0.1  # leave some space at the edges, unless that edge is 0
    lower = lower - abs(lower) * 0.1

    p.figure()
    p.suptitle(name, fontsize=20, fontweight="bold")
    p.subplot(211)
    p.axis([0, len(inp), lower, upper])  # custom axis scaling
    p.plot(inp, "g-")
    p.title("Input")
    p.grid(True)
    p.ylabel("Voltage")
    p.subplot(212)
    p.axis([0, len(inp), lower, upper])  # make the axes the same
    p.plot(out, "b-")
    p.title("Output")
    p.grid(True)
    p.ylabel("Voltage")
    p.xlabel("Sample Number")
    p.savefig(name + ".png")
    p.clf()  # clears the figure so we can start blank again
def testDeconvolver(channel,noise=0.):
	# generates a random 8 bit message
    message = lab2_5.randomMessage(100)
    # generates samples from the bit sequence
    inp = numpy.array(lab2_1.transmit(message,0,100))
    # passes the samples through channel
    out = channel(inp,noise)
    # uses the deconvolver to reconstruct the input
    dMessage = deconvolver(out,lab2_3.unit_sample_response(channel))
    p.figure()	# plot
    p.suptitle('Random Message through '+channel.__name__,fontsize = 20,fontweight='bold')
    plot_samples(311, inp, len(inp), -.1, 1.1, 'Input')
    plot_samples(312, out, len(out), -.1, 1.1, 'Output', 'g-')
    plot_samples(313, dMessage, len(dMessage), -.1, 1.1, 'Deonvolved Result', 'r-')
    p.subplots_adjust(hspace=0.5)
    if noise == 0.:
    	p.savefig(channel.__name__+'Deconvolution.png')
    else:
    	p.savefig(channel.__name__+'Noise'+str(noise)+'Deconvolution.png')
    p.clf()
def transmitMessage(channel):
	# compute the output of the channel when transmitting the message
	# 10101010 with a 10 sample preamble and 100 sample postamble
	message = [1,0,1,0,1,0,1,0]
	inp = lab2_1.transmit(message,10,100)
	return channel(inp)
def predictChannelOutput(channel):
	message = [1,0,1,0,1,0,1,0]
	return convolutionSum(lab2_1.transmit(message,10,100),lab2_3.unit_sample_response(channel))
def transmitMessage(message,channel,samples_per_bit):
	inp = lab2_1.transmit(message,0,0,samples_per_bit)
	return channel(inp)