Advertisement
mllm

cordic

Apr 18th, 2019 (edited)
400
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.31 KB | None | 0 0
  1. import numpy as np
  2.  
  3. pi = np.pi
  4.  
  5. def cordic(beta:float, n:int)-> list[float]:
  6.     """
  7.     This function computes [cos(beta), sin(beta)] using n iterations. Increasing n will increase the precision.
  8.  
  9.     Args:
  10.         beta (float): the angle in radians
  11.         n (integer): the number of iterations. Maximum number of n is 23 due to the table sizes.
  12.    
  13.     Returns:
  14.         result (list): a list with the cos(beta) as the first argument and sin(beta) as the second argument
  15.     """
  16.  
  17.     if beta < -pi/2 or beta > pi/2:
  18.         if beta < 0:
  19.             result = cordic(beta + pi, n)
  20.         else:
  21.             result = cordic(beta - pi, n)
  22.         result = -result
  23.         return result
  24.  
  25.     # Initialization of tables of constants used by CORDIC
  26.     # need a table of arctangents of negative powers of two, in radians:
  27.     # angles = atan(2.^-(0:27));
  28.     angles =  [
  29.         0.78539816339745, 0.46364760900081, 0.24497866312686, 0.12435499454676,
  30.         0.06241880999596, 0.03123983343027, 0.01562372862048, 0.00781234106010,
  31.         0.00390623013197, 0.00195312251648, 0.00097656218956, 0.00048828121119,
  32.         0.00024414062015, 0.00012207031189, 0.00006103515617, 0.00003051757812,
  33.         0.00001525878906, 0.00000762939453, 0.00000381469727, 0.00000190734863,
  34.         0.00000095367432, 0.00000047683716, 0.00000023841858, 0.00000011920929,
  35.         0.00000005960464, 0.00000002980232, 0.00000001490116, 0.00000000745058 ]
  36.  
  37.     # and a table of products of reciprocal lengths of vectors [1, 2^-2j]:
  38.     # Kvalues = cumprod(1./abs(1 + 1j*2.^(-(0:23))))
  39.     Kvalues = [
  40.         0.70710678118655, 0.63245553203368, 0.61357199107790, 0.60883391251775,
  41.         0.60764825625617, 0.60735177014130, 0.60727764409353, 0.60725911229889,
  42.         0.60725447933256, 0.60725332108988, 0.60725303152913, 0.60725295913894,
  43.         0.60725294104140, 0.60725293651701, 0.60725293538591, 0.60725293510314,
  44.         0.60725293503245, 0.60725293501477, 0.60725293501035, 0.60725293500925,
  45.         0.60725293500897, 0.60725293500890, 0.60725293500889, 0.60725293500888 ]
  46.  
  47.     Kn = Kvalues[min(n, len(Kvalues))]
  48.  
  49.     # Initialize loop variables:
  50.     v = [1,0] # start with 2-vector cosine 1 and sine of zero
  51.     power_of_two = 1
  52.     angle = angles[0]
  53.  
  54.     # Iterations
  55.     for j in range(0,n-1):
  56.         if beta < 0:
  57.             sigma = -1
  58.         else:
  59.             sigma = 1
  60.  
  61.         factor = sigma * power_of_two
  62.         # Note the matrix multiplication can be done using scaling by powers of two and addition subtraction
  63.         R = [[1, -factor],[factor, 1]]     
  64.         v = np.dot(R, v)       
  65.         beta = beta - sigma * angle # update the remaining angle
  66.         power_of_two = power_of_two / 2    
  67.         # update the angle from table, or eventually by just dividing by two
  68.         if j+2 > len(angles):
  69.             angle = angle / 2
  70.         else:
  71.             angle = angles[j+1]
  72.  
  73.     # Adjust length of output vector to be [cos(beta), sin(beta)]
  74.     v = v * Kn
  75.     result =
  76.     return result
  77.  
  78.  
  79. def main():
  80.     zero = cordic(0, 23)      # expected output: [1, 0]
  81.     print(zero)
  82.     pitwo = cordic(pi/2,23)    # expected output: [0, 1]
  83.     print(pitwo)
  84.     pialone = cordic(pi, 23)      # expected output: [-1, 0]   
  85.     print(pialone)
  86.     pithree = cordic(pi/3, 23)   # expected output: [0.5, sqrt(3)/2]   
  87.     print(pithree[0], pithree[1])
  88.     pifour = cordic(pi/4, 23)   # expected output: [sqrt(2)/2, sqrt(2)/2]
  89.     print(pifour[0],pifour[1])
  90.     pisix = cordic(pi/6, 23)   # expected output: [sqrt(3)/2, 0.5]
  91.     print(pisix[0],pisix[1])
  92.     pi32 = cordic(3*pi/2, 23) # expected output: [0, -1]   
  93.     print(pi32[0], pi32[1])
  94.  
  95. if __name__ == "__main__":
  96.     main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement