CORDIC原理与FPGA实现(2)

CORDIC算法实现极坐标(polar)到直角坐标系(Cartesian)的变换。

   1:  function [horizonal,vertical]=polar2car(mag, pha);
   2:  x =mag;
   3:  y =0;
   4:  z=pha;
   5:  d=0;
   6:  i=0;
   7:  k = 0.6073; %K 增益
   8:  x = k*x;
   9:  while i<50
  10:      if z<0 d =-1;
  11:      else d = 1;
  12:      end
  13:      xNew=x-y*d*(2^(-i));
  14:      y=y+x*d*(2^(-i));
  15:      z=z-d*atan(1/2^(i));
  16:      i=i+1;
  17:       
  18:       
  19:      x=xNew;
  20:  end
  21:  horizonal = x;
  22:  vertical = y;
CORDIC算法实现直角坐标到极坐标系的变换。
   1:  function [mag, pha]= car2polar(x,y);
   2:    
   3:  %y =0;
   4:                       %将直角坐标系中的点(x,y)旋转到x轴,旋转的角度即为其极坐标的相位,在x轴的长度等于极坐标的幅度  
   5:  d=0;                 %可用于求相位,幅度
   6:  i=0;
   7:  z=0;
   8:  k = 0.6073; %K 增益
   9:   
  10:  while i<50
  11:      if y<0 d = 1;
  12:      else d = -1;
  13:      end
  14:      xNew=x-y*d*(2^(-i));
  15:      y=y+x*d*(2^(-i));
  16:      z=z-d*atan(1/2^(i));
  17:      i=i+1;
  18:       
  19:       
  20:      x=xNew;
  21:  end
  22:   x =  x*k;
  23:   mag=x;
  24:   pha=z;
验证:

[a,b]= polar2car( 1,pi/3)

a =

    0.5000


b =

    0.8661

[a,b]=  car2polar( 0.5000, 0.8661)

a =

    1.0001


b =

    1.0472

计算正切值atan只需将直角坐标变换为极坐标的程序中取出最后的角度值,即可得到反正切值。
   1:  function [ pha]= cordic_arcsin(c);
   2:    
   3:  %y =0;
   4:                         %将点(1,0)旋转至其纵坐标=c,旋转的角度为角度 求反余弦也是同样道理  
   5:  d=0;
   6:  i=0;
   7:  z=0;
   8:  x=1;
   9:  y=0;
  10:  k = 0.6073; %K 增益
  11:   xNew =  x* k;
  12:  while i<100
  13:      if y<=c d = 1;
  14:      else d =  -1;
  15:      end
  16:      x =xNew-y*d*(2^(-i));
  17:      y=y+xNew*d*(2^(-i));
  18:      z=z+d*atan(1/2^(i));
  19:      i=i+1;
  20:       
  21:       
  22:       xNew=x;
  23:  end
  24:   
  25:   %mag=x;
  26:   pha=z;
   1:  function [pha]= cordic_arccos(c);
   2:    
   3:  %y =0;  
   4:  d=0;
   5:  i=0;
   6:  z=0;
   7:  x=1;
   8:  y=0;
   9:  k = 0.6073; %K 增益
  10:   xNew =  x* k;
  11:  while i<100
  12:      if x>=c d = 1;
  13:      else d =  -1;
  14:      end
  15:      x =xNew-y*d*(2^(-i));
  16:      y=y+xNew*d*(2^(-i));
  17:      z=z+d*atan(1/2^(i));
  18:      i=i+1;
  19:       
  20:       
  21:       xNew=x;
  22:  end
  23:   
  24:   %mag=x;
  25:   pha=z;
   1:  function [  pha]= cordic_arctan(x,y);
   2:    
   3:  %y =0;
   4:                      %将点(x,y)旋转到x轴所需要的角度  
   5:  d=0;
   6:  i=0;
   7:  z=0;
   8:  k = 0.6073; %K 增益
   9:   x =  x*k;
  10:  while i<50
  11:      if y<0 d = 1;
  12:      else d = -1;
  13:      end
  14:      xNew=x-y*d*(2^(-i));
  15:      y=y+x*d*(2^(-i));
  16:      z=z-d*atan(1/2^(i));
  17:      i=i+1;
  18:       
  19:       
  20:      x=xNew;
  21:  end
  22:   
  23:   %mag=x;
  24:   pha=z;
   1:  function [sine,cosine] = cordic_sine(angle);
   2:  % Initialitation
   3:     %%angle=30 ;
   4:      x = 1;
   5:      y = 0;
   6:      z = angle;
   7:      d = 1;
   8:      
   9:      i = 0;          % Iterative factor
  10:     k = 0.6073;     %K Factor
  11:      xNew = k*x;
  12:   while i < 50
  13:       if z <=0 d =-1;
  14:       else d = 1;
  15:       end
  16:       x= xNew -d*y*2^(-i);
  17:       y=y+d*xNew*2^(-i);
  18:       z=z-d*atan(2^(-i));
  19:       i=i+1;
  20:       xNew=x;
  21:   end
  22:  cosine = x
  23:  sine = y
OPTIMISM, PASSION & HARDWORK
原文地址:https://www.cnblogs.com/hiramlee0534/p/3387603.html