timthorpe的个人空间 https://blog.eetop.cn/670982 [收藏] [复制] [分享] [RSS]

空间首页 动态 记录 日志 相册 主题 分享 留言板 个人资料

日志

cordic原理与fpga实现(2)

已有 1331 次阅读| 2013-10-30 09:17 |个人分类:算法

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

 
For Sweetest things turn sourest by their deeds; Lilies that fester smell far worse than weeds.
--William Shakespeare 

点赞

评论 (0 个评论)

facelist

您需要登录后才可以评论 登录 | 注册

  • 关注TA
  • 加好友
  • 联系TA
  • 0

    周排名
  • 0

    月排名
  • 0

    总排名
  • 0

    关注
  • 1

    粉丝
  • 0

    好友
  • 0

    获赞
  • 1

    评论
  • 880

    访问数
关闭

站长推荐 上一条 /2 下一条


小黑屋| 手机版| 关于我们| 联系我们| 在线咨询| 隐私声明| EETOP 创芯网
( 京ICP备:10050787号 京公网安备:11010502037710 )

GMT+8, 2024-12-22 15:18 , Processed in 0.015929 second(s), 8 queries , Gzip On, Redis On.

eetop公众号 创芯大讲堂 创芯人才网
返回顶部