Cordic benchmarking

This python CORDIC (COordinate Rotation DIgital Computer) implementation helps deciding the width of intermediate calculations and the number of steps for implementing the CORDIC in a FPGA. It uses the Simple Python Fixed-Point Module, along with scipy.

Please note does not intend to be, and is not, an efficient implementation of the CORDIC algorithm. The purpose of this notebook is to give insight on how the algorithm works, its convergence and how fixed-number precision affects the output.


In [15]:
%matplotlib inline

import FixedPoint as fp
import scipy as sp
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.cm as cm

In [16]:
#Parameters for you cordic
input_width = 16
output_width = 16
internal_width = output_width + 5
steps = 32
m = 1

In [35]:
#Initial values
x_in = 1
y_in = 0.1
tetha_in = 0

In [36]:
x = np.zeros(steps); x[0] = x_in;
y = np.zeros(steps); y[0] = y_in;
tetha  = np.zeros(steps); tetha[0] = tetha_in;
k = np.zeros(steps); k[0] = 1;

#Todo: allow change vectoring to rotating mode
for i in range(0,steps-1):
    
    if y[i] <= 0:
        delta = -1
    else:
        delta = 1
    
    x[i+1] = x[i] + delta * m * y[i]/(2**i)
    y[i+1] = y[i] - delta     * x[i]/(2**i)

    alpha = np.arctan(2**-i)
    tetha[i+1] =  tetha[i] + delta * alpha
    k[i+1] = k[i] / (np.cos(alpha))

In [37]:
#Create iteration plot
vectors = zip(x,y)

plt.figure()
ax = plt.gca()

colors = np.linspace(0,1,steps)

ax.quiver(0,0,x,y,colors,angles='xy',scale_units='xy',scale=1, cmap=cm.spectral, alpha=0.5, width=0.003)
ax.set_xlim([min(0,min(x)), max(x)])
ax.set_ylim([min(0,min(y)), max(y)])
plt.draw()
plt.show()



In [38]:
rel_error = (np.absolute(x_in+1j*y_in) - x/k)/(x/k)
plt.plot(np.fabs(rel_error));
ax = plt.gca(); ax.set_yscale('log',basey=2); plt.xlabel('Step'); plt.ylabel('Relative error'); plt.show()



In [38]:


In [38]: