📄 intsincordic.c
字号:
#include "headDefine.h"
int intSinCordic(unsigned long theta) {
int i; // for iteration
int N = 14; // loop times // NTSC:N=7
// 415097064 = round(0.607253 * 256) = 155
int X[21] = {0}; // 11 = N + 1 432 142
int Y[21] = {0};
int result = 0; // the return result
int precision = 4; // the pricision of the radian 36 -> 0.1
int thetaQuadrant = 0; // the quadrant of theta
int thetaFlag = 0; // 1 means theta1:[PI/4, PI/2]
int S; //
//int quotient;
//
int thetaTable[17] = {51472, 30386, 16055, 8150, 4091, 2047, 1024, 512, 256,
128, 64, 32, 16, 8, 4, 2, 1};
/*int thetaTable[30] = {
1686629713, 995675659, 526087673, 267050317, 134043374, 67087031,
33551702, 16776875, 8388565, 4194299, 2097151, 1048576,
524288, 262144, 131072, 65536, 32768, 16384,
8192, 4096, 2048, 1024, 512, 256, 128, 64, 32, 16, 8, 4};*/
long theta1 = (theta >> 15); // BACK_BIT = 23
// get the (theta1 % (2 * SHORT_PI_CORDIC)), [0, 2*SHORT_PI_CORDIC]
//quotient = floor(theta1 / (2 * SHORT_PI_CORDIC));
//theta1 = theta1 - 2 * SHORT_PI_CORDIC * quotient;
// turn to the range: [-SHORT_PI_CORDIC, SHORT_PI_CORDIC]
//if (theta1 > SHORT_PI_CORDIC && theta1 <= 2 * SHORT_PI_CORDIC) {
// theta1 = theta1 - 2 * SHORT_PI_CORDIC;
//}
// judge the quadrant of theta, and transfer theta1 to the first quadrant
if (theta1 >= 0 && theta1 <= SHORT_PI_CORDIC / 2) { // 第一象限
//theta1 = theta1;
thetaQuadrant = 1;
}
else if (theta1 > SHORT_PI_CORDIC / 2 && theta1 <= SHORT_PI_CORDIC) { // 第二象限
theta1 = SHORT_PI_CORDIC - theta1;
thetaQuadrant = 2;
}
else if (theta1 >= SHORT_PI_CORDIC && theta1 < SHORT_PI_CORDIC / 2 * 3) { // 第三象限
theta1 = theta1 - SHORT_PI_CORDIC;
thetaQuadrant = 3;
}
else if (theta1 >= SHORT_PI_CORDIC / 2 * 3 && theta1 < 2 * SHORT_PI_CORDIC) { // 第四象限
theta1 = 2 * SHORT_PI_CORDIC - theta1;
thetaQuadrant = 4;
}
// judge if theta1 is belongs: [PI/4, PI/2], use sin(theta1) = cos(PI/2 - theta1)
if (theta1 > SHORT_PI_CORDIC / 3 && theta1 <= SHORT_PI_CORDIC / 2) {
theta1 = SHORT_PI_CORDIC / 2 - theta1;
thetaFlag = 1;
X[0] = 146; // 142
}
else if (theta1 >= 0 && theta1 <= SHORT_PI_CORDIC / 3) {
thetaFlag = 0;
X[0] = 432; // 432
}
// SHORT_PI_CORDIC(65536) / 180 = 364表示1度
if (theta1 == 0 || (theta1 >= 10905 && theta1 <= 10941) || (theta1 >= 16366 && theta1 <= 16402) //) {
|| (theta1 >= 21817 && theta1 <= 21863)){ // || theta1 == SHORT_PI_CORDIC / 2) {
if (theta1 == 0) {
if (thetaFlag == 1)
result = 256;
else
result = 0;
}
else if (theta1 >= 10887 && theta1 <= 10959) { // 65536 / 6 = 10923 偏移0.1度的范围:[PI/6-0.1, PI/6+0.1]
if (thetaFlag == 1)
result = 222;
else
result = 128;
}
else if (theta1 >= 16348 && theta1 <= 16420) // 16384 偏移0.1度的范围:[PI/4-0.1, PI/4+0.1]
result = 181;
else if (theta1 >= 21809 && theta1 <= 21881) // 65536 / 3 = 21845 偏移0.1度的范围:[PI/3-0.1, PI/3+0.1]
result = 222;
/*else if (theta1 == SHORT_PI_CORDIC / 2)
result = 256;*/
if (thetaQuadrant == 1 || thetaQuadrant == 2)
result = result;
else if (thetaQuadrant == 3 || thetaQuadrant == 4)
result = -result;
}
else {
// main iterations
//theta1 = theta1;
for (i = 0; i < N; i++) {
// get the sign of S
// that is the clockwise or anticlockwise rotation
if (theta1 > precision)
S = 1;
else if (theta1 < -precision)
S = -1;
else {
break;
}
X[i + 1] = X[i] - S * (Y[i] >> i);
Y[i + 1] = Y[i] + S * (X[i] >> i);
theta1 = theta1 - S * thetaTable[i];
}
if (thetaFlag == 1) {
result = X[i]; // cos
}
else result = Y[i]; // sin
if (thetaQuadrant == 1 || thetaQuadrant == 2)
result = result;
else if (thetaQuadrant == 3 || thetaQuadrant == 4)
result = -result;
}
//result = result / 256;
return(result);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -