#include "shared.h" /* pre-computed binary sine 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 }; /* pre-computed binary cosine values */ 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]; /* initialize cosine and sine tables */ int inittables(void) { int i; double invpow2; for (i = 0; i < N; i++) { invpow2 = pow(2,-(i+1)); bsin[i] = sin(invpow2); bcos[i] = cos(invpow2); printf("%f, ", bcos[i]); } printf("\n"); return 0; } /* convert double to binary array */ int dtobarr(double val) { double nval= val*2; int i; for (i = 0; i < N; i++) { bin[i] = (int) nval; nval = 2*(nval - floor(nval)); } return 0; } double cordic(double val) { int i, edge; double sinval, cosval; /* check input */ if (val < 0 || val > 1) { printf("usage: inputs should be between 0 and 1\n"); exit(1); } /* convert val to a binary array */ dtobarr(val); /* Deal with edge case first. Once one known sine value * is found, trig addition takes over. */ edge = 0; for (i = 0; i < N; i++) { if (bin[i] == 1) { if (edge != 0) { sinval=sinval*bcos[i]+cosval*bsin[i]; cosval=cosval*bcos[i]-sinval*bsin[i]; } else { sinval=bsin[i]; cosval=bcos[i]; edge = 1; } } } return sinval; }