Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

Basic Question on implementing Cordic algorithm for multiplication of 2 numbers.

Status
Not open for further replies.

kyalama

Newbie level 3
Joined
Dec 2, 2011
Messages
3
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Activity points
1,312
Hi,
I was trying to implement the algorithm for multiplication first in matlab and then in verilog and would like to quote from Andraka's paper to illustrate an example. My basic problem is that when I implement the modified equations for multiplication from the paper all i get is x*2 and not x*z (so irrespective of whatever value I initialize z it always does x*2). I find this true when i do it by hand and not just in matlab and want to know where I am going wrong.

So from Andraka's paper:
x(i+1)=x(i)-0*y(i)d(i)*2^-i; which is basically x(i) or just x0 (the initial value)

y(i+1)=y(i)+x(i)*d(i)*(2^-(i)); where d(i)=-1 for z(i)<0,+1 otherwise

z(i+1)=z(i)-d(i)*(2^-(i));

So in the end it says I should get:
x(n)=x0
y(n)=y0+x0*z0
z(n)=0

Lets take an example : x0=3, y0=0, z0=3 (expected result --> y(n) = 9, z(n) = 0)
i=0: x(1)= x0 (this doesnt matter i guess), also d0=1 since z0 is +ve
y(1)= 0 + 3*1*2^-0 = 3
z(1)= 3 - 1*2^-0 = 2

i=1: y(2)= 3+ 3*1*2^-1 = 3+3/2
z(2)= 2 - 1*2^-1 = 2 - 1/2

so on further iterations we notice that
y(n) = 3+ 3/2 + 3/4 + 3/8 +... = 3*( 1+1/2+1/4+1/8+...) isn't this equal to 3 *2 = 6 ?
z(n) = 2 - (1/2+1/4+1/8...) = 2-1 = 1 ?

So can someone tell me where am I going wrong in these iterations and why I am not getting the expected result.

Thanks in advance.
 

You have to set these variables to some data types.
Example :
x=int8(x);
y=int8(y);

then
x0 = x(1) = 255; % first value of Cosinus;
y0 = y(1) = 0; % first value of Sinus;

and in 2^(-i) , i must be constant --> it is define how many times shift to the left in digital systems.

Try it! Goodluck :D
 

@SPIZERO

I wanted to know where I was going wrong in calculating each iteration, not the syntax actually. I could give the datatypes and sizes after i get to know what is happening. Also if I initialize the values, as you suggested it would return 510 ( since it performs 255*2 irrespective of what value i give for z.)
I want it to give me 4*3=12 and and not 4*2=8. Also if someone knows how to modify the Cordic to multiply 2 complex numbers please let me know of the modified equations and the initial values for x0,y0,z0. To be more specific I am going to be using this to calcuate (a-jb)*W where W=e-j2\[\pi\]kn/N.
If there is a shortcut to do the above operation please let me know.

Here is the code in Matlab:

Code:
clear;


%by CORDIC algorithm 

n=20; %Number of iterations


xo=4;   
yo=0;        
zo=3; 
ao=sign(zo); 

x=xo; 
y=yo+xo*ao*(2^-(0)); 
z=zo-ao*(2^-(0)); 
zo=z;  
xo=x;  
yo=y;  
if zo>=0   
    ai=+1; 
 else 
    ai=-1; 
end

for i=1:n-1 
      x=xo;
      y=yo+xo*ao*(2^-(i)); 
      z=zo-ao*(2^-(i));
      if z>=0  
         ai=+1; 
      else 
         ai=-1; 
      end 
      zo=z;       
      xo=x;      
      yo=y;      
 end

display(x);
display(y);
display(z);

Thanks for your reply though.
 
Last edited by a moderator:

z=zo-ao*(2^-(i)); in this equation which u mentioned. don't u think its suppose to be z=zo-ao* tan^-1 (2^-(i)); ?

even i'm workin on a project based on cordic algorithm, can u pls tell me how i can code for 11 iterations?

---------- Post added at 08:15 ---------- Previous post was at 08:12 ----------

z=zo-ao*(2^-(i)); in this equation which u mentioned. don't u think its suppose to be z=zo-ao* tan^-1 (2^-(i)); ?

even i'm workin on a project based on cordic algorithm, can u pls tell me how i can code for 11 iterations?
 

z=zo-ao*(2^-(i)); in this equation which u mentioned. don't u think its suppose to be z=zo-ao* tan^-1 (2^-(i)); ?

even i'm workin on a project based on cordic algorithm, can u pls tell me how i can code for 11 iterations?

---------- Post added at 08:15 ---------- Previous post was at 08:12 ----------

z=zo-ao*(2^-(i)); in this equation which u mentioned. don't u think its suppose to be z=zo-ao* tan^-1 (2^-(i)); ?

even i'm workin on a project based on cordic algorithm, can u pls tell me how i can code for 11 iterations?

Hi,

Are you for asking for multiplication of 2 no.s or finding sin or cosine.. If you want do just simple multiplication, I haven't figured out how to do it yet, but for my purpose I found a workaround by using a different set of equations from the same paper to calculate the twiddle factor. If its Matlab code u want, you could just use the one i posted where you just need to modify the initializing values and the basic equations for different types of functions. If its verilog you either put it in a for loop or manually write all the 11 iterations.. I chose 11 to get the desired accuracy..it could be less than that too.
 

hi, KYALAMA thank u so much for ur reply.
Welll i'm finding sine and cosine.. And here is the code which i've written for al the 11 stages(as i'm in the initial stage of working on cordic, i preferred to write al the steps) but there's some error that i'm getting which i'm not able 2 trace. pls help me out if u can :)

Code:
clc;
x0=1;
y0=0;
k=0.6073;
phi=input('enter angle for comparison');
z0=0;
x1=x0-y0*d*2^-0;
y1=y0-x0*d*2^-0;
z1=z0-d*(tan^-1(2^0));
if(z1>phi)
    d=1;
else
    d=-1;
x2=x1-y1*d*2^-1;
y2=y1-x1*d*2^-1;
z2=z1-(d*tan^-1(2^-1));
s1=d*tan^-1(2^-1);
if(z2>phi)
    d=1;
else
    d=-1;
x3=x2-y2*d*2^-2;
y3=y2-x2*d*2^-2;
z3=z2-(d*tan^-1(2^-2));
s2=d*tan^-1(2^-2);
if(z3>phi)
    d=1;
else
    d=-1;
x4=x3-y3*d*2^-3;
y4=y3-x3*d*2^-3;
z4=z3-d*tan^-1(2^-3);
s3=d*tan^-1(2^-3);
if(z4>phi)
    d=1;
else
    d=-1;
x5=x4-y4*d*2^-4;
y5=y4-x4*d*2^-4;
z5=z4-d*tan^-1(2^-4);
s4=d*tan^-1(2^-4);
if(z5>phi)
    d=1;
else
    d=-1;
x6=x5-y5*d*2^-5;
y6=y5-x5*d*2^-5;
z6=z5-d*tan^-1(2^-5);
s5=d*tan^-1(2^-5);
if(z6>phi)
    d=1;
else
    d=-1;  
x7=x6-y6*d*2^-6;
y7=y6-x6*d*2^-6;
z7=z6-d*tan^-1(2^-6);
s6=d*tan^-1(2^-6);
if(z7>phi)
    d=1;
else
    d=-1; 
x8=x7-y7*d*2^-7;
y8=y7-x7*d*2^-7;
z8=z7-d*tan^-1(2^-7);
s7=d*tan^-1(2^-7);
if(z8>phi)
    d=1;
else
    d=-1; 
x9=x8-y8*d*2^-8;
y9=y8-x8*d*2^-8;
z9=z8-d*tan^-1(2^-8);
s8=d*tan^-1(2^-8);
if(z9>phi)
    d=1;
else
    d=-1; 
x10=x9-y9*d*2^-9;
y10=y9-x9*d*2^-9;
z10=z9-d*tan^-1(2^-9);
s9=d*tan^-1(2^-9);
if(z10>phi)
    d=1;
else
    d=-1; 
x11=x10-y10*d*2^-10;
y11=y10-x10*d*2^-10;
z11=z10-d*tan^-1(2^-10);
s10=d*tan^-1(2^-10);

x=k*x11;
y=k*y11;
z=k*z11;
phi2=z1+s1+s2+s3+s4+s5+s6+s7+s8+s9+s10;
 
Last edited by a moderator:

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top