5172
|
1 |
# compute arctangent table for CORDIC computations in fttrigon.c
|
|
2 |
import sys, math
|
|
3 |
|
|
4 |
#units = 64*65536.0 # don't change !!
|
|
5 |
units = 256
|
|
6 |
scale = units/math.pi
|
|
7 |
shrink = 1.0
|
|
8 |
comma = ""
|
|
9 |
|
|
10 |
def calc_val( x ):
|
|
11 |
global units, shrink
|
|
12 |
angle = math.atan(x)
|
|
13 |
shrink = shrink * math.cos(angle)
|
|
14 |
return angle/math.pi * units
|
|
15 |
|
|
16 |
def print_val( n, x ):
|
|
17 |
global comma
|
|
18 |
|
|
19 |
lo = int(x)
|
|
20 |
hi = lo + 1
|
|
21 |
alo = math.atan(lo)
|
|
22 |
ahi = math.atan(hi)
|
|
23 |
ax = math.atan(2.0**n)
|
|
24 |
|
|
25 |
errlo = abs( alo - ax )
|
|
26 |
errhi = abs( ahi - ax )
|
|
27 |
|
|
28 |
if ( errlo < errhi ):
|
|
29 |
hi = lo
|
|
30 |
|
|
31 |
sys.stdout.write( comma + repr( int(hi) ) )
|
|
32 |
comma = ", "
|
|
33 |
|
|
34 |
|
|
35 |
print ""
|
|
36 |
print "table of arctan( 1/2^n ) for PI = " + repr(units/65536.0) + " units"
|
|
37 |
|
|
38 |
# compute range of "i"
|
|
39 |
r = [-1]
|
|
40 |
r = r + range(32)
|
|
41 |
|
|
42 |
for n in r:
|
|
43 |
|
|
44 |
if n >= 0:
|
|
45 |
x = 1.0/(2.0**n) # tangent value
|
|
46 |
else:
|
|
47 |
x = 2.0**(-n)
|
|
48 |
|
|
49 |
angle = math.atan(x) # arctangent
|
|
50 |
angle2 = angle*scale # arctangent in FT_Angle units
|
|
51 |
|
|
52 |
# determine which integer value for angle gives the best tangent
|
|
53 |
lo = int(angle2)
|
|
54 |
hi = lo + 1
|
|
55 |
tlo = math.tan(lo/scale)
|
|
56 |
thi = math.tan(hi/scale)
|
|
57 |
|
|
58 |
errlo = abs( tlo - x )
|
|
59 |
errhi = abs( thi - x )
|
|
60 |
|
|
61 |
angle2 = hi
|
|
62 |
if errlo < errhi:
|
|
63 |
angle2 = lo
|
|
64 |
|
|
65 |
if angle2 <= 0:
|
|
66 |
break
|
|
67 |
|
|
68 |
sys.stdout.write( comma + repr( int(angle2) ) )
|
|
69 |
comma = ", "
|
|
70 |
|
|
71 |
shrink = shrink * math.cos( angle2/scale)
|
|
72 |
|
|
73 |
|
|
74 |
print
|
|
75 |
print "shrink factor = " + repr( shrink )
|
|
76 |
print "shrink factor 2 = " + repr( shrink * (2.0**32) )
|
|
77 |
print "expansion factor = " + repr(1/shrink)
|
|
78 |
print ""
|
|
79 |
|