aboutsummaryrefslogtreecommitdiff
path: root/Semestr 3/anm/cordic/csin.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'Semestr 3/anm/cordic/csin.cpp')
-rw-r--r--Semestr 3/anm/cordic/csin.cpp49
1 files changed, 49 insertions, 0 deletions
diff --git a/Semestr 3/anm/cordic/csin.cpp b/Semestr 3/anm/cordic/csin.cpp
new file mode 100644
index 0000000..bcc9af4
--- /dev/null
+++ b/Semestr 3/anm/cordic/csin.cpp
@@ -0,0 +1,49 @@
+#include "CORDICtable.c"
+#include <iostream>
+// angle is radians multiplied by CORDIC multiplication factor M
+// modulus can be set to CORDIC inverse gain 1/F to avoid post-division
+void CORDICsincos(int a, int m, int *s, int *c)
+{
+ int k, tx, x = m, y = 0, z = a, fl = 0;
+ if (z > +CORDIC_HALFPI)
+ {
+ fl = +1;
+ z = (+CORDIC_PI) - z;
+ }
+ else if (z < -CORDIC_HALFPI)
+ {
+ fl = +1;
+ z = (-CORDIC_PI) - z;
+ }
+ for (k = 0; k < CORDIC_MAXITER; k++)
+ {
+ std::cout << x << " " << y << " " << z << "\n";
+ tx = x;
+ if (z >= 0)
+ {
+ x -= (y >> k);
+ y += (tx >> k);
+ z -= CORDIC_ZTBL[k];
+ }
+ else
+ {
+ x += (y >> k);
+ y -= (tx >> k);
+ z += CORDIC_ZTBL[k];
+ }
+ }
+ if (fl)
+ x = -x;
+ *c = x; // m*cos(a) multiplied by gain F and factor M
+ *s = y; // m*sin(a) multiplied by gain F and factor M
+}
+
+int main()
+{
+ double x;
+ std::cin >> x;
+ int sinus, cosinus;
+ CORDICsincos(x * CORDIC_MUL, CORDIC_1F, &sinus, &cosinus);
+ std::cout << sinus << " " << cosinus << "\n";
+ std::cout << (double)sinus / (double)CORDIC_MUL << " " << (double)cosinus / (double)CORDIC_MUL << "\n";
+} \ No newline at end of file