-
Notifications
You must be signed in to change notification settings - Fork 35
/
Copy pathPermu_Sawada.m
398 lines (392 loc) · 14.6 KB
/
Permu_Sawada.m
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
function P = Permu_Sawada(W,Y,SetupStruc,method)
%%
thA = 20; %% Angle/degree
thU = 3; %% SIR/dB
thU_mul = 10^(thU/10); %if you dont wanna use 'DOA' method as the preprocessing of 'cor' method, set this to inf
interval = 3; %% Field of cor/+-bins
thcor = 0.7; %% Correlation
thcor_single = 0.8;
single_sign = 0; %%select if could judge permutation by one cal of cor
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%
Num = size(W,1);
N = size(W,2);
K_m = size(W,3);
P = zeros(Num,Num,K_m);
Permu = zeros(Num,K_m-1);
Permu_sign = zeros(K_m-1,1); %%% 1 repersents bin aligned,0 not aligned, -1 cant aliged,2 remains to fresh domian,3 caled with hamonic
Permu_cir = zeros(factorial(Num),K_m-1);
Permu_Num = zeros(K_m-1,1);
Permu_NumL = ones(K_m-1,1)*interval*2;
for i = 1:interval
Permu_NumL(i) = i+interval-1;
Permu_NumL(K_m-i) = i+interval-1;
end
Rate_meth = zeros(K_m,4);
%%%% I used the information of angle which should calculated by the inverse of W, and that process is time-consuming.
Angle = SetupStruc.Angle;
Transfer = SetupStruc.Transfer;
if(~exist('Angle','var'))
sprintf('Here should calculate Angle by W+.');
end
if(~exist('method','var'))
method = 'all';
end
if(strcmp(method,'DOA'))
thA = 360;
thU_mul = 0;
elseif(strcmp(method,'cor'))
thcor = -1;
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% DOA
%%%%This is a simple realization without angle interval detection, so 'thA' has not been used.
for i = 2:K_m
trans = permute(Transfer(i,:,:),[2 3 1]);
Pat_count = zeros(Num,1);
W_norm = W;
for i_W = 1:Num %%%%%% Normalize the row of W
W_norm(i_W,:,i) = W(i_W,:,i)/(norm(W(i_W,:,i))+eps);
end
Beam_pat = abs(W_norm(:,:,i)*trans);
if(~strcmp(method,'DOA'))
for j = 1:Num
[max1,pos1] = max(Beam_pat(j,:));
Beam_pat(j,pos1) = -1;
max2 = max(Beam_pat(j,:));
if(max2*thU_mul<=max1)
Permu(j,i-1) = pos1;
Pat_count(pos1) = Pat_count(pos1)+1;
end
end
else
%%%%%%%%%%%%%%%%%%%%%%%% Decided by the energy of the Beam
% for j = 1:Num
% [max1,pos1] = max(Beam_pat);
% [~,pos2] = max(max1);
% x_ = pos1(pos2); y_ = pos2;
% Permu(x_,i-1) = y_;
% Pat_count(y_) = Pat_count(y_)+1;
% Beam_pat(x_,:) = zeros(1,Num)-1;
% Beam_pat(:,y_) = zeros(Num,1)-1;
% end
%%%%%%%%%%%%%%%%%%%%%%%% Decided by the difference of the energy of the Beam
Dif = zeros(Num,2);
for j = 1:Num
[max1,Dif(j,2)] = max(Beam_pat(j,:));
Beam_pat(j,Dif(j,2)) = -1;
max2 = max(Beam_pat(j,:));
Dif(j,1) = max1-max2;
end
for j = 1:Num
[~,pos] = max(Dif(:,1));
Permu(pos,i-1) = Dif(pos,2);
Pat_count(Dif(pos,2)) = Pat_count(Dif(pos,2))+1;
Dif(pos,1) = -1;
for j_p = 1:Num
if(Dif(j_p,2)==Dif(pos,2) && j_p~=pos)
[max1,Dif(j_p,2)] = max(Beam_pat(j_p,:));
Beam_pat(j_p,Dif(j_p,2)) = -1;
max2 = max(Beam_pat(j_p,:));
Dif(j_p,1) = max1-max2;
end
end
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
end
if(~isempty(find(Pat_count>1, 1))) %%%%Judge if there're more than 1 row of W target the same angle.
for j = 1:Num
if(Permu(j,i-1)>0)
if(Pat_count(Permu(j,i-1))>1)
Pat_count(Permu(j,i-1)) = 0;
Permu(j,i-1) = 0;
end
end
end
end
Rate_meth(i-1,1) = length(find(Pat_count==1))/Num;
zer = find(Pat_count==0);
if(isempty(zer))
Permu_sign(i-1) = 2;
elseif(length(zer)==1)
Permu(Permu(:,i-1)==0,i-1) = zer;
Permu_sign(i-1) = 2;
end
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% Correlation method
if(~strcmp(method,'DOA'))
if(isempty(find(Permu_sign==2,1)))
Permu(:,round(K_m/4)) = 1:Num;
Permu_sign(round(K_m/4)) = 2;
end
%%%%% Generate the table of permutation circumstance
Table_Permu = zeros(factorial(Num),Num);
Table_Permu(1,:) = 1:Num;
occup = ones(Num,1);
pin = Num;
for i = 2:factorial(Num)
while(isempty(find(occup==0,1)) || (pin>0 && Table_Permu(i-1,pin)>find(occup==0, 1, 'last' )))
occup(Table_Permu(i-1,pin)) = 0;
pin = pin-1;
end
index = find(occup==0);
Table_Permu(i,pin) = index(find(index>Table_Permu(i-1,pin),1));
occup(Table_Permu(i-1,pin)) = 0;
occup(Table_Permu(i,pin)) = 1;
Table_Permu(i,pin+1:Num) = find(occup==0);
occup = ones(Num,1);
if(pin>1)
Table_Permu(i,1:pin-1) = Table_Permu(i-1,1:pin-1);
end
pin = Num;
end
end
Permu_pending = find(Permu_sign==2);
while(~isempty(Permu_pending))
for i = 1:length(Permu_pending)
x_l = Permu_pending(i)-interval;
x_h = Permu_pending(i)+interval;
if(x_l<1)
x_l = 1;
end
if(x_h>K_m-1)
x_h = K_m-1;
end
for j = x_l:x_h
if(Permu_sign(j)==0)
cor_cir = zeros(size(Table_Permu,1),1);
for k = 1:size(Table_Permu,1)
if(~isempty(find(Permu(:,j)~=0,1)))
verif = find(Permu(:,j)~=0);
sign_verif = 0;
for k_i = 1:length(verif)
if(Table_Permu(k,verif(k_i))~=Permu(verif(k_i),j))
sign_verif = 1;
break;
end
end
if(sign_verif==1)
continue;
end
end
for k_i = 1:Num
y1_temp = abs(Y(:,k_i,j+1));
index = Permu(:,Permu_pending(i))==Table_Permu(k,k_i);
y2_temp = abs(Y(:,index,Permu_pending(i)+1));
cor = corr(y1_temp,y2_temp);
cor_cir(k) = cor_cir(k)+abs(cor);
end
end
% cor_cir = cal_cor(Y(:,:,j+1),Y(:,:,Permu_pending(i)+1)); %%%%%%%%%%%%%%
if(single_sign==1 && max(cor_cir)>=thcor_single*Num)
[~,index] = max(cor_cir);
Permu(:,j) = Table_Permu(index,:)';
Permu_sign(j) = 2;
Rate_meth(j,2) = 0.5;
else
Permu_cir(:,j) = Permu_cir(:,j)*Permu_Num(j)+cor_cir;
Permu_Num(j) = Permu_Num(j)+1;
if(Permu_Num(j)==Permu_NumL(j))%%%%%%%%
[cor_in,index] = max(Permu_cir(:,j));
if(strcmp(method,'cor') || cor_in>=thcor*Num*Permu_Num(j))
Permu(:,j) = Table_Permu(index,:)';
Permu_sign(j) = 2;
Rate_meth(j,2) = 1;
end
end
Permu_cir(:,j) = Permu_cir(:,j)/Permu_Num(j);
end
end
end
Permu_sign(Permu_pending(i)) = 1;
% Permu_cir(:,Permu_pending(i)) = zeros(size(Permu_cir,1),1);
end
Permu_pending = find(Permu_sign==2);
if(isempty(Permu_pending))
if(find(Permu_sign==0,1))
cir0 = find(Permu_sign==0);
icor_max = max(Permu_cir(:,cir0));
[cor_max,i_max] = max(icor_max);
if(strcmp(method,'cor') || cor_max>=thcor*Num)
[~,index] = max(Permu_cir(:,cir0(i_max)));
Permu(:,cir0(i_max)) = Table_Permu(index,:)';
Permu_sign(cir0(i_max)) = 2;
Rate_meth(cir0(i_max),2) = 1;
Permu_pending = cir0(i_max);
end
end
end
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% Harmonic method
if(strcmp(method,'all'))
Harm_lim = floor((K_m-2)/3);
Permu_Harm = zeros(size(Table_Permu,1),Harm_lim);
Permu_NumH = zeros(Harm_lim,1);
for i = 2:Harm_lim
Harmo = [2*i-1 2*i 2*i+1 3*i-1 3*i 3*i+1];
Harmo_Num = 6;
if(i==2)
Harmo = [2*i-1 2*i 2*i+1 3*i 3*i+1];
Harmo_Num = 5;
end
if(Permu_sign(i)==0)
for j = 1:Harmo_Num
cor_cir = zeros(size(Table_Permu,1),1);
if(Permu_sign(Harmo(j))==1)
for k = 1:size(Table_Permu,1)
if(~isempty(find(Permu(:,i)~=0,1)))
verif = find(Permu(:,i)~=0);
sign_verif = 0;
for k_i = 1:length(verif)
if(Table_Permu(k,verif(k_i))~=Permu(verif(k_i),i))
sign_verif = 1;
break;
end
end
if(sign_verif==1)
continue;
end
end
for k_i = 1:Num
y1_temp = abs(Y(:,k_i,i+1));
index = Permu(:,Harmo(j))==Table_Permu(k,k_i);
y2_temp = abs(Y(:,index,Harmo(j)+1));
cor = corr(y1_temp,y2_temp);
cor_cir(k) = cor_cir(k)+abs(cor);
end
end
if(single_sign==1 && max(cor_cir)>=thcor_single*Num)
[~,index] = max(cor_cir);
Permu(:,i) = Table_Permu(index,:)';
Permu_sign(i) = 2;
Rate_meth(i,3) = 0.5;
else
Permu_Harm(:,i) = Permu_Harm(:,i)+cor_cir;
Permu_NumH(i) = Permu_NumH(i)+1;
end
end
end
cor_in = max(Permu_cir(:,i));
[cor_ha,index] = max(Permu_Harm(:,i));
if(cor_ha/(Permu_NumH(i)+eps)>cor_in)
Permu(:,i) = Table_Permu(index,:)';
Permu_sign(i) = 2;
Rate_meth(i,3) = 1;
end
end
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%Recal cor
Permu_pending = find(Permu_sign==2);
while(~isempty(Permu_pending))
for i = 1:length(Permu_pending)
x_l = Permu_pending(i)-interval;
x_h = Permu_pending(i)+interval;
if(x_l<1)
x_l = 1;
end
if(x_h>K_m-1)
x_h = K_m-1;
end
for j = x_l:x_h
if(Permu_sign(j)==0)
cor_cir = zeros(size(Table_Permu,1),1);
for k = 1:size(Table_Permu,1)
if(~isempty(find(Permu(:,j)~=0,1)))
verif = find(Permu(:,j)~=0);
sign_verif = 0;
for k_i = 1:length(verif)
if(Table_Permu(k,verif(k_i))~=Permu(verif(k_i),j))
sign_verif = 1;
break;
end
end
if(sign_verif==1)
continue;
end
end
for k_i = 1:Num
y1_temp = abs(Y(:,k_i,j+1));
index = Permu(:,Permu_pending(i))==Table_Permu(k,k_i);
y2_temp = abs(Y(:,index,Permu_pending(i)+1));
cor = corr(y1_temp,y2_temp);
cor_cir(k) = cor_cir(k)+abs(cor);
end
end
% cor_cir = cal_cor(Y(:,:,j+1),Y(:,:,Permu_pending(i)+1)); %%%%%%%%%%%%%%
if(single_sign==1 && max(cor_cir)>=thcor_single*Num)
[~,index] = max(cor_cir);
Permu(:,j) = Table_Permu(index,:)';
Permu_sign(j) = 2;
Rate_meth(j,4) = 0.5;
else
Permu_cir(:,j) = Permu_cir(:,j)*Permu_Num(j)+cor_cir;
Permu_Num(j) = Permu_Num(j)+1;
if(Permu_Num(j)==Permu_NumL(j))%%%%%%%%
[~,index] = max(Permu_cir(:,j));
Permu(:,j) = Table_Permu(index,:)';
Permu_sign(j) = 2;
Rate_meth(j,4) = 1;
end
Permu_cir(:,j) = Permu_cir(:,j)/Permu_Num(j);
end
end
end
Permu_sign(Permu_pending(i)) = 1;
% Permu_cir(:,Permu_pending(i)) = zeros(size(Permu_cir,1),1);
end
Permu_pending = find(Permu_sign==2);
if(isempty(Permu_pending))
if(find(Permu_sign==0,1))
cir0 = find(Permu_sign==0);
icor_max = max(Permu_cir(:,cir0));
[~,i_max] = max(icor_max);
[~,index] = max(Permu_cir(:,cir0(i_max)));
Permu(:,cir0(i_max)) = Table_Permu(index,:)';
Permu_sign(cir0(i_max)) = 2;
Rate_meth(cir0(i_max),4) = 1;
Permu_pending = cir0(i_max);
end
end
end
end
%% Generate P
for i = 2:K_m
W_inv = pinv(W(:,:,i));
if(~isempty(find(Permu(:,i-1)==0,1)))
P_sign = zeros(Num,1);
for j = 1:Num
if(Permu(j,i-1)~=0)
P_sign(Permu(j,i-1)) = 1;
end
end
for j = 1:Num
if(P_sign(j)==0)
j_sign = j;
P_sign(j_sign) = 1;
break;
end
end
for j = 1:Num
if(Permu(j,i-1)==0)
P(j,j_sign,i) = W_inv(1,j_sign);
for k = j_sign:Num
if(P_sign(k)==0)
j_sign = k;
break;
end
end
else
P(j,Permu(j,i-1),i) = W_inv(1,Permu(j,i-1));
end
end
else
for j = 1:Num
P(j,Permu(j,i-1),i) = W_inv(1,Permu(j,i-1));
end
end
end
%%
% autoPlot(Rate_meth,1);
return;