- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <math.h>
- int main(int argc,char *argv[])
- {
- int entries, entries_2PI, phase, amplitude, offset, data_per_line;
- int i, j;
- int max, min, max_digits;
- if (argc != 7) {
- fprintf(stderr,"Generate C source code for sine table\n");
- fprintf(stderr,"Usage:\n");
- fprintf(stderr,"sinus_gen entries entries_per_2PI phase amplitude offset data_per_line >outputfile\n\n");
- fprintf(stderr,"entries : number of entries in sine table\n");
- fprintf(stderr,"entries_per_2PI : number of samples within full 360 degree\n");
- fprintf(stderr,"phase : phase offset in samples\n");
- fprintf(stderr,"amplitude : amplitude of sine wave, integer\n");
- fprintf(stderr,"offset : offset of sine wave, integer\n");
- fprintf(stderr,"data_per_line : number of samples per line in source file\n");
- fprintf(stderr,">outputfile : redirect output form terminal to file\n");
- return 1;
- }
- entries = atoi(argv[1]); //180
- entries_2PI = atoi(argv[2]); //180
- phase = atoi(argv[3]); // 0
- amplitude = atoi(argv[4]); // 20
- offset = atoi(argv[5]); // 20
- data_per_line = atoi(argv[6]); // 输出多少行
- min = abs(offset - amplitude);
- max = abs(offset + amplitude);
- max_digits=2;
- if (min > 99 || max > 99) max_digits=3;
- if (min > 999 || max > 999) max_digits=4;
- if (min > 9999 || max > 9999) max_digits=5;
- // header
- printf("const int sinus[%d] = {\n\t", entries);
- for (i=0, j=0; i<entries; i++) {
- switch(max_digits) {
- case 2: printf("%3d", (int)(offset + amplitude*sin((phase+i)*2*3.1415927/*3))); break;
- case 3: printf("%4d", (int)(offset + amplitude*sin((phase+i)*2*3.1415927/entries_2PI))); break;
- case 4: printf("%5d", (int)(offset + amplitude*sin((phase+i)*2*3.1415927/entries_2PI))); break;
- case 5: printf("%6d", (int)(offset + amplitude*sin((phase+i)*2*3.1415927/entries_2PI))); break;
- }
- if (i < (entries-1)) {
- printf(", ");
- }
- j++;
- if (j==data_per_line) {
- if (i < (entries-1)) {
- printf("\n\t");
- } else {
- printf("\n");
- }
- j=0;
- }
- }
- if (j !=0) printf("\n");
- // footer
- printf("};\n");
- return 0;
- }
程序2
- #include "cordic-16bit.h"
- #include <math.h> // for testing only!
- //Print out sin(x) vs fp CORDIC sin(x)
- int main(int argc, char **argv)
- {
- double p;
- int s,c;
- int i;
- for(i=0;i<50;i++)
- {
- p = (i/50.0)*M_PI/2;
- //use 32 iterations
- cordic((p*MUL), &s, &c, 16);
- //these values should be nearly equal
- printf("%f : %f\n", s/MUL, sin(p));
- }
- }
cordic-16bit.h
- //Cordic in 16 bit signed fixed point math
- //Function is valid for arguments in range -pi/2 -- pi/2
- //for values pi/2--pi: value = half_pi-(theta-half_pi) and similarly for values -pi---pi/2
- //
- // 1.0 = 16384
- // 1/k = 0.6072529350088812561694
- // pi = 3.1415926536897932384626
- //Constants
- #define cordic_1K 10000
- #define half_pi 0x00006487
- #define MUL 16384.000000
- #define CORDIC_NTAB 16
- int cordic_ctab [] = {0x00003243, 0x00001DAC, 0x00000FAD, 0x000007F5, 0x000003FE, 0x000001FF, 0x000000FF, 0x0000007F, 0x0000003F, 0x0000001F, 0x0000000F, 0x00000007, 0x00000003, 0x00000001, 0x00000000, 0x00000000, };
- void cordic(int theta, int *s, int *c, int n)
- {
- int k, d, tx, ty, tz;
- int x=cordic_1K,y=0,z=theta;
- n = (n>CORDIC_NTAB) ? CORDIC_NTAB : n;
- for (k=0; k<n; ++k)
- {
- d = z>>15;
- //get sign. for other architectures, you might want to use the more portable version
- //d = z>=0 ? 0 : -1;
- tx = x - (((y>>k) ^ d) - d);
- ty = y + (((x>>k) ^ d) - d);
- tz = z - ((cordic_ctab[k] ^ d) - d);
- x = tx; y = ty; z = tz;
- }
- *c = x; *s = y;
- }