Exemple #1
0
def test1():
    '''
    Runs the spectral factorization routine on a few trivial examples
    and plots the results.
    '''
    #Specify zeros of the filter which produces the PSD
    z1 = 0.5
    z2 = -0.5 + 0.75j
    z3 = 0.99 + 0.01j
    H1_z, P1_z = [z1], [z1, 1/z1]
    H2_z, P2_z = [z1, z2, z2.conjugate()], [z1, 1/z1, z2, 1/z2,
                                            z2.conjugate(),
                                            1/z2.conjugate()]
    H3_z, P3_z = [z3, z3.conjugate()], [z3, 1/z3, z3.conjugate(),
                                        1/z3.conjugate()]

    for H_z, P_z in [(H1_z, P1_z), (H2_z, P2_z), (H3_z, P3_z)]:
        P = poly(H_z)
        P = polymul(P[::-1], P) #Creates a valid PSD polynomial

        #Pass in only the causal portion
        Q, s = spectral_factorization(P[len(P)/2:])
        Q_zeros = roots(Q)

        fig = plt.figure()
        ax = fig.add_subplot(1,1,1)
        uc = patches.Circle((0, 0), radius = 1, fill = False,
                            color = 'black', ls = 'dashed')
        ax.add_patch(uc)
        plt.xlim([-3, 3])
        plt.ylim([-3, 3])
        plt.plot([z.real for z in P_z], [z.imag for z in P_z],
                 'b+', markersize = 12, mew = 3,
                 label = 'Roots of $P(z)$')
        plt.plot([z.real for z in Q_zeros], [z.imag for z in Q_zeros],
                 'rx', markersize = 12, mew = 3,
                 label = 'Roots of $\sigma Q(z)$')
        plt.legend()
        plt.gca().set_aspect('equal')  
        plt.title('Spectral Factorization')
        plt.xlabel('Real Part')
        plt.ylabel('Imaginary Part')
        plt.show()
    return
Exemple #2
0
def test2():
    '''
    Performs a number of randomized tests with visualizations
    '''
    N = 10 #Number of tests
    p = 7 #Number of roots
    np.random.seed(1)
    for i in range(N):
        K = stats.expon.rvs(scale = 5)
        b, H = random_arma(p, 0, k = K, p_radius = 1) #Random filter
        H = b[0]*H
        
        P = polymul(H[::-1], H) #Creates a valid PSD polynomial

        #Pass in only the causal portion
        Q, s = spectral_factorization(P[len(P)/2:])

        Q_zeros = roots(Q)
        P_zeros = roots(P)

        fig = plt.figure()
        ax = fig.add_subplot(1,1,1)
        uc = patches.Circle((0, 0), radius = 1, fill = False,
                            color = 'black', ls = 'dashed')
        ax.add_patch(uc)
        plt.xlim([-3, 3])
        plt.ylim([-3, 3])
        plt.plot([z.real for z in P_zeros], [z.imag for z in P_zeros],
                 'b+', markersize = 12, mew = 3,
                 label = 'Roots of $P(z)$')
        plt.plot([z.real for z in Q_zeros], [z.imag for z in Q_zeros],
                 'rx', markersize = 12, mew = 3,
                 label = 'Roots of $\sigma Q(z)$')
        plt.legend()
        plt.title('Spectral Factorization')
        plt.xlabel('Real Part')
        plt.ylabel('Imaginary Part')
        plt.gca().set_aspect('equal')  
        plt.show()
    
    return
Exemple #3
0
def test3():
    '''
    Performs randomized tests with less visualization.
    '''
    N = 100 #Number of tests
    p = [3, 7, 11, 16, 21, 35, 70, 141, 211] #Number of roots
    np.random.seed(1)
    #Tolerances for our checks
    tols = [1./(10**i) for i in range(1, 10)]
    factorization_fails = np.array([0]*len(p))

    for p_index, num_roots in enumerate(p):
        if prog_bar:
            pbar = ProgressBar(widgets = ('p = %d ' % num_roots, 
                                          Bar(), Percentage(),
                                          ETA()), maxval = N).start()
        num_fails = np.array([0]*len(tols))
        for trial_num in range(N):
            if prog_bar:
                pbar.update(trial_num)
            K = stats.expon.rvs(scale = 5) #gain term
            b, H = random_arma(num_roots, 0, k = K, p_radius = 1) #Random filter
            H = b*H

            #Creates a valid PSD polynomial
            #Only the causal portion is needed
            P = polymul(H[::-1], H)

            try:
                Q, s = spectral_factorization(P[len(P)/2:])
            except FactorizationError:
                factorization_fails[p_index] += 1
                num_fails += 1
                continue

            Q_zeros = np.sort(roots(Q))
            P_minphase_zeros = np.sort(roots(H))
            
            for (atol_i, atol) in enumerate(tols):
                if not (np.allclose(Q_zeros, P_minphase_zeros,
                                    rtol = 0, atol = atol) and
                        np.isclose(K, s, rtol = 0, atol = atol)):
                    num_fails[atol_i] += 1

        try:
            total_fails = np.vstack((total_fails, num_fails))

        except NameError:
            total_fails = num_fails

        if prog_bar:
            pbar.finish()

    total_fails = total_fails / float(N) #Make everything a ratio
    if tab:
        print 'Fraction of time (over %d trials) accuracy not within... '\
            '(oo indicates a failure to return a valid factorization)' % N
        headers = ['p', 'oo'] + tols
        table = []
        for i, pi in enumerate(p):
            row_i = [pi] + [factorization_fails[i]] + list(total_fails[i])
            table.append(row_i)
        print tabulate(table, headers = headers)
        results_file = open('test_results.txt', 'w')
        results_file.write('Fraction of time (over %d trials) accuracy '\
                           'not within...\n(oo indicates a failure to '\
                           'return a valid factorization)\n' % N)
        results_file.write(tabulate(table, headers = headers))
        results_file.write('\n')
        results_file.close()
    return