CORDIC算法計算正余弦


網上有很多關於CORDIC算法的資料,看了之后覺得還是wikipedia講述的更加清晰,特此總結+轉載

http://en.wikipedia.org/wiki/CORDIC


 算法思想


 

CORDIC算法是一種對目標值進行逼近的迭代算法,且迭代次數越多精度越高。迭代過程中僅僅需要除2運算和加減運算,因此特別適合硬件方式實現。在單位圓中,圓上角β點的x坐標和y坐標分別對應β的cos和sin值,因此,求角β的正弦值的CORDICn次迭代過程如下:

1、以(1,0)為初始點,向靠近β的方向旋轉arctan(1)=45°得到點v1

2、v1向靠近β的方向旋轉角度arctan(1/2)得到點v2

3、點vi向靠近β的方向旋轉角度arctanc(1/(2^i))得到vi+1

4、當i+1=n時,停止,vn的坐標便是所求正余弦值

坐標旋轉


 

每一次cordic迭代都是以此旋轉計算,通過讓乘以旋轉矩陣來實現,如下式:

旋轉矩陣通過下式來計算

而cos和sin函數可用下式化為tan

因此可化為:

如果讓tan取值,那么vi坐標與矩陣的乘法運算均可用移位來實現,此時旋轉公式為:

其中,

Ki可以在迭代完成后單獨計算,最終只需乘以Kn即可:

隨着n的增加,Kn趨於穩定

取±1,它決定着是順時針旋轉還是逆時針旋轉。

β則根據下式進行逼近,每次迭代都應朝着向β靠近的方向旋轉。

的值可以通過查表法來獲得。

Matlab 代碼


 

 1 function v = cordic(beta,n)
 2 % This function computes v = [cos(beta), sin(beta)] (beta in radians)
 3 % using n iterations. Increasing n will increase the precision.
 4  
 5 if beta < -pi/2 || beta > pi/2
 6     if beta < 0
 7         v = cordic(beta + pi, n);
 8     else
 9         v = cordic(beta - pi, n);
10     end
11     v = -v; % flip the sign for second or third quadrant
12     return
13 end
14  
15 % Initialization of tables of constants used by CORDIC
16 % need a table of arctangents of negative powers of two, in radians:
17 % angles = atan(2.^-(0:27));
18 angles =  [  ...
19     0.78539816339745   0.46364760900081   0.24497866312686   0.12435499454676 ...
20     0.06241880999596   0.03123983343027   0.01562372862048   0.00781234106010 ...
21     0.00390623013197   0.00195312251648   0.00097656218956   0.00048828121119 ...
22     0.00024414062015   0.00012207031189   0.00006103515617   0.00003051757812 ...
23     0.00001525878906   0.00000762939453   0.00000381469727   0.00000190734863 ...
24     0.00000095367432   0.00000047683716   0.00000023841858   0.00000011920929 ...
25     0.00000005960464   0.00000002980232   0.00000001490116   0.00000000745058 ];
26 % and a table of products of reciprocal lengths of vectors [1, 2^-2j]:
27 Kvalues = [ ...
28     0.70710678118655   0.63245553203368   0.61357199107790   0.60883391251775 ...
29     0.60764825625617   0.60735177014130   0.60727764409353   0.60725911229889 ...
30     0.60725447933256   0.60725332108988   0.60725303152913   0.60725295913894 ...
31     0.60725294104140   0.60725293651701   0.60725293538591   0.60725293510314 ...
32     0.60725293503245   0.60725293501477   0.60725293501035   0.60725293500925 ...
33     0.60725293500897   0.60725293500890   0.60725293500889   0.60725293500888 ];
34 Kn = Kvalues(min(n, length(Kvalues)));
35  
36 % Initialize loop variables:
37 v = [1;0]; % start with 2-vector cosine and sine of zero
38 poweroftwo = 1;
39 angle = angles(1);
40  
41 % Iterations
42 for j = 0:n-1;
43     if beta < 0
44         sigma = -1;
45     else
46         sigma = 1;
47     end
48     factor = sigma * poweroftwo;
49     R = [1, -factor; factor, 1];
50     v = R * v; % 2-by-2 matrix multiply
51     beta = beta - sigma * angle; % update the remaining angle
52     poweroftwo = poweroftwo / 2;
53     % update the angle from table, or eventually by just dividing by two
54     if j+2 > length(angles)
55         angle = angle / 2;
56     else
57         angle = angles(j+2);
58     end
59 end
60  
61 % Adjust length of output vector to be [cos(beta), sin(beta)]:
62 v = v * Kn;
63 return
View Code

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM