#include "Header.h" // sins[i] = sin(1/2^(i+1)) // 32 values float sins[] = { 4.794255e-01, 2.474040e-01, 1.246747e-01, 6.245932e-02, 3.124491e-02, 1.562436e-02, 7.812420e-03, 3.906240e-03, 1.953124e-03, 9.765623e-04, 4.882812e-04, 2.441406e-04, 1.220703e-04, 6.103516e-05, 3.051758e-05, 1.525879e-05, 7.629395e-06, 3.814697e-06, 1.907349e-06, 9.536743e-07, 4.768372e-07, 2.384186e-07, 1.192093e-07, 5.960464e-08, 2.980232e-08, 1.490116e-08, 7.450581e-09, 3.725290e-09, 1.862645e-09, 9.313226e-10, 4.656613e-10, 2.328306e-10}; // coss[i] = cos(1/2^(i+1)) // 32 values float coss[] = { 8.775826e-01, 9.689124e-01, 9.921977e-01, 9.980475e-01, 9.995118e-01, 9.998779e-01, 9.999695e-01, 9.999924e-01, 9.999981e-01, 9.999995e-01, 9.999999e-01, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0}; //converts a float to binary unsigned int floatToBin(float x) { int i; unsigned int bit, bin =0; float reminder = x; if(x <0.0 || x>=1.0) { printf("input (%g) is out of range. Input must be >= 0 and < 1\n", x); exit(1); } for(i =0; i < LENGTH; i++) { reminder *= 2.0; bit = (unsigned int) reminder; bin |= (bit<>i) & mask; if(i%4 ==0) printf(" "); printf("%u", bit); } printf("\n"); return 0; } //recursive method for solving sin // sin(a+b) = sin(a) cos(b) + cos(a) sin (b) // // see Header.h for more details... float sinRec(unsigned int bin) { //base case when there is no more bits if(bin == 0) return 0; else //there are still bits { unsigned int i = 1; int j = 0; //find the first bit while((bin & i) == 0) { i = i << 1; j++; } //remove the bit from bin bin = bin ^ i; return (sins[j] * cosRec(bin)) + (coss[j] * sinRec(bin)); } } //recursive method for solving cos // cos(a+b) = cos(a) cos(b) - sin(a) sin(b) // // see Header.h for more details... float cosRec(unsigned int bin) { //base case when there is no more bits if(bin == 0) return 1; else //there are still bits { unsigned int i = 1; int j = 0; //find the first bit while((bin & i) == 0) { i = i << 1; j++; } //remove the bit from bin bin = bin ^ i; return (coss[j] * cosRec(bin)) - (sins[j] * sinRec(bin)); } } //computes sin of num using cordic float cordicSin(float num) { //convert float to binary, then computes sin return sinRec(floatToBin(num)); }