# template for Lab #2, Task #5
import numpy,random
import matplotlib.pyplot as p
import channel
reload(channel)
import im1d
reload(im1d)
import lab2
reload(lab2)
import lab1_1
reload(lab1_1)
import lab2_2
reload(lab2_2)

# turn on interactive mode, useful if we're using ipython
p.ion()  

# arguments:
#   samples -- numpy array of received voltage samples
#   h -- numpy array as returned by unit_sample_response
# return value
#   numpy array of deconvolved voltage samples
def deconvolver(samples,h):
    """
    Take the samples that are the output from a channel (y), and
    the channel's unit-sample response (h), deconvolve the
    samples, and return the reconstructed samples.  Be sure the
    length of the reconstructed samples is the same as the length
    of the input samples.
    """
    #pass  # your code here...

if __name__ == '__main__':

    # Create two noise free channels
    mychannel1 = channel.channel(channelid='1',noise=0.0)
    mychannel2 = channel.channel(channelid='2',noise=0.0)

    # Compute the channel unit sample responses of the
    # noise-free channels
    h1 = lab2_2.unit_sample_response(mychannel1)
    h2 = lab2_2.unit_sample_response(mychannel2)

    # Generate a sequence of test samples
    samples = numpy.sin(2*numpy.pi*0.01*numpy.array(range(100)))
    samples[0:len(samples)/2] += 1.0
    samples[len(samples)/2:] += -1.0
    maxs = max(samples)
    mins = min(samples)
    samples -= mins  # Make samples positive
    samples *= 1.0/(maxs - mins) # Scale between zero and one

    # Test deconvolver
    lab2.demo_deconvolver(samples,mychannel1,h1,deconvolver)
    lab2.demo_deconvolver(samples,mychannel2,h2,deconvolver)

    """
    #(Uncomment when ready to test) on a bigger data set.
    # Read in a .png image as array of greyscale values
    # between 0.0 and 1.0

    image1 = im1d.im1dread("mandril")
    image2 = im1d.im1dread("brobot")

    lab2.demo_deconvolver(image1,mychannel1,h1,deconvolver,
                          images=True)
    lab2.demo_deconvolver(image2,mychannel2,h2,deconvolver,
                          images=True)
    """

    # when ready for checkoff, enable the following line
    #lab2.checkoff(deconvolver,'L2_5')