Exemplo n.º 1
0
def matching_pursuit(samp):
    """
    Run matching pursuit, with time shifted signals in the dictionary.

    Abandoned because too slow, not fully tested.
    """
    R = samp.get_target()
    ret = []
    maxinn = 1
    while maxinn > 0:
        print("iter: " + str(len(ret)))
        maxinn = 0
        maxres = (-1, -1, -1)
        for i, s in enumerate(samp.components):
            print("file: " + str(i))
            for offset in range(len(R)):
                f = np.pad(samp.comp_to_signal(i), (offset, 0), 'constant')
                f, R = sample.pad(f, R)
                a = np.inner(R, f)  # np.inner uses dot product
                if (np.abs(a) < maxinn):
                    continue
                maxinn = a
                maxres = (i, offset, a)

        g = np.pad(samp.comp_to_signal(maxres[0]), (maxres[1], 0), 'constant')
        g = g * maxres[2]
        g, R = sample.pad(g, R)
        R = R - g
        R = np.trim_zeros(R, 'b')
        ret.append(maxres)
    return AlgResult(samp, ret)
Exemplo n.º 2
0
def matching_pursuit_mp(samp):
    """
    Run matching pursuit, with time shifted signals in the dictionary.

    Abandoned because too slow, not fully tested.

    """
    R = samp.get_target()
    ret = []
    maxinn = 1
    while maxinn > 0:
        print("iter: " + str(len(ret)))
        maxinn = 0
        maxres = (-1, -1, -1)
        mp_args = [(R, samp, i) for i in range(len(samp.components))]
        p = mp.Pool(config.POOL_SIZE)
        processresults = p.imap(_matching_pusuit_mp_in, mp_args)

        maxres = max(processresults, key=lambda x: x[2])
        g = np.pad(samp.comp_to_signal(maxres[0]), (maxres[1], 0), 'constant')
        g = g * maxres[2]
        R, g = sample.pad(R, g)
        R = R - g
        R = np.trim_zeros(R, 'b')
        ret.append(maxres)
    return AlgResult(samp, ret)
Exemplo n.º 3
0
 def to_signal(self):
     """Use the components and result from to create the result signal."""
     signal = np.ndarray((0))
     for c in self.a_result:
         s = np.pad(self.comp_to_signal(c[0]), (c[1], 0), 'constant') * c[2]
         signal, s = sample.pad(
             signal,
             s,
         )
         signal += s
     return signal
Exemplo n.º 4
0
def MSE_L2_time(results):
    """Mean Square Error where error is the L2 norm."""
    total = 0
    res_string = "\n\nSingle Sample L2 Norms:\n"
    for r in results:
        rsignal, tsignal = sample.pad(r.to_signal(), r.get_target())
        single = np.linalg.norm(tsignal - rsignal)
        res_string += r.s_name + ": " + single.__str__() + "\n"
        total += single**2
    total /= len(results)
    res_string = "total: " + total.__str__() + res_string
    return EvalResult("MSE_L2_time", res_string)
Exemplo n.º 5
0
def matching_pursuit2(samp):
    """
    Run matching pursuit, with time shifted signals in the dictionary.

    Imagine if I didn't forget about the convolution theorem
    and cross correlation when it is overly relevent.

    Normal test stop condition doesn't currently work
    because of floating point errors;
    need new one, but this is good enough to test for time
    """
    if config.VERBOSE:
        print("Matching Pursuit on " + samp.s_name)

    if config.PLOT:
        plotfile = open(
            config.RESULTS_DIR + "matching_pursuit2-" + samp.s_name + ".txt",
            'w')

    R = samp.get_target()
    ret = []
    maxinn = 1
    itt = 1
    while np.abs(maxinn) > 0 and itt <= config.N_SIGNALS:
        if config.VERBOSE:
            print("\tChoosing Signal " + str(itt))

        maxinn = 0
        maxres = (-1, -1, -1)
        for i, s in enumerate(samp.components):
            f = samp.get_signal(s)
            # convolving with 2nd arg reversed is cross correlation
            # cross correlation essentially sliding dot product
            inners = scipy.signal.fftconvolve(R, f[::-1], 'full')[f.size - 1:]
            offset = np.argmax(np.abs(inners))
            a = inners[offset]
            if np.abs(a) < np.abs(maxinn):
                continue
            maxinn = a
            maxres = (i, offset, a)

        g = np.pad(samp.comp_to_signal(maxres[0]), (maxres[1], 0), 'constant')
        g = g * maxres[2]
        R, g = sample.pad(R, g)
        R = R - g
        R = np.trim_zeros(R, 'b')
        if config.PLOT:
            plotfile.write(str(1 - np.linalg.norm(R)) + "\n")

        ret.append(maxres)
        itt += 1
    return AlgResult(samp, ret)
Exemplo n.º 6
0
def _matching_pusuit_mp_in(args):
    target, samp, i = args
    maxinn = 0
    sig = samp.comp_to_signal(i)
    for offset in range(len(target)):
        f = np.pad(sig, (offset, 0), 'constant')
        f, R = sample.pad(f, target)
        a = np.inner(R, f)  # np.inner uses dot product
        if (np.abs(a) < maxinn):
            continue
        maxinn = a
        maxres = (i, offset, a)

    return maxres
Exemplo n.º 7
0
def matching_pursuit2_mp(samp):
    """
    Run matching pursuit, with time shifted signals in the dictionary.

    Convolution again, but this time multiprocess because I am impatient.
    """
    if config.VERBOSE:
        print("Matching Pursuit on " + samp.s_name)

    if config.PLOT:
        plotfile = open(
            config.RESULTS_DIR + "matching_pursuit2-" + samp.s_name + ".txt",
            'w')

    R = samp.get_target()
    ret = []
    maxinn = 1
    itt = 1
    p = mp.Pool(config.POOL_SIZE)
    while np.abs(maxinn) > 0 and itt <= config.N_SIGNALS:
        if config.VERBOSE:
            print("\tChoosing Signal " + str(itt))

        maxinn = 0
        mp_args = [(R, samp, i) for i in range(len(samp.components))]

        processresults = p.starmap(_matching_pusuit2_mp_in, mp_args)

        maxres = max(processresults, key=lambda x: np.abs(x[2]))

        g = np.pad(samp.comp_to_signal(maxres[0]), (maxres[1], 0), 'constant')
        g = g * maxres[2]
        R, g = sample.pad(R, g)
        R = R - g
        R = np.trim_zeros(R, 'b')
        if config.PLOT:
            plotfile.write(str(1 - np.linalg.norm(R)) + "\n")
        ret.append(maxres)
        itt += 1
        maxinn = maxres[2]
    return AlgResult(samp, ret)