⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 cordic.m

📁 Cordic算法的Matlab实现
💻 M
字号:
%% description
%   this routine is employed to compute functions 
%   such as x0z0,y0/x0,sinz0,cosz0,atany0,sinhz0,
%   coshz0, and atanhy0 and so on
%   some other functions such as 
%   tanz0,tanhz0,expz0,lnw,w^.5 can also be derived
%   from the above results
%% code
function [xn,gain,Fs,Ds,zn,yn]=cordic(x0,y0,z0,mode,...
                                      dv_n,ROM,wordwidth,acc)
% initial state:        x0,y0,z0
%               initial state distincts various operatoins
%               and some of the initial values effects the
%               final results
% mode hints:           mode
%               there are three modes - circular,linear,
%               hyperbolic, generally, we use 1,0,-1 to
%               denotes them respectively and theoretically.
%               m chooses one of them
% decision variable name:    dv_n
%               decision variable decides which direction
%               to rotate,generally, dv is chosen between
%               'y' and 'z'
% datas stored:     ROM
% wordwidth of system: wordwidth
% accuracy requirement: 2^(-acc)

% check inputs===============================================
if(nargin<3)
    disp('too few inputs\n')
    z0=0;
end
if(nargin<4)
    m=1;  % circular mode
end
if(nargin<5)
    dv_n='z'; % z is chosen to be decision variable
end
if(nargin<6)
    ROM='d';% default
end
if(nargin<7)% DATAs are not provided
    wordwidth=64; % default word width - 64bit
end
if(nargin<8)% acc
    acc=2^(floor(log2(10^(-6))));% about 10^(-6)
end
% preprocess=================================================

% main section===============================================
% initial state----------------------------------------------
xi=x0;yi=y0;zi=z0;m=mode;F=-ones(1,1);D=zeros(1,1);An=1;
for iteration=0:wordwidth+1
    % parameter initialization-------------------------------
    if(dv_n=='x')dv=xi;elseif(dv_n=='y')dv=yi;
    else dv=zi;% default decision variable
    end
    % termination test---------------------------------------
    disable=terminator(dv,acc);
    if(disable)break;
    end%if
    % iteration begins---------------------------------------
    % --------------------------di---------------------------
    di=direction(dv,dv_n);
    D(iteration+1)=di;
    % -------------------------Fi/e--------------------------
    if(ROM=='d')
        NumOfData=wordwidth+1;
        [Fi ei]=Angle2rotate(dv,m,NumOfData,wordwidth);
    else
        Fi=ROM(iteration+1,1);ei=ROM(iteration+1,2);
%         [Fi ei]=Angle2rotate(dv,m,length(ROM),...
%                              wordwidth,ROM);
    end
    F(iteration+1)=Fi;% record Fi
    % --------------------------An---------------------------
    An=An*gainKi(Fi,m);% calculate An
    % ----------------------xi/yi/zi-------------------------
    xi_old=xi;
    xi=xi_old-m*yi*di*2^(-Fi);
    yi=yi+xi_old*di*2^(-Fi);
    zi=zi-di*ei;
end% iteration
% postprocess================================================

%check outputs===============================================
if(nargout<0 || nargout>6)
    xn=0;yn=0;
    disp('too few or too many outputs.\n')
end
if(nargout>0)
    xn=xi;end
if(nargout>1)
    gain=An;end
if(nargout>2)
    [row,column]=size(F);
    if(row<column)Fs=reshape(F,column,row);
    else Fs=F;end
end
if(nargout>3)
    Ds=D';
end
if(nargout>4)
    zn=zi;end
if(nargout>5)
    yn=yi;end
%ends function
end

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -