#include"header.h" // Precompute 32 basic sine xues and 32 basic cosine values. double bsin[] = { 0.479426, 0.247404, 0.124675, 0.062459, 0.031245, 0.015624, 0.007812, 0.003906, 0.001953, 0.000977, 0.000488, 0.000244, 0.000122, 0.000061, 0.000031, 0.000015, 0.000008, 0.000004, 0.000002, 0.000001, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000 }; double bcos[] = { 0.877583, 0.968912, 0.992198, 0.998048, 0.999512, 0.999878, 0.999969, 0.999992, 0.999998, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000 }; int bin[N]; //Initializing the 64 basic sine and cosine values. int initial(void) { int i; double invpow2; for (i = 0; i < N; i++) { invpow2 = pow(2,-(i+1)); bsin[i] = sin(invpow2); bcos[i] = cos(invpow2); } return 0; } //Convert double numbers between 0 and 1 to a binary array. int dtob(double x) { double nx= x*2; int i; for (i = 0; i < N; i++) { bin[i] = (int) nx; nx = 2*(nx - floor(nx)); } return 0; } // The CORDIC part. double cordic(double x) { int i, edge; double sinx, cosx; //Make sure x is between 0.0 and 1.0, if x is out of range, the function stops. if (x < 0 || x > 1) { return 0; } //Convert x to a binary array. dtob(x); // Use addition formula. edge = 0; for (i = 0; i < N; i++){ if (bin[i] == 1) { if (edge != 0) { sinx=sinx*bcos[i]+cosx*bsin[i]; cosx=cosx*bcos[i]-sinx*bsin[i]; } else { sinx=bsin[i]; cosx=bcos[i]; edge = 1; } } } return sinx; }