Example #1
0
def output(partIdx):
    """Uses the student code to compute the output for test cases."""
    outputString = ''
    dictInput = pickle.load(open("testInputA2.pkl"))  ## load the dictionary containing output types and test cases
    testCases = dictInput['testCases']
    outputType = dictInput['outputType']
  
    if partIdx == 0: # This is A2-part-1: genSine
        for line in testCases['A2-part-1']:
            answer = genSine(**line)
            if outputType['A2-part-1'][0] == type(answer):
                outputString += convertNpObjToStr(answer) + '\n'
            else:
                wrongOutputTypeError(outputType['A2-part-1'][0],partIdx)
                return ''
                #sys.exit(1)
  
    elif partIdx == 1: # This is A2-part-2: genComplexSine
        for line in testCases['A2-part-2']:
            answer = genComplexSine(**line)
            if outputType['A2-part-2'][0] == type(answer):
                outputString += convertNpObjToStr(answer) + '\n'
            else:
                wrongOutputTypeError(outputType['A2-part-2'][0],partIdx)
                return ''
                #sys.exit(1)    
      
    elif partIdx == 2: # This is A2-part-3: DFT
        for line in testCases['A2-part-3']:
            answer = DFT(**line)
            if outputType['A2-part-3'][0] == type(answer):
                outputString += convertNpObjToStr(answer) + '\n'
            else:
                wrongOutputTypeError(outputType['A2-part-3'][0],partIdx)
                return ''
                #sys.exit(1)        
      
    elif partIdx == 3: # This is A2-part-4: IDFT
        for line in testCases['A2-part-4']:
            answer = IDFT(**line)
            if outputType['A2-part-4'][0] == type(answer):
                outputString += convertNpObjToStr(answer) + '\n'
            else:
                wrongOutputTypeError(outputType['A2-part-4'][0],partIdx)
                return ''
                #sys.exit(1)         

    elif partIdx == 4: # This is A2-part-5: genMagSpec
        for line in testCases['A2-part-5']:
            answer = genMagSpec(**line)
            if outputType['A2-part-5'][0] == type(answer):
                outputString += convertNpObjToStr(answer) + '\n'
            else:
                wrongOutputTypeError(outputType['A2-part-5'][0],partIdx)
                return ''
                #sys.exit(1)         

    return outputString.strip()
Example #2
0
def DFT(x):
    """
    Input:
        x (numpy array) = input sequence of length N
    Output:
        The function should return a numpy array of length N
        X (numpy array) = The N point DFT of the input sequence x
    """
    N = len(x)
    W = np.vstack([genComplexSine(k, N) for k in range(N)])
    return W.dot(x)
Example #3
0
def DFT(x):
    """
    Input:
    x (numpy array) = input sequence of length N
    Output:
    The function should return a numpy array of length N
    X (numpy array) = The N point DFT of the input sequence x
    """
    N = len(x)
    X = np.array([(np.sum(x*genComplexSine(k, N))) for k in range(N)])
    return X
Example #4
0
def IDFT(X):
    """
    Input:
        X (numpy array) = frequency spectrum (length N)
    Output:
        The function should return a numpy array of length N
        x (numpy array) = The N point IDFT of the frequency spectrum X
    """
    N = len(X)
    W = np.conj(np.vstack([genComplexSine(k, N) for k in range(N)]))
    return (1./N) * W.dot(X)
Example #5
0
 def test_genComplexSine(self):
     expected = np.array([
         1.0 + 0.j, 0.30901699 - 0.95105652j, -0.80901699 - 0.58778525j,
         -0.80901699 + 0.58778525j, 0.30901699 + 0.95105652j
     ])
     actual = genComplexSine(1, 5)
     print('expected:', np.imag(expected))
     print('actual:', np.imag(actual))
     print(np.isclose(np.imag(expected), np.imag(actual)))
     self.assertTrue(np.allclose(np.real(expected), np.real(actual)))
     self.assertTrue(np.allclose(np.imag(expected), np.imag(actual)))
Example #6
0
def output(partIdx):
  """Uses the student code to compute the output for test cases."""
  outputString = ''
  dictInput = pickle.load(open("testInputA2.pkl"))  ## load the dictionary containing output types and test cases
  testCases = dictInput['testCases']
  outputType = dictInput['outputType']

  if partIdx == 0: # This is A2-part-1: genSine
    for line in testCases['A2-part-1']:
      answer = genSine(**line)
      if outputType['A2-part-1'][0] == type(answer):
        outputString += convertNpObjToStr(answer) + '\n'
      else:
        wrongOutputTypeError(outputType['A2-part-1'][0])
        sys.exit(1)

  elif partIdx == 1: # This is A2-part-2: genComplexSine
    for line in testCases['A2-part-2']:
      answer = genComplexSine(**line)
      if outputType['A2-part-2'][0] == type(answer):
        outputString += convertNpObjToStr(answer) + '\n'
      else:
        wrongOutputTypeError(outputType['A2-part-2'][0])
        sys.exit(1)

  elif partIdx == 2: # This is A2-part-3: DFT
    for line in testCases['A2-part-3']:
      answer = DFT(**line)
      if outputType['A2-part-3'][0] == type(answer):
        outputString += convertNpObjToStr(answer) + '\n'
      else:
        wrongOutputTypeError(outputType['A2-part-3'][0])
        sys.exit(1)

  elif partIdx == 3: # This is A2-part-4: IDFT
    for line in testCases['A2-part-4']:
      answer = IDFT(**line)
      if outputType['A2-part-4'][0] == type(answer):
        outputString += convertNpObjToStr(answer) + '\n'
      else:
        wrongOutputTypeError(outputType['A2-part-4'][0])
        sys.exit(1)

  elif partIdx == 4: # This is A2-part-5: genMagSpec
    for line in testCases['A2-part-5']:
      answer = genMagSpec(**line)
      if outputType['A2-part-5'][0] == type(answer):
        outputString += convertNpObjToStr(answer) + '\n'
      else:
        wrongOutputTypeError(outputType['A2-part-5'][0])
        sys.exit(1)

  return outputString.strip()
Example #7
0
def DFT(x):
    """
    Input:
        x (numpy array) = input sequence of length N
    Output:
        The function should return a numpy array of length N
        X (numpy array) = The N point DFT of the input sequence x
    """
    X = np.array([])
    N = x.size
    for k in xrange(N):
        term = genComplexSine(k,N)
        X = np.append(X, sum(term*x))

    return X
Example #8
0
def DFT(x):
    """
    Input:
        x (numpy array) = input sequence of length N
    Output:
        The function should return a numpy array of length N
        X (numpy array) = The N point DFT of the input sequence x
    """

    X = np.array([])
    for k in range(np.size(x)):
        cSine = genComplexSine(k, x.size)
        X = np.append(X, np.dot(x, cSine))

    return X
Example #9
0
def DFT(x):
    """
    Input:
        x (numpy array) = input sequence of length N
    Output:
        The function should return a numpy array of length N
        X (numpy array) = The N point DFT of the input sequence x
    """
    
    X = np.array([])
    for k in range(np.size(x)):
    	cSine = genComplexSine(k, x.size)
    	X = np.append(X, np.dot(x, cSine))

    return X
def DFT(x):
    """
    Input:
        x (numpy array) = input sequence of length N
    Output:
        The function should return a numpy array of length N
        X (numpy array) = The N point DFT of the input sequence x
    """
    # Your code here
    N = len(x)
    X = np.array([])
    for k in range(N):
        s = genComplexSine(k=k, N=N)
        X = np.append(X, sum(x * np.conjugate(s)))
    return X
def DFT(x):
    """
    Input:
        x (numpy array) = input sequence of length N
    Output:
        The function should return a numpy array of length N
        X (numpy array) = The N point DFT of the input sequence x
    """
    ## Your code here
    N = len(x)
    X = np.zeros(N)
    for n in range(0, N):
        exp_n = genComplexSine(n, N)
        X = np.sum([X, x[n] * exp_n], axis=0)
    return X
Example #12
0
def IDFT(X):
    """
    Input:
        X (numpy array) = frequency spectrum (length N)
    Output:
        The function should return a numpy array of length N 
        x (numpy array) = The N point IDFT of the frequency spectrum X
    """

    x = np.array([])
    N = X.size
    for k in range(N):
        cSine = genComplexSine(-k, N)
        x = np.append(x, 1.0 / N * np.dot(X, cSine))

    return x
Example #13
0
def DFT(x):
    """
    Input:
        x (numpy array) = input sequence of length N
    Output:
        The function should return a numpy array of length N
        X (numpy array) = The N point DFT of the input sequence x
    """
    # Your code here
    N = x.size
    X = np.array([])
    for k in np.arange(0, N):
        item = sum(x * genComplexSine(k, N))
        X = np.append(X, item)

    return X
Example #14
0
def genMagSpec(x):
    """
    Input:
        x (numpy array) = input sequence of length N
    Output:
        The function should return a numpy array
        magX (numpy array) = The magnitude spectrum of the input sequence x
                             (length N)
    """
    XMag = np.array([])
    N = x.size
    for k in range(N):
    	cSine = genComplexSine(k, N)
    	XMag = np.absolute(np.append(XMag, np.dot(x, cSine)))

    return XMag
Example #15
0
def IDFT(X):
    """
    Input:
        X (numpy array) = frequency spectrum (length N)
    Output:
        The function should return a numpy array of length N 
        x (numpy array) = The N point IDFT of the frequency spectrum X
    """
    
    x = np.array([])
    N = X.size
    for k in range(N):
    	cSine = genComplexSine(-k, N)
    	x = np.append(x, 1.0/N * np.dot(X, cSine))

    return x
Example #16
0
def DFT(x):
    """
    Input:
        x (numpy array) = input sequence of length N
    Output:
        The function should return a numpy array of length N
        X (numpy array) = The N point DFT of the input sequence x
    """
    ## Your code here
    N = len(x)
    X = np.zeros(N, dtype=np.complex128)

    # do DFT.
    for k in np.arange(N):
        s = genComplexSine(k=k, N=N)
        X[k] = np.dot(x, s)
    return X
def IDFT(X):
    """
    Input:
        X (numpy array) = frequency spectrum (length N)
    Output:
        The function should return a numpy array of length N 
        x (numpy array) = The N point IDFT of the frequency spectrum X
    """

    #get the length of the discrete spectrum
    N = len(X)

    #empty array of length N to store output sequence in
    signal = np.array([])

    #loop over n and compute inner products
    for n in range(N):
        #complex sine must be conjugated to get positiv sign for j
        complex_sine = np.conjugate(genComplexSine(n, N))
        inner_product = np.sum(complex_sine * X) * 1./N
        signal = np.append(signal, inner_product)

    return signal
Example #18
0
import matplotlib.pyplot as plt
import numpy as np
from A2Part1 import genSine
from A2Part2 import genComplexSine
from A2Part3 import DFT
from A2Part4 import IDFT

x = genComplexSine(1.5, 64)
# x = genSine(1, 4, 0, 64, 1)
x = DFT(x)
x = IDFT(x)

plt.plot(np.real(x))
plt.plot(np.imag(x))
# plt.plot(np.abs(x))
plt.show()
Example #19
0
def output(partIdx):
    """Uses the student code to compute the output for test cases."""
    outputString = ''
    filename = open('testInputA2.pkl','rb')
    try: ## load the dict containing output types and test cases
        dictInput = pickle.load(filename,encoding='latin1')  ## python3
    except TypeError:
        dictInput = pickle.load(filename)  ## python2 
        
    testCases = dictInput['testCases']
    outputType = dictInput['outputType']
  
    if partIdx == 0: # This is A2-part-1: genSine
        for line in testCases['A2-part-1']:
            answer = genSine(**line)
            if outputType['A2-part-1'][0] == type(answer):
                outputString += convertNpObjToStr(answer) + '\n'
            else:
                wrongOutputTypeError(outputType['A2-part-1'][0],partIdx)
                return ''
                #sys.exit(1)
  
    elif partIdx == 1: # This is A2-part-2: genComplexSine
        for line in testCases['A2-part-2']:
            answer = genComplexSine(**line)
            if outputType['A2-part-2'][0] == type(answer):
                outputString += convertNpObjToStr(answer) + '\n'
            else:
                wrongOutputTypeError(outputType['A2-part-2'][0],partIdx)
                return ''
                #sys.exit(1)    
      
    elif partIdx == 2: # This is A2-part-3: DFT
        for line in testCases['A2-part-3']:
            answer = DFT(**line)
            if outputType['A2-part-3'][0] == type(answer):
                outputString += convertNpObjToStr(answer) + '\n'
            else:
                wrongOutputTypeError(outputType['A2-part-3'][0],partIdx)
                return ''
                #sys.exit(1)        
      
    elif partIdx == 3: # This is A2-part-4: IDFT
        for line in testCases['A2-part-4']:
            answer = IDFT(**line)
            if outputType['A2-part-4'][0] == type(answer):
                outputString += convertNpObjToStr(answer) + '\n'
            else:
                wrongOutputTypeError(outputType['A2-part-4'][0],partIdx)
                return ''
                #sys.exit(1)         

    elif partIdx == 4: # This is A2-part-5: genMagSpec
        for line in testCases['A2-part-5']:
            answer = genMagSpec(**line)
            if outputType['A2-part-5'][0] == type(answer):
                outputString += convertNpObjToStr(answer) + '\n'
            else:
                wrongOutputTypeError(outputType['A2-part-5'][0],partIdx)
                return ''
                #sys.exit(1)         

    return outputString.strip()