From 9477dbe667f250ecd23f8fc0d56b942191526421 Mon Sep 17 00:00:00 2001 From: Franciszek Malinka Date: Thu, 25 Feb 2021 14:42:55 +0100 Subject: Stare semestry, niepoukladane --- Semestr 3/anm/cordic/csin.cpp | 49 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 Semestr 3/anm/cordic/csin.cpp (limited to 'Semestr 3/anm/cordic/csin.cpp') 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 +// 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 -- cgit v1.2.3