CORDIC algorithm returning bad numbers

365 Views Asked by At

I started to implement a CORDIC algorithm from zero and I don't know what I'm missing, here's what I have so far.

import math
from __future__ import division

# angles
n = 5
angles = []
for i in range (0, n):
    angles.append(math.atan(1/math.pow(2,i)))    

# constants
kn = []
fator = 1.0
for i in range (0, n):
    fator = fator * (1 / math.pow(1 + (2**(-i))**2, (1/2)))
    kn.append(fator)


# taking an initial point p = (x,y) = (1,0)
z = math.pi/2 # Angle to be calculated

x = 1
y = 0
for i in range (0, n):
    if (z < 0):
        x = x + y*(2**(-1*i))
        y = y - x*(2**(-1*i))
        z = z + angles[i]
    else:
        x = x - y*(2**(-1*i))
        y = y + x*(2**(-1*i))
        z = z - angles[i]
    x = x * kn[n-1]
    y = y * kn[n-1]
print x, y

When I plug z = π/2 it returns 0.00883479322917 and 0.107149125055, which makes no sense. Any help will be great!

@edit, I made some changes and now my code has this lines instead of those ones

for i in range (0, n):
    if (z < 0):
        x = x0 + y0*(2**(-1*i))
        y = y0 - x0*(2**(-1*i))
        z = z + angles[i]
    else:
        x = x0 - y0*(2**(-1*i))
        y = y0 + x0*(2**(-1*i))
        z = z - angles[i]
    x0 = x
    y0 = y
    x = x * kn[n-1]
    y = y * kn[n-1]

Now it's working way better, I had the problem because I wasn't using temporary variables as x0 and y0, now when I plug z = pi/2 it gives me better numbers as (4.28270993661e-13, 1.0) :)

0

There are 0 best solutions below