-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathkary_algorithm.m
More file actions
147 lines (100 loc) · 3.02 KB
/
Copy pathkary_algorithm.m
File metadata and controls
147 lines (100 loc) · 3.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
function [weight] = kary_algorithm(discrete_weights, complex_numbers)
%%%
% INPUTS:
% discrete_weights: array, preferably input as row vector
% complex_numbers: array, preferably input as column vector
% OUTPUTS:
% weight: array of optimal weights for maximzing absolute value of weighted sum of
% complex numbers, where weights are constrained in discrete_weights
%%%
k = length(discrete_weights);
n = length(complex_numbers);
discrete_weights = reshape(discrete_weights, 1, k);
c = reshape(complex_numbers, n, 1);
R = zeros(3, k);
R(1,:)=discrete_weights;
for ii = 1:k
lower_angles = [];
upper_angles = [];
a = discrete_weights(ii);
for jj = 1:k
if jj == ii
continue
end
b = discrete_weights(jj);
lower_angles = [lower_angles, conj((a - b)) * (-1i) ];
upper_angles = [upper_angles, conj(a-b) * 1i ];
end
for jj = 1:k-1
% test which is lower side of cone ii
l = lower_angles(jj);
flag = 0;
for kk = 1:k
if kk==ii
continue
end
if real( (a-discrete_weights(kk)) * l) < 0
flag=1;
end
end
if flag==0
lower=l;
end
end
for jj = 1:k-1
% test which is upper side of cone ii
u = upper_angles(jj);
flag = 0;
for kk = 1:k
if kk==ii
continue
end
if real( (a-discrete_weights(kk)) * u) < 0
flag=1;
end
end
if flag==0
upper=u;
end
end
R(2,ii) = lower ;
R(3,ii) = upper ;
end
maximum = 0;
for ii = 1:n
if c(ii) == 0
continue
end
for jj = 1:k
w = zeros(n,1);
d = c * R(2, jj) / c(ii);
w(ii) = R(1, jj);
for kk = 1:n
if kk == ii
continue
end
if d(kk) == 0
w(kk) = discrete_weights(1); % assigning arbitrary weight for a zero, because it wont matter when maximizing the weighted sum
continue
end
ctr=1;
flag=-1;
while (flag<0)
c1 = real(1i * d(kk) * conj( R(3,ctr))) ;
c2 = real(1i * R(2,ctr) * conj(d(kk))) ;
if ( imag(d(kk)/R(2,ctr)) == 0 && real( d(kk)/R(2,ctr) ) > 0 ) || ( (c1 > 0) && (c2 >= 0) ) % logic: if d(kk) is along lower direction of cone, or it is between lower and upper directions of cone, then assign it to that discrete value
w(kk) = R(1, ctr);
flag=1;
end
ctr = ctr + 1;
end
end
weighted_sum = sum( c .* w);
if abs(weighted_sum) > maximum
optimal_weights = w;
maximum = abs(weighted_sum);
end
end
end
weight = optimal_weights;
end