OpenCores
URL https://opencores.org/ocsvn/cordic_atan_iq/cordic_atan_iq/trunk

Subversion Repositories cordic_atan_iq

[/] [cordic_atan_iq/] [cordic_atan_iq.m] - Blame information for rev 8

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 8 Papayaved
 
2
function [angle, isqrt] = cordic_atan_iq(IS, QS);
3
 
4
IS = int32(IS);
5
QS = int32(QS);
6
 
7
tg = double(QS) / double(IS);
8
atg = atan(tg) / pi * 180;
9
fprintf('Given I = %g, Q = %g, angle = %g degree\n', IS, QS, atg);
10
 
11
sign = IS < 0;
12
if sign
13
    IS = -IS;
14
    QS = -QS;
15
end
16
 
17
% [F Exp_I] = log2(double(IS));
18
% [F Exp_Q] = log2(double(QS));
19
%
20
% Exp_I
21
% Exp_Q
22
%
23
% E = max(Exp_I, Exp_Q) + 1;
24
%
25
% IS = int32(IS * 2^(31 - E));
26
% QS = int32(QS * 2^(31 - E));
27
 
28
fprintf('I = 0x%08x, Q = 0x%08x\n', IS, QS);
29
 
30
i = 0;
31
atan_table(1) = int32(1);
32
 
33
while i == 0 || atan_table(i) ~= 0
34
    atan_table(i+1) = int32(2^30 * atan(1/2^i) / (pi/2));
35
    i = i + 1;
36
end
37
 
38
k = 1;
39
for i=1:length(atan_table)
40
   k = k * 1 / sqrt(1 + 2^(-2 * (i-1)));
41
end
42
 
43
x(1) = IS;
44
y(1) = QS;
45
a(1) = int32(0);
46
 
47
tg = double(y(1)) / double(x(1));
48
atg = atan(tg) / pi * 180;
49
fprintf('i = %d: x = %d, y = %d (%g deg), a = %d(%g degree)\n', 0, x(1), y(1), atg, a(1), double(a(1))/2^30 * 90);
50
 
51
for i=2:length(atan_table) % i dont use last table element
52
    if y(i-1) > 0
53
        x(i) = x(i-1) + y(i-1) / 2^(i-2);
54
        y(i) = -x(i-1) / 2^(i-2) + y(i-1);
55
        a(i) = a(i-1) - atan_table(i-1);
56
        fprintf('i = %d: rot = %g deg, ', i-1, -double(atan_table(i-1)) / 2^30 * 90);
57
    else
58
        x(i) = x(i-1) - y(i-1) / 2^(i-2);
59
        y(i) = x(i-1) / 2^(i-2) + y(i-1);
60
        a(i) = a(i-1) + atan_table(i-1);
61
        fprintf('i = %d: rot = %g deg, ', i-1, double(atan_table(i-1)) / 2^30 * 90);
62
    end
63
 
64
    tg = double(y(i)) / double(x(i));
65
    atg = atan(tg) / pi * 180;
66
    fprintf('x = %d, y = %d (%g deg), a = %d(%g deg)\n', x(i), y(i), atg, a(i), double(a(i))/2^30 * 90);
67
end
68
 
69
angle = -double(a(end))/2^30 * 90;
70
if sign
71
    if angle > 0
72
        angle = angle - 180;
73
    else
74
        angle = angle + 180;
75
    end
76
end
77
 
78
isqrt = int32(k * double(x(end)));
79
 
80
end

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.