Assignment statement
Background theory
Consider a phasor at an angle of Ø (0 <= Ø <= 90°) with projections on the x and y axes as x0 and y0, respectively.
Let it be rotated by an angle of Ø0 (0 <= Ø0 < 90°) so that it is now at an angle of Ø'0 (0 <= Ø'0 <= 90°) with projections on the x and y axes as x1 and y1, respectively.
It can be easily shown that x1 and y1 are related to x0 and y0 as follows:
x1 = x0 * cos(Ø0) - y0 * sin(Ø0)
y1 = x0 * sin(Ø0) + y0 * cos(Ø0)
These equations above can be re-written as:
x1 = cos(Ø0) * [ x0 - y0 * tan(Ø0) ]
y1 = cos(Ø0) * [ x0 * tan(Ø0) + y0 ]
Let the phasor it be rotated for the ith time by an angle of Øi (0 <= Øi < 90°) so that it is now at an angle of Ø'i (0 <= Ø'i <= 90°) with projections on the x and y with projections on the x and y axes as xi and yi, respectively.
The new projections on the x and y axes will be:
xi+1 = cos(Øi) * [ xi - yi * tan(Øi) ]
yi+1 = cos(Øi) * [ xi * tan(Øi) + yi ]
If repeated substitutions are made, then there will be a leading product term
Ci = ∏j=1 to i cos(Øj)
We choose the angles Øi as tan-1(2-i)
Then,
C∞ = ∏j=0 to ∞ (2j / √(1+2j)) ≈ 0.0607253 ≈ 39797/65536 = 39797 / 216
We can avoid the final multiplication by taking sufficient number of terms and accomodate for C∞ in the initial values of x0 and y0.
We can approximate any angle Ø (0 <= Ø <= 90°) by the as Ø ≈ ∑i=0 to N ±Øi.
The above mentioned technique can then be used to compute both sin(Ø) and cos(Ø) without requiring the use of multiplication or division operations.
Problem statement
Write a program to read an angle Ø scaled up by 100. Use only integer varilables for the actual cordic computation (excluding the pre-processing for computing the table of angles). Let Ø' be the approximation of Ø by your algorithm. Your program should compute the sin(Ø') and cos(Ø'). In your program you should not use any multiplication or division operation. You should implement division by 2 using the bitwise right shift operator avaialble in 'C' (>>). You should store values of tan-1(2-i), (0 <= i < 16), scaled up by 100 in an integer array called angles. Initialize the array in your program so angles[i]=arctan(2-i).
At the end of your computation, prior to the final division by 216, your results will be scaled up by 216. Note, that if you do an integer division, then your result will be zero (as sine and cosine values never exceed one).
For reporting the final result, you should use floating point division for the final division (by type casting the operands to float)
As the computation progresses, report the intermediate (scaled) Ø' and values of (xi) sin(Ø') and (yi) cos(Ø') computed by cordic (these will be int values). Finally, report the acutal values of Ø and values of sin(Ø) and cos(Ø) as float values and also the corresponding values given by the math library functions.
Sample output
The numbers below are fictitious values, given only to illustrate the correct format
84 9425 65198
100 1003 65529
0.000000 0.000000 1.000000
0.000000 0.000000 1.000000
The last row shows the given value of Ø and sine and cosine values reported by the library functions
The penultimate line shows the approximated values Ø' and sine and cosine values computed using the cordic procedure, but converted to floating point numbers
The earlier numbers show the intermediate values of Ø' and sine and cosine values as int values