示例#1
0
def toFinite(fareyVector, N):
    '''
    Return the finite vector corresponding to the Farey vector provided for a given modulus/length N
    and the multiplicative inverse of the relevant Farey angle
    '''
    p, q = get_pq(fareyVector)
    coprime = nt.is_coprime(abs(q), N)
    prime = 1
    if N % 2 == 0:  #dyadic
        prime = 2
    qNeg = q  #important, if q < 0, preserve for minverse.
    if q < 0:
        q += N  #necessary for quadrants other than the first
    if p < 0:
        p += N  #necessary for quadrants other than the first
#    print("p:", p, "q:", q, "N:", N)

    mValue = 0
    inv = 1
    if coprime:
        inv = nt.minverse(qNeg, N)
        #        identity = (inv*q)%N
        mValue = (p * inv) % N
#        print("vec:", fareyVector, "m:", mValue, "inv:", inv)
    else:  #perp projection
        inv = nt.minverse(p, N)
        #        mValue = (q*inv)%N + N
        mValue = int((q * inv) % N / prime) + N


#        print("perp vec:", fareyVector, "m:", mValue, "inv:", inv)

    return mValue, inv
示例#2
0
def oversamplingFilter(n, M, p = 2):
    '''
    Produce the nxn oversampling filter that is needed to exactly filter dyadic DRT etc.
    This version returns values as multiplicative inverses (mod M) for use with the NTT
    '''
    gcd_table = np.zeros(n)
    gcdInv_table = np.zeros(n)

    gcd_table[0] = n + int(n)/p
    gcdInv_table[0] = nt.minverse(gcd_table[0], M)
    gcd_table[1] = 1
    gcdInv_table[1] = nt.minverse(gcd_table[1], M)
    for j in range(2,n):
        u, v, gcd_table[j] = nt.extended_gcd( int(j), int(n) )
        gcdInv_table[j] = nt.minverse(gcd_table[j], M)

    filter = nt.zeros((n,n))
    for j in range(0,n):
        for k in range(0,n):
            if gcd_table[j] < gcd_table[k]:
                filter[j,k] = nt.integer(gcdInv_table[j])
            else:
                filter[j,k] = nt.integer(gcdInv_table[k])

    return filter
示例#3
0
def remapProjections(p, rotation):
    '''
    For a given p size, a Farey set is generated and rotated by rotation vector provided.
    The new mapping is returned, with their corresponding new Farey vectors.
    '''
    original_angles = []
    rotated_angles = []
    ms = []
    inverses = []
    m_dashs = []
    for m in range(p):  #add the Farey angles corresponding to m values
        ms.append(m)
        #~ angle = complex(1.0, m)
        angle = complex(m, 1)
        original_angles.append(angle)
    ms.append(p)  #add the pth projection
    #~ original_angles.append( complex(0.0, 1.0) )
    original_angles.append(complex(1.0, 0.0))

    for angle in original_angles:
        rotated_angle = angle * rotation
        #~ rotated_angle = complex(angle.imag*rotation.imag - angle.real*rotation.real, angle.imag*rotation.real + rotation.imag*angle.real) #Imants' coordinate system
        #~ rotated_angle = complex(angle.imag*rotation.real + rotation.imag*angle.real, angle.imag*rotation.imag - angle.real*rotation.real)
        #~ rotated_angle = rotation*angle
        '''if abs(rotated_angle.real) >= p or abs(rotated_angle.imag) >= p: # only for coomparison, since negative values would be lost otherwise
            rotated_angle_rational = complex( int(rotated_angle.real)%p, int(rotated_angle.imag)%p )
        else:
            rotated_angle_rational = rotated_angle'''
        rotated_angle_rational = complex(
            int(rotated_angle.real + p) % p,
            int(rotated_angle.imag + p) % p)
        rotated_angles.append(rotated_angle_rational)

        #compute m'
        #~ u = nt.minverse(rotated_angle_rational.real, p)
        u = nt.minverse(rotated_angle_rational.imag, p)
        #~ inverse = (u*int(rotated_angle_rational.real))%p
        inverse = (u * int(rotated_angle_rational.imag)) % p
        inverses.append(inverse)
        if inverse > 1:
            print("MInverse Failed")
        elif inverse == 0:
            m_dash = p
        else:
            #~ m_dash = (  u * int(rotated_angle_rational.imag) )%p
            m_dash = (u * int(rotated_angle_rational.real)) % p
        m_dashs.append(m_dash)

    #~ print original_angles
    #~ print rotated_angles
    #~ print inverses
    #~ print ms
    #~ print m_dashs

    return m_dashs, rotated_angles
示例#4
0
def oversampling_1D_filter_Integer(n, M, p = 2, norm = False):
    '''
    The 1D filter for removing oversampling. All values are multiplicative inverses.
    To apply this filter multiply with 1D NTT slice
    '''
    gcd_table = np.zeros(n)
    gcdInv_table = nt.zeros(n)
    inv = nt.integer(1)
    if norm:
        inv = nt.integer(nt.minverse(int(n), M))

    gcd_table[0] = n + int(n)/p
    gcdInv_table[0] = nt.integer(nt.minverse(gcd_table[0], M))
    gcd_table[1] = 1
    gcdInv_table[1] = nt.integer(nt.minverse(gcd_table[1], M))
    for j in range(2,n):
        u, v, gcd_table[j] = nt.extended_gcd( int(j), int(n) )
        gcdInv_table[j] = (nt.integer(nt.minverse(gcd_table[j], M))*inv)%M

    return gcdInv_table
示例#5
0
import farey
import numbertheory as nt
import numpy as np
import matplotlib.pyplot as plt

if __name__ == "__main__":

    N = 17
    a = 1
    b = 10

    lst_of_multiples = np.zeros((N, 2))
    lst_of_thingos = np.zeros((N, 2))

    # find grad
    m = nt.minverse(a, N) * b

    for i in range(0, N):
        lst_of_multiples[i, :] = np.array([[(a * i) % N, (b * i) % N]])
        lst_of_thingos[i, :] = np.array([[i % N, (m * i) % N]])

    img1 = np.zeros((N, N))
    img2 = np.zeros((N, N))

    for i in range(N):
        img1[int(lst_of_multiples[i, 0]), int(lst_of_multiples[i, 1])] = 1
        img2[int(lst_of_thingos[i, 0]), int(lst_of_thingos[i, 1])] = 1

    # Plot each
    fig, axes = plt.subplots(1, 2)