我读了一两篇关于CORDIC的论文,但不太明白。但是我从互联网上下载了一个使用这种算法计算指数函数的代码。它非常有用,可以帮助我在FPGA上触发一个函数的指数项。但现在我正在尝试编写报告,我无法解释CORDIC部分如何工作,我无法与一般CORDIC算法相关。请帮助我,并提前感谢你。
function fx = exp_cordic ( x, n )
a_length = 25;
a = [ ...
1.648721270700128, ...
1.284025416687742, ...
1.133148453066826, ...
1.064494458917859, ...
1.031743407499103, ...
1.015747708586686, ...
1.007843097206488, ...
1.003913889338348, ...
1.001955033591003, ...
1.000977039492417, ...
1.000488400478694, ...
1.000244170429748, ...
1.000122077763384, ...
1.000061037018933, ...
1.000030518043791, ...
1.0000152589054785, ...
1.0000076294236351, ...
1.0000038147045416, ...
1.0000019073504518, ...
1.0000009536747712, ...
1.0000004768372719, ...
1.0000002384186075, ...
1.0000001192092967, ...
1.0000000596046466, ...
1.0000000298023228 ];
e = 2.718281828459045;
x_int = floor ( x );
%
% Determine the weights.
%
poweroftwo = 0.5;
z = x - x_int;
for i = 1 : n
w(i) = 0.0;
if ( poweroftwo < z )
w(i) = 1.0;
z = z - poweroftwo;
end
poweroftwo = poweroftwo / 2.0;
end
%
% Calculate products.
%
fx = 1.0;
for i = 1 : n
if ( i <= a_length )
ai = a(i);
else
ai = 1.0 + ( ai - 1.0 ) / 2.0;
end
if ( 0.0 < w(i) )
fx = fx * ai;
end
end
%
% Perform residual multiplication.
%
fx = fx ...
* ( 1.0 + z ...
* ( 1.0 + z / 2.0 ...
* ( 1.0 + z / 3.0 ...
* ( 1.0 + z / 4.0 ))));
%
% Account for factor EXP(X_INT).
%
if ( x_int < 0 )
for i = 1 : -x_int
fx = fx / e;
end
else
for i = 1 : x_int
fx = fx * e;
end
end
return
end
我做了一些修改并删除了一些代码,并试图让它更简单,它有效,我不知道我做了什么,为什么它仍然工作!!!!
a = [ ...
1.648721270700128, ...
1.284025416687742, ...
1.133148453066826, ...
1.064494458917859, ...
1.031743407499103, ...
];
e = 2.718281828459045;
x_int = floor ( x );
z = x - x_int;
fx = 1.0;
for i = 1 : n
if ( 2^(-i) < z )
z=z-2^(-i);
fx = fx * a(i);
end
end
if ( x_int < 0 )
for i = 1 : -x_int
fx = fx / e;
end
else
for i = 1 : x_int
fx = fx * e;
end
end
return
end
答案 0 :(得分:1)
这使用了众所周知的事实
exp(x+y)=exp(x)*exp(y) and a^(x*y)=(a^x)^y.
输入数x
首先被分解为整数和小数部分x = x_int + z
。 x_int
的指数可以通过任何整数幂算法轻松计算,所呈现的算法相当次优。
因子表是二进制表示中的小数部分
z = z[1]/2+z[2]/4+z[3]/8+…
其中z[i]
为0
或1
。然后第一个循环计算
exp(1/2)^z[1] * exp(1/4)^z[2] * exp(1/8)^z[3]*…
其中第二次取幂为
(z[i]==1) ? exp(1/2^i) : 1
即,只有z[i]==1
的因素实际存在于产品中。