__author__ = 'shenojia'
import pprint
import re
import sys
import unittest
import sys
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.axis import *
from matplotlib.axes import *

sys.path.insert(0, '..')
from R12_math_util.gold import *
from R12_math_util.pn import *

class Test_Gold(unittest.TestCase):
    @unittest.skip("test_new")
    def test_new(self):
        print('hello gold')


    #@unittest.skip("test_pn")
    def test_pn(self):
        c = PN(12, 20)

        r__l_n__s  = np.array(np.zeros(6), dtype=np.complex)

        for m in range(6):
            r__l_n__s[m] = np.complex((1/np.sqrt(2))*(1-2*c[2*m]) , (1/np.sqrt(2))*(1-2*c[2*m+1]))
        print(r__l_n__s)


    @unittest.skip("defaults")
    def test_default(self):
        """Main Program"""
        nbits = 31
        print(nbits)
        if nbits != None:
            g = gold(nbits)
            #plotting
            pylab.figure()
            pylab.subplot(2,2,1)
            pylab.title('Autocorrelation g[0]')
            g0 = numpy.where(g[0], 1.0, -1.0)
            pylab.plot((numpy.roll(filter.ccorr(g0, g0).real, len(g0)/2-1)))
            pylab.xlim(0, len(g0))
            pylab.subplot(2,2,2)
            pylab.title('Autocorrelation g[-1]')
            gm1 = numpy.where(g[-1], 1.0, -1.0)
            pylab.plot((numpy.roll(filter.ccorr(gm1, gm1).real, len(gm1)/2-1)))
            pylab.xlim(0, len(gm1))
            pylab.subplot(2,2,3)
            pylab.title('Crosscorrelation g[0] g[1]')
            g1 = numpy.where(g[1], 1.0, -1.0)
            pylab.plot((numpy.roll(filter.ccorr(g0, g1).real, len(g0)/2-1)))
            pylab.xlim(0, len(g0))
            pylab.subplot(2,2,4)
            pylab.title('Crosscorrelation g[0] g[-1]')
            pylab.plot((numpy.roll(filter.ccorr(g0, gm1).real, len(g0)/2-1)))
            pylab.xlim(0, len(g0))
            pylab.show()

    @unittest.skip("test_paper_eg")
    def test_paper_eg(self):
        """Gold code set example base on paper by E. H. Dinan and B. Jabbari
        Spreading Codes for Direct Sequence CDMA and Wideband CDMA Cellular
        Networks"""
        seq1 = mls.lfsr([2],[1, 1, 1, 1, 1])
        print( 'Sequence 1:', numpy.where(seq1, 1, 0))
        seq2 = mls.lfsr([1, 2, 3], [1, 1, 1, 1, 1])
        print( 'Sequence 2:', numpy.where(seq2, 1, 0))
        gold = gen_gold(seq1, seq2)
        print( 'Gold 0 shift combination:', numpy.where(gold[0], 1, 0))
        print( 'Gold 1 shift combination:', numpy.where(gold[1], 1, 0))
        print( 'Gold 30 shift combination:', numpy.where(gold[-1], 1, 0))

        pylab.figure()
        pylab.subplot(2,2,1)
        pylab.title('Autocorrelation gold[0]')
        # g0 = numpy.where(gold[0], 1.0, -1.0)
        # pylab.plot((numpy.roll(filter.ccorr(g0, g0).real, len(g0)/2-1)))
        # pylab.subplot(2,2,2)
        pylab.title('Autocorrelation gold[30]')
        g30 = numpy.where(gold[30], 1.0, -1.0)
        # pylab.plot((numpy.roll(filter.ccorr(g30, g30).real, len(g30)/2-1)))
        # pylab.subplot(2,2,3)
        # pylab.title('Crosscorrelation gold[0] gold[1]')
        g1 = numpy.where(gold[1], 1.0, -1.0)
        # pylab.plot((numpy.roll(filter.ccorr(g0, g1).real, len(g0)/2-1)))
        # pylab.subplot(2,2,4)
        # pylab.title('Crosscorrelation gold[0] gold[30]')
        # pylab.plot((numpy.roll(filter.ccorr(g0, g30).real, len(g0)/2-1)))
        pylab.show()


    @unittest.skip("test_web_eg")
    def test_web_eg(self):
        """Example of producing Gold Codes from the net
        (http://paginas.fe.up.pt/~hmiranda/cm/Pseudo_Noise_Sequences.pdf)"""
        seq1 = mls.lfsr([1],[1,0,0])
        print( 'Sequence 1:', numpy.where(seq1, 1, 0))
        seq2 = mls.lfsr([2], [1,0,0])
        print( 'Sequence 2:', numpy.where(seq2, 1, 0))
        for gold in gen_gold(seq1, seq2):
            print( 'Gold Code:', numpy.where(gold, 1, 0))




if __name__ == '__main__':
    unittest.main()