Exemplo n.º 1
0
def idft(f, scale=True):
    """
    Function to evaluate the inverse of the dft for an array.

    Args:
        f (1d-array): array to calculate the dft of
        scale (optional[boolean]): scales the result by dividing it by the
                                    number of array elements

    Returns:
        Array of the inverse fourier transformation
    """
    def swap(f):
        """
        Function to swap the imaginary and real components of an imaginary array
        """
        return f.real * (0 + 1j) + f.imag

    M = f.shape[0]
    scaleFactor = 1
    if (scale):
        scaleFactor = (1 / M)

    transform = swap(numerical.dft(swap(f))) / scaleFactor

    return transform
Exemplo n.º 2
0
def idft(f, scale=True):
    """
    Function to evaluate the inverse of the dft for an array.

    Args:
        f (1d-array): array to calculate the dft of
        scale (optional[boolean]): scales the result by dividing it by the
                                    number of array elements

    Returns:
        Array of the inverse fourier transformation
    """

    def swap(f):
        """
        Function to swap the imaginary and real components of an imaginary array
        """
        return f.real*(0+1j) + f.imag

    M = f.shape[0]
    scaleFactor = 1
    if (scale):
        scaleFactor = (1/M)

    transform = swap(numerical.dft(swap(f)))/scaleFactor

    return transform
Exemplo n.º 3
0
def idft(F, scale=False):
    def swap(F):
        return F.real * (0 + 1j) + F.imag

    M = F.shape[0]
    scaleFactor = 1
    if (scale):
        scaleFactor = (1 / M)

    ift = swap(numerical.dft(swap(F))) / scaleFactor

    return ift
Exemplo n.º 4
0
def dft2(f, scale=True):

    c = f.swapaxes(0, 1)
    Columns = []
    for col in range(c.shape[0]):
        Columns.append(numerical.dft(c[col], False))

    x = np.array(Columns)
    r = x.swapaxes(0, 1)
    Rows = []

    for row in range(r.shape[0]):
        Rows.append(numerical.dft(r[row], False))

    fU = np.array(Rows)

    M = f.shape[0] * f.shape[1]
    scaleFactor = 1
    if (scale):
        scaleFactor = (1 / M)

    return fU * scaleFactor
Exemplo n.º 5
0
def dft2(f, scale=True):
    """
    Function to evaluate the dft for a 2D array.

    Args:
        f (2d-array): array to calculate the 2 dimensional dft of
        scale (optional[boolean]): scales the result by dividing it by the
                                    number of array elements

    Returns:
        2 dimensional array of fourier transformation
    """

    # for every column, evaluate the dft
    columns = f.swapaxes(0, 1)
    Columns = []
    for col in range(columns.shape[0]):
        Columns.append(numerical.dft(columns[col], False))

    newSpace = np.array(Columns)

    # for every row in the new space, evaluate the dft
    # swap the axes again to set it back to normal
    rows = newSpace.swapaxes(0, 1)
    Rows = []
    for row in range(rows.shape[0]):
        Rows.append(numerical.dft(rows[row], False))

    finalSpace = np.array(Rows)

    M = f.shape[0] * f.shape[1]
    scaleFactor = 1
    if (scale):
        scaleFactor = (1 / M)

    return finalSpace * scaleFactor
Exemplo n.º 6
0
def dft2(f, scale=True):
    """
    Function to evaluate the dft for a 2D array.

    Args:
        f (2d-array): array to calculate the 2 dimensional dft of
        scale (optional[boolean]): scales the result by dividing it by the
                                    number of array elements

    Returns:
        2 dimensional array of fourier transformation
    """

    # for every column, evaluate the dft
    columns = f.swapaxes(0, 1)
    Columns = []
    for col in range(columns.shape[0]):
        Columns.append(numerical.dft(columns[col], False))

    newSpace = np.array(Columns)

    # for every row in the new space, evaluate the dft
    # swap the axes again to set it back to normal
    rows = newSpace.swapaxes(0, 1)
    Rows = []
    for row in range(rows.shape[0]):
        Rows.append(numerical.dft(rows[row], False))

    finalSpace = np.array(Rows)

    M = f.shape[0] * f.shape[1]
    scaleFactor = 1
    if scale:
        scaleFactor = 1 / M

    return finalSpace * scaleFactor
      scaleFactor = (1/M)

   ft = scaleFactor * np.sum((f*e), axis = 1)
   return ft


if __name__ == '__main__':

   import numerical
   import numpy
   import time

   N = 2**12
   f = numpy.ones(N, dtype=numpy.complex128)

   repeats = 10
   print('Repetitions = {0}'.format(repeats))

   startTime = time.clock()
   for repeat in range(repeats):
      F = numerical.dft(f)
   string = 'Average time per transform = {0:.8f} [s] ({1}-point DFT)'
   print(string.format((time.clock() - startTime)/repeats, len(f)))

   startTime = time.clock()
   for repeat in range(repeats):
      F = numpy.fft.fft(f)
   string = 'Average time per transform = {0:.8f} [s] ({1}-point FFT)'
   print(string.format((time.clock() - startTime)/repeats, len(f)))

Exemplo n.º 8
0
        scaleFactor = (1/M)

    transform = scaleFactor * np.sum((f*phase), axis=1)
    return transform

"""
PYTHON TEST HARNESS
"""
if __name__ == '__main__':
    import numerical
    import numpy
    import time

    N = 2**12
    f = numpy.ones(N, dtype=numpy.complex128)

    repeats = 10
    print('Repetitions = {0}'.format(repeats))

    startTime = time.clock()
    for repeat in range(repeats):
        F = numerical.dft(f)
    string = 'Average time per transform = {0:.8f} [s] ({1}-point DFT)'
    print(string.format((time.clock() - startTime)/repeats, len(f)))

    startTime = time.clock()
    for repeat in range(repeats):
        F = numpy.fft.fft(f)
    string = 'Average time per transform = {0:.8f} [s] ({1}-point FFT)'
    print(string.format((time.clock() - startTime)/repeats, len(f)))