math - Approximation of `atan` function in fixed point -
i must calculations need use trigonometric functions, , atan
one. code run on atmega328p, , efficiency sake, can't use float
s: i'm using fixed point numbers. thus, can't use standard atan
function.
i have function take value in fixed point format s16_10 (signed, 16 bits width, point in 10th position), , returns s16_6 format. input between 0 , 1 (so 0 , 210), output, in degrees, between -45 , 45 (so -45 * 26 , 45 * 26).
let's y fixed point, s16_6 representation of y, real angle of arc, , x such atan(x) = y
, , x s16_10 representation of x. start approximating atan
function, (0,1) (-45,45) 4th degrees polynomial, , found can use:
y ~= 8.11 * x^4 - 19.67 * x^3 - 0.93 * x^2 + 57.52 * x + 0.0096
which leads to:
y ~= (8.11 * x^4)/2^34 - (19.62* x^3)/2^24 - (0.93 * x^2)/2^14 + (57.52*x)/2^4 + 0.0069 * 2^6
and here stuck... on 1 hand, computing x^4
lead 0 1 fifth of definition interval, , on other , 2n4n in {3, 2, 1} lead 0 value... how ?
some of terms being truncated 0 not disaster; doesn't substantially worsen approximation. simulated fixed precision setup in matlab rounding each term of polynomial nearest integer:
q4 = @(x) round((8.11 * x.^4)/2^34); q3 = @(x) -round((19.62* x.^3)/2^24); q2 = @(x) -round((0.93 * x.^2)/2^14); q1 = @(x) round((57.52*x)/2^4); q0 = @(x) round(0.0069 * 2^6);
it's true on first fifth of interval [0,210] terms q4, q3, q2 rather choppy, , q4 absent.
but these effects of rounding of same size theoretical error of approximation of atan
polynomial. here plot red difference (polynomial-atan) calculated without rounding integers, , green difference (q4+q3+q2+q1+q0-atan):
as can see, rounding not make approximation worse; in cases reduces error happy accident.
i notice polynomial systematically overestimates atan. when fit 4th degree polynomial atan on [0,1] matlab, coefficients different:
8.0927 -19.6568 -0.9257 57.5106 -0.0083
even truncating these 2 significant figures, did, better approximation:
(8.09 * x^4)/2^34 - (19.66* x^3)/2^24 - (0.93 * x^2)/2^14 + (57.52*x)/2^4 - 0.0083 * 2^6
this time truncation integers worsen things. expected outcome of calculation several intermediate results rounded integers off +-2 or so. theoretical accuracy of +-0.5, shown polynomial, cannot realized given arithmetical tools.
Comments
Post a Comment