cant quite understand CORDIC algorithm for computing exponantial functions

374 Views Asked by At

I read a paper or two about CORDIC but can not quite understand it. however i download a code from internet that calculate exponential functions using this algorithm. it was very useful and help me impelenet a exponential term of a function on the FPGA. but now i am trying to write a report and i can not explain how the CORDIC part work and i can not relate to general CORDIC algorithm. plz help me and thank you in advance.

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

and i did some modification and deleted some code and tried to make it simpler and it worked and i do not know what did i do and why it is still work!!!!

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
1

There are 1 best solutions below

3
On BEST ANSWER

This uses the well-known fact that

exp(x+y)=exp(x)*exp(y) and a^(x*y)=(a^x)^y.

The input number x is first decomposed into integer and fractional part x = x_int + z. The exponential of x_int can be easily computed by any integer power algorithm, the presented one is rather sub-optimal.

The table of factors is for the fractional part in its binary representation

z = z[1]/2+z[2]/4+z[3]/8+…

where z[i] is either 0 or 1. The first loop then computes

exp(1/2)^z[1] * exp(1/4)^z[2] * exp(1/8)^z[3]*…

where the second exponentiation is to read as

(z[i]==1) ? exp(1/2^i) : 1 

that is, only factors with z[i]==1 are actually present in the product.