📄 testcordic.html
字号:
</pre><pre class="codeoutput">Error: Function definitions are not permitted at the prompt or in scripts.</pre><h2>subfunction1-direction<a name="4"></a></h2> <p>In order to find the right direction to rotate, this program was provided.According to the algorithm, it's much too easy to choose di. However, this file was provided separately to make the structure of the program clear. inputs/outputs refer to cordic.m,while dv_name denotes the name of the decision variable </p> <h2>code<a name="5"></a></h2><pre class="codeinput"><span class="keyword">function</span> di=direction(dv,dv_name)<span class="comment">% check the No. of inputs</span><span class="keyword">if</span>(nargin<2) dv_name=<span class="string">'z'</span>; <span class="comment">% default decision variable:z</span><span class="keyword">end</span><span class="comment">% main code section</span>di=1;<span class="keyword">if</span>(dv_name==<span class="string">'x'</span> || dv_name==<span class="string">'y'</span> || dv_name==<span class="string">'z'</span>) <span class="comment">% main code section</span> <span class="keyword">if</span>(dv_name==<span class="string">'z'</span>) <span class="comment">% z is decision variable</span> <span class="keyword">if</span>(dv<0) di=-1; <span class="keyword">end</span> <span class="keyword">else</span> <span class="comment">% x/y is decision variable</span> <span class="keyword">if</span>(dv>=0) di=-1; <span class="keyword">end</span> <span class="keyword">end</span><span class="keyword">else</span> printf(<span class="string">'Wrong decision name!\n'</span>);<span class="keyword">end</span><span class="comment">%end of function</span><span class="keyword">end</span></pre><h2>subfunction2-Angle2rotate<a name="6"></a></h2> <p>this file is very important for at least two reasons. the one is DATAs, which is suppose to be stored in ROM, will be provided in the first part of the routine. The other is, in fact, it has great effect on the steps of iteration.Of course, the angle to rotate at each step is based on the stored DATAs. And angles will be chosen to get the result as quickly as possible accordingly. How many DATAs are required to obtain the acceptable results,further study is badly needed. inputs y or z can be both chosen to decrease.HOWEVER, the same algorithm was employed. NumofData and wordwidth offer the parameters to generate those DATAs. The method is discussed below. Since wordwidth limits range of the datas, and the smallest angle to rotate is arctan(2^(-wordwidth)),the second smallest one is arctan(2^(-wordwidth+1)), and so on. There will be NumOfData of those smallest datas generated and stored unless the last parameter is provided.Or those angles are defined in ROM. outputs out, the best choice for the places to shift and the current angle to rotate format Fi arctan(2^(-Fi)) data format in ROM F0 arctanh(2^-(F0)) F1 arctanh(2^-(F1)) ... and so on </p> <h2>code<a name="7"></a></h2><pre class="codeinput"><span class="keyword">function</span> [Fi ei]=Angle2rotate(z,m,di,NumOfData,wordwidth,ROM)<span class="comment">% DATAs generator</span><span class="keyword">if</span>(nargin<6) <span class="comment">% F is in an increasing order</span> F=wordwidth-(NumOfData-1:-1:0)'; theta=2.^(-F); <span class="keyword">if</span>(m~=-1) ROM=[F atan(theta)]; <span class="keyword">else</span><span class="comment">% m=-1</span> ROM=[F atanh(theta)]; <span class="keyword">if</span>(~F(1))<span class="comment">% F0=0 must be removed</span> ROM=[F(2:end),atanh(theta(2:end))]; <span class="keyword">end</span> <span class="keyword">end</span>save <span class="string">ROM.dat</span> <span class="string">ROM</span> <span class="string">-ascii</span><span class="keyword">end</span><span class="comment">% FIND THE BEST CHOICE FOR ANGLE TO ROTATE</span><span class="comment">% there's only one angle stored</span><span class="keyword">if</span>(NumOfData==1 || length(ROM)==1) Fi=ROM(1);ei=ROM(2);<span class="comment">% there's more than one choice</span><span class="keyword">else</span><span class="comment">% find the minumal difference</span> <span class="keyword">for</span> i=1:length(z) [v,index]=min(abs(z(i,1).*ones(size(ROM,1),1)<span class="keyword">...</span> -di*ROM(:,2))); <span class="comment">% v is useless</span> Fi(i)=ROM(index(1),1); ei(i)=ROM(index(1),2); <span class="keyword">end</span> Fi=Fi';ei=ei';<span class="keyword">end</span><span class="comment">% ends function</span><span class="keyword">end</span></pre><h2>subfunction3-terminator<a name="8"></a></h2><pre> Terminator play an important role in iterative algorithms.In this project, there are mainly two conditions which terminate the process,zi=0 or yi=0.However, the word width for every digital system is finite, which relaxes the conditions. Therefore, we get zi < 2^(-wordwidth) or yi < 2^(-wordwidth)input z or y can play such a role wordwidth is a system parameteroutput signals the end of the process</pre><h2>code<a name="9"></a></h2><pre class="codeinput"><span class="keyword">function</span> disable=terminator(z,wordwidth)<span class="keyword">if</span>(nargin<2) wordwidth=64;<span class="keyword">end</span><span class="keyword">if</span>(abs(z)<2^(-wordwidth+1)) disable=1;<span class="keyword">else</span> disable=0;<span class="keyword">end</span><span class="keyword">end</span></pre><h2>subfunction4-gainAn<a name="10"></a></h2><pre> this routine is written to analysis the relationship between n and An. Theoretically, as n approach infinite, An will be a constant. What's more important, we will study on the difference between the constant and An to look into how many steps the algorithm takes to make the precision of the results up to our expectation. Input F, shifting sequences in the form of comun vector(s) m, mode Output An, gain of the algorithm</pre><h2>code<a name="11"></a></h2><pre class="codeinput"><span class="keyword">function</span> An=gainAn(F,m)[row,column]=size(F);An=ones(column);<span class="comment">% initialize An</span><span class="keyword">if</span>(m)<span class="comment">% m!=0 No linear mode</span> <span class="keyword">for</span> j=1:column Ki=gainKi(F(:,j),m);<span class="comment">% select Fi=i, mode</span> <span class="comment">%compute An for j-th column</span> <span class="keyword">for</span> i=1:row An(j)=An(j)*Ki(i); <span class="keyword">end</span> <span class="keyword">end</span><span class="keyword">end</span><span class="comment">% %plot</span><span class="comment">% plot(n,An,'ro'),hold on</span><span class="comment">% plot(n,An,'g'),hold off</span><span class="comment">% ends function</span><span class="keyword">end</span></pre><h2>subfunction5-gainKi<a name="12"></a></h2><pre> this program can be used to compute the gain of CORDIC processing systems at each iterative step Inputs: m, represents the mode of the computing system Fi, elementary shifts for the i-th iteration CAUTION: m and Fi can be both vectors Outputs: Ki, i-th processing gain</pre><h2>code<a name="13"></a></h2><pre class="codeinput"><span class="keyword">function</span> Ki=gainKi(Fi,m)Ki=sqrt(1+m.*2.^(-2*Fi));<span class="keyword">if</span>(m==-1 && Fi(1)==0)<span class="comment">% when m=-1, Fi can't be 0</span> Ki(1)=1;<span class="keyword">end</span><span class="keyword">end</span></pre><p class="footer"><br> Published with MATLAB® 7.4<br></p> </div> <!--##### SOURCE BEGIN #####%% Test
% test cordic
clc
theta=linspace(0,pi/2,100)';
% function [xn,gain,Fs,Ds,yn,zn]=cordic(x0,y0,z0,mode,...
% dv_n,ROM,wordwidth)
wordwidth=64;
Fs=zeros(wordwidth,length(theta));
Ds=Fs;
for i=1:length(theta)
[xn(i),An(i),F,D,zn(i),yn(i)]...
=cordic(1,0,theta(i),1,'z');
Fs(1:length(F),i)=F;
Ds(1:length(D),i)=D;
%sprintf('theta=%f',theta(i))
end
cosine=xn./An;
sine=yn./An;
true_cosine=cos(theta);% true values
true_sine=sin(theta);
%theta=theta/pi*180;
hold on
plot(theta,cosine,'LineWidth',1.5)
plot(theta,sine,'LineWidth',1.5)
plot(theta,true_cosine,'g')
plot(theta,true_sine,'g')
plot(theta,true_cosine-cosine','r')
plot(theta,true_sine-sine','m')
legend('Cordic cosine','Cordic sine',...
'true cosine','true sine',...
'error cosine','error sine')
xlabel('\theta'),ylabel('value')
hold off
% some statistics
save F.dat Fs -ascii
save D.dat Ds -ascii
%%==========================================================
%% main function-Cordic
% 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=wordwidth;% about 10^(-6)
end
% preprocess=================================================
% main section===============================================
% initial stateREPLACE_WITH_DASH_DASHREPLACE_WITH_DASH_DASHREPLACE_WITH_DASH_DASHREPLACE_WITH_DASH_DASHREPLACE_WITH_DASH_DASHREPLACE_WITH_DASH_DASHREPLACE_WITH_DASH_DASHREPLACE_WITH_DASH_DASHREPLACE_WITH_DASH_DASHREPLACE_WITH_DASH_DASHREPLACE_WITH_DASH_DASHREPLACE_WITH_DASH_DASHREPLACE_WITH_DASH_DASHREPLACE_WITH_DASH_DASHREPLACE_WITH_DASH_DASHREPLACE_WITH_DASH_DASHREPLACE_WITH_DASH_DASHREPLACE_WITH_DASH_DASHREPLACE_WITH_DASH_DASHREPLACE_WITH_DASH_DASHREPLACE_WITH_DASH_DASHREPLACE_WITH_DASH_DASHREPLACE_WITH_DASH_DASH
xi=x0;yi=y0;zi=z0;m=mode;F=zeros(1,1);D=zeros(1,1);An=1;
for iteration=0:wordwidth+1
% parameter initializationREPLACE_WITH_DASH_DASHREPLACE_WITH_DASH_DASHREPLACE_WITH_DASH_DASHREPLACE_WITH_DASH_DASHREPLACE_WITH_DASH_DASHREPLACE_WITH_DASH_DASHREPLACE_WITH_DASH_DASHREPLACE_WITH_DASH_DASHREPLACE_WITH_DASH_DASHREPLACE_WITH_DASH_DASHREPLACE_WITH_DASH_DASHREPLACE_WITH_DASH_DASHREPLACE_WITH_DASH_DASHREPLACE_WITH_DASH_DASHREPLACE_WITH_DASH_DASH-
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -