1. 程式人生 > >cordic演算法計算sin和cos

cordic演算法計算sin和cos

%根據cordic演算法計算sin&cos
%共迭代16次
%輸入:-pi~pi
%輸出:sin&cos
function [s,c]=cordic(theta)
tanx=0:15;
tanx=2.^tanx;
tanx=1./tanx;
atanx=atan(tanx);

if theta <0
    x=0;
    y=-1;
    theta=theta+pi/2;
else
    x=0;
    y=1;
    theta=theta-pi/2;
end

for n=1:length(tanx)
    if theta>0
        xk=x-y*tanx(n);
        yk=y+x*tanx(n);
        theta=theta-atanx(n);
    else
        xk=x+y*tanx(n);
        yk=y-x*tanx(n);
        theta=theta+atanx(n);
    end
    x=xk;
    y=yk;
end
atanx=cos(atanx);
atanx=prod(atanx);
s=y*atanx;
c=x*atanx;