-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathbiadmm2.m
429 lines (388 loc) · 14.9 KB
/
biadmm2.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
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
%% Start again mvdr beamformer, hopefully distributed using biadmm. Needs
% to have 50 sensors randomly placed within a 100x100x100 m free space. One
% target speech signal, and one noise signal, also randomly placed. Signal
% of interest is a 20 s speech signal chosen randomly from a 60 s file. fs
% = 16 kHz. Window length is 25 ms with a Hann window and 50% overlap.
% Interference is a randomly placed, zero meaqn gaussian point source with
% power equal to -5, 0, 5 dB when compared to the target signal.
close all; clear;
% % Import target audio
% [s1,fs1] = audioread('273177__xserra__la-vaca-cega-eva.wav');
%
% % Downsample
% fsd = 16e3; % fsd = desired sampling frequency
% fs = fs1/round(fs1/fsd); % fs = actual sampling frequency post resample
% s1 = resample(s1,1,round(fs1/fsd));
%
% % Truncate to desired length, ensure that the length is a multiple of
% % the window length, and randomly select a section of the audio file.
% K = 2^9+1; % K = window length in samples, and the number of frequency bins
% tls = 20; % tls = target length in seconds
% tl = tls*fs-mod(tls*fs,K-1); % tl = target length in samples, adjusted for window length and sampling frequency
% start = floor((length(s1)-tl)*rand);
% s1 = s1(start:start+tl-1,1); % Truncate s1 to be one channel, and 20 s long
%
% % Normalize the target audio file to make it easy to change files
% s1rms = rms(s1);
% s1rmsinv = 1./s1rms;
% s1 = s1 * (0.1*diag(s1rmsinv));% This should probably be scaled down to avoid clipping
%
% % Set up interferer with equal power, i.e. snr = 0 dB
% s1Pow = (s1'*s1) / length(s1);
% s2 = sqrt(s1Pow) * randn(length(s1),1); % Currently set up for equal power
% s2Pow = (s2'*s2) / length(s2);
% SourceSNRdB = 10*log10(s1Pow/s2Pow)
%
% %% STFT
% % pad the source signals so the 1st half window doesn't distort the data
% s1Padded = [zeros((K-1)/2,1);s1;zeros((K-1)/2,1)];
% s2Padded = [zeros((K-1)/2,1);s2;zeros((K-1)/2,1)];
%
% % Take stft of both sources and truncate to exclude negative frequencies
% % as well as dc and fs/2.
% [S1,L] = stft(s1Padded,K);
% S1half = S1(2:(K+1)/2-1,:);
% S1halfreal = real(S1half);
% S1halfimag = imag(S1half);
% [S2,L2] = stft(s2Padded,K);
% S2half = S2(2:(K+1)/2-1,:);
% S2halfreal = real(S2half);
% S2halfimag = imag(S2half);
%
% save('S1halfreal.txt','S1halfreal','-ASCII');
% save('S2halfreal.txt','S2halfreal','-ASCII');
% save('S1halfimag.txt','S1halfimag','-ASCII');
% save('S2halfimag.txt','S2halfimag','-ASCII');
% import data from file for testing with consistent data
fs = 14700;
K = 513;
L = 1149;
S1halfreal = importdata('S1halfreal.txt');
S1halfimag = importdata('S1halfimag.txt');
S1half = S1halfreal + j*S1halfimag;
S2halfreal = importdata('S2halfreal.txt');
S2halfimag = importdata('S2halfimag.txt');
S2half = S2halfreal + j*S2halfimag;
%% Place sensors
M = 100; % M = number of sensors
Nsrcs = 2; % Nsrcs = number of sources
spSize = 10; % spSize = size of the room (m)
space = [spSize, spSize, spSize]'; % Dimensions of the space
spcDim = length(space);
% Mloc = (rand(M,spcDim)*diag(space)).'; % Mloc = matrix containing 3d sensor locations
% sloc = ((rand(Nsrcs,spcDim)*diag(space))).';%+[0,0,2;0,0,2]).'; % sloc = matrix condaining 3d source locations
Mloc = importdata('Mloc.txt'); % For testing with consistent sensor placement
Mloc = Mloc(:,1:M); % Truncate unrequired sensors
sloc = importdata('sloc.txt'); % For testing with consistent source placement
% save('Mloc.txt', 'Mloc', '-ASCII');
% save('sloc.txt', 'sloc', '-ASCII');
% Calculate distances
ssd = zeros(Nsrcs,M);
for ns=1:Nsrcs
for m=1:M
ssd(ns,m) = norm(Mloc(:,m)-sloc(:,ns));
end
end
% Display layout
figure; plot3(Mloc(1,:), Mloc(2,:), Mloc(3,:), '*'); grid on; hold on;
plot3(sloc(1,1), sloc(2,1), sloc(3,1), 'o');
plot3(sloc(1,2), sloc(2,2), sloc(3,2), '^'); legend('Sensors','Target','Interferer')
set(gca, 'fontsize', 14);
%% Create ATFs
Khalf = (K-1)/2-1;
fdom = (fs/(K-1)) * [1:Khalf]';
c = 343; % c = speed of sound in m.s^-1
At = zeros(Khalf,M);
Ai = zeros(Khalf,M);
Atnogain = zeros(Khalf,M);
for m=1:M
At(:,m) = exp(-1i*2*pi*fdom'*ssd(1,m)/c) / (4*pi*ssd(1,m)^2);
Ai(:,m) = exp(-1i*2*pi*fdom'*ssd(2,m)/c) / (4*pi*ssd(2,m)^2);
Atnogain(:,m) = exp(-1i*2*pi*fdom'*ssd(1,m)/c);
end
%% Create observations
X = zeros(Khalf,L,M); Xt = zeros(Khalf,L,M); Xi = zeros(Khalf,L,M);
for l = 1:L
for m = 1:M
Xt(:,l,m) = At(:,m).*S1half(:,l); % These are used for calculating SNR
Xi(:,l,m) = Ai(:,m).*S2half(:,l); % These are used for calculating SNR
X(:,l,m) = Xt(:,l,m)+Xi(:,l,m);
end
end
%% Delay and sum
% W = At; % Atnogain
% [yds,ydsSNRdb] = myBfOp(X,Xt,Xi,W);
% ydsSNRdb
% dsSNRdb = mySnr();
% Covariance of noise and interference, should be I = identity
% Ri = zeros(Khalf,M,M);
% for k = 1:Khalf
% for l = 1:L
% Xtmp = squeeze(Xi(k,l,:));
% Ri(k,:,:) = (1/L)*squeeze(Ri(k,:,:)) + Xtmp*Xtmp';
% end
% end
% White noise gain
% G = zeros(Khalf,1);
% for k = 1:Khalf
% Atmp = Atnogain(k,:).';
% Wtmp = W(k,:).';
% G(k) = (Atmp'*Wtmp*Wtmp'*Atmp)/(Wtmp'*Wtmp);
%
% end
% Gsum = sum(real(G));
% 10*log10(Gsum)
%% MVDR optimum weights
% dl = 1e-9; % dl = diagonal loading factor - ensures that the covariance is invertible
% Wopt = myMvdrOpt(At,X,dl);
% [yopt,yFrSNRdb] = myBfOp(X,Xt,Xi,Wopt);
% yFrSNRdb
% output = [M, ydsSNRdb, yFrSNRdb];
% fid = fopen('SnrVsSensors.txt', 'at');
% fprintf(fid, '%f %f %f\n', output);
% fclose(fid);
% White noise gain
% G = zeros(Khalf,1);
% for k = 1:Khalf
% Atmp = Atnogain(k,:).';
% Wtmp = Wopt(k,:).';
% G(k) = (Atmp'*Wtmp*Wtmp'*Atmp)/(Wtmp'*Wtmp);
%
% end
% Gsum = sum(real(G));
% 10*log10(Gsum)
% Vorobyov "principles of min..."
% SINR = E{|w^H s|^2} / E{|w^H (i+n)|^2} = sigma_s^2 |w^H a(theta_s)|^2 /
% w^H R_i+n w where sigma_s^2 = E{|s(k)|^2} and R_i+n=E{(i(k)+n(k))(i(k)+n(k)^H)}
% sigma_S_sq = zeros(Khalf,1);
% Rin = zeros(Khalf,M,M);
% SINR = zeros(Khalf,1);
% for k=1:Khalf
% for l=1:L
% sigma_S_sq(k) = sigma_S_sq(k) + (1/L)*S1(k,l)'*S1(k,l);
% Rin(k,:,:) = squeeze(Rin(k,:,:)) + (1/L)*(squeeze(Xi(k,l,:))*squeeze(Xi(k,l,:))');
% end
% SINR(k) = (sigma_S_sq(k)*(abs(conj(Wopt(k,:))*At(k,:).')^2))...
% / (conj(Wopt(k,:))*squeeze(Rin(k,:,:))*Wopt(k,:).');
%
% SINRnosig(k) = (conj(Wopt(k,:))*At(k,:).')'*(conj(Wopt(k,:))*At(k,:).')...
% / ((1/M)*(conj(Wopt(k,:))*Wopt(k,:).'));
% end
% Kelson
% WHA = zeros(Khalf,2,2);
% for k=1:Khalf
% WHA(k,:,:) = [conj(Wopt(k,:));conj(Wopt(k,:))] * [At(k,:).', Ai(k,:).'] ;
%
% end
% for l=1:L
% Ytc(:,l) = WHA(:,1,1) .* S1half(:,l);
% Yti(:,l) = WHA(:,1,2) .* S2half(:,l);
% end
% Ytc = [zeros(1,L);Ytc;zeros(2,L);conj(flipud(Ytc))];
% Yti = [zeros(1,L);Yti;zeros(2,L);conj(flipud(Yti))];
% ytc = myOverlapAdd(Ytc);
% yti = myOverlapAdd(Yti);
% SNRKelson = 10*log10((ytc'*ytc) / (yti'*yti))
%% Adaptive Frost MVDR
% mu = 200; % mu = step size
% Iter = 10; % Iter = number of iterations per window
% [Y,W,Wmse] = myFrostAdapt(At,X,mu,Iter,Wopt);
% % [yFrA,yFrASNRdb] = myBfOp(X,Xt,Xi,W);
%
% % Create two sided Y and take istft
% Y = [zeros(1,L);Y;zeros(2,L);conj(flipud(Y))];
% yFrO = myOverlapAdd(Y); % yFrO = y Frost Online
% figure; plot(yFrO);%
% figure; semilogy(Wmse);
% figure; plot(Wmse);
%
% %% Adaptive Frost using actual covariance
% mu = 200; % mu = step size
% Iter = 10; % Iter = number of iterations per window
% [Y,W,Wmse] = myFrostAdaptTrueCov(At,X,mu,Iter,Wopt);
%
% % Create two sided Y and take istft
% Y = [zeros(1,L);Y;zeros(2,L);conj(flipud(Y))];
% yFrO = myOverlapAdd(Y); % yFrO = y Frost Online
% figure; plot(yFrO);
% figure; semilogy(Wmse);
% figure; plot(Wmse);
%% Sparse distributed BiADMM MVDR Matt
% Calculate the adjacency matrix
% Find sensor to sensor distances
sensd = zeros(M,M); % sensd = sensor to sensor distance
for m=1:M
for mm=m:M
sensd(m,mm) = norm(Mloc(:,m)-Mloc(:,mm));
end
end
sensd = sensd + triu(sensd,1).'; % Convert from upper triangular to symmetric
sensdtmp = sensd + diag(ones(1,M)*2*spSize); % 2* spSize is guaranteed to be larger than any distance between sensors
Nnghbrs = 5; % Nnghbrs sets the minimum number of neighbors per node
NNghbrsCheck = 20;
N = -ones(NNghbrsCheck,M); % N = matrix containing indices of the neighbours to each node
C = zeros(M,M);
while min(min(N(1:Nnghbrs,:))) == -1
sensdmin = find(sensdtmp(:)==min(sensdtmp(:)));
[sensdminRow, sensdminCol] = ind2sub(size(sensdtmp),sensdmin(1)); % Find the row and column of the smallest distance left, note that there will be two values as the matrix is symmetric
% You only get to add a neighbor if you have less than Nnghbrs already
if N(1,sensdminRow) == -1 || N(2,sensdminRow) == -1 || N(3,sensdminRow) == -1 || N(4,sensdminRow) == -1 || N(5,sensdminRow) == -1 ||...
N(1,sensdminCol) == -1 || N(2,sensdminCol) == -1 || N(3,sensdminCol) == -1 || N(4,sensdminCol) == -1 || N(4,sensdminCol) == -1
for aa = 1:NNghbrsCheck
if N(aa,sensdminRow) == -1
N(aa,sensdminRow) = sensdminCol; % Add the neighbor to the first empty slot
break
end
end
for aa = 1:NNghbrsCheck
if N(aa,sensdminCol) == -1
N(aa,sensdminCol) = sensdminRow; % Add the neighbor to the first empty slot
break
end
end
C(sensdminRow,sensdminCol) = 1;
C(sensdminCol,sensdminRow) = 1;
end
% Set both copies of the distance just used higher than the possible
% distance, so they cannot be the minimum distance again.
sensdtmp(sensdminRow,sensdminCol) = 2*spSize;
sensdtmp(sensdminCol,sensdminRow) = 2*spSize;
end
Ncell = cell(1,M);
for m=1:M
Ncell{m} = [N(1:min(find(N(:,m)==-1))-1,m);m];
end
% Truncate unused rows of N
% maxNoNghbrs = min(find(sum(N,2)==-M))-1;
% N = N(1:maxNoNghbrs,:);
% Ccross2 = 1./(C'*C); % This results in a lot of inf, maybe that is what
% the 'protected' in the paper is referring to?
% Step through nodes for primal/dual update
W = cell(Khalf,maxNoNghbrs);
% m=1;n=57;
% [M,N] = myChop(N(:,m),m,N(:,n),n);
% [Amn,Anm] = myAconsistency(M,N);
Lnm = zeros();
W = zeros(M,Khalf,maxNoNghbrs);
for l=2
for m=1
% Build consistency matrices Amn and Anm
NM = myChop(N(:,m),m); % NM = list of neighbors of the current node m, note that it includes itself
NMl = length(NM);
Amn = 9*ones(2,NMl,NMl-1); % 9 indicates an unused column, necessary as the number of neighbors varies with node
Anm = 9*ones(2,maxNoNghbrs,NMl-1);
for n=1:NMl-1
NN = myChop(N(:,NM(n)),NM(n));
[AmnTmp,AnmTmp] = myAconsistency(NM,NN);
Amn(:,:,n) = AmnTmp;
Anm(:,1:length(AnmTmp),n) = AnmTmp;
end
% Find neighborhood covariance estimate
% get observations from neighbors
XN = zeros(Khalf,NMl);
for n=1:NMl
XN(:,n) = X(:,l,NM(n));
end
% Calculate local covariance
Rkm = zeros(Khalf,NMl,NMl);
for k=1:Khalf
Rkm(k,:,:) = XN(k,:).'*conj(XN(k,:)); % Need to add C+2, the protected elementwise inverse of the square of the adjacency matrix
end
% Create dk
dk = zeros(Khalf,NMl); % This is pretty wierd, its a big matrix of mostly zeros
dk(:,end) = At(:,l,m);
% Calculate local weight matrix update
Wlplus1 = zeros(Khalf,NMl);
for k=1:Khalf
AmnSum = zeros(NMl,NMl);
ALAWSum = zeros(NMl,1);
for n=1:NMl-1
AmnSum = AmnSum + Amn(:,:,n).'*Amn(:,:,n);
ALAWSum = ALAWSum + Amn(:,:,n).'*(Lnm-Anm(:,1:nonines,NMl)*Wlm(k,n))
end
% Calculate the next adapted weights
Wlplus1(k,:) = (AmnSum + Rkm(k,:,:))\(ALAWSum + dk(k,:));
end
% Calculate local dual update
% Update primal and dual for virtual node
end
end
% for l=2:2
% for m=1:1
%
%
% % aa = 1;
% % % XN = [];
% % Ntmp = [N(1:min(find(N(:,m)==-1))-1,m);m];
% % NoNghbrs = length(Ntmp);
% % % XN = zeros(Khalf,NoNghbrs);
% % Anm = 9*ones(2,10,NoNghbrs-1);
% % for aa = 1:8 % step through current node's neighbors
% % n = Ntmp(aa);
% % n=n
% % Anmtmp = zeros(length(N(1:min(find(N(:,n)==-1))-1,n)),1);
% % for bb = 1:NoNghbrs
% % bb = bb
% % Anmtmp = Anmtmp - (Ntmp(bb) == N(1:min(find(N(:,n)==-1))-1,n));
% % end
% % Anmtmp = [Anmtmp.',-1;-Anmtmp.',1];
% % Anm(:,:,aa) = [Anmtmp,9*ones(2,length(Anm)-length(Anmtmp))]
% % end
% %
% % % Now Anm
% % aa = 1;
% % Ntmp = [N(1:min(find(N(:,m)==-1))-1,m);m];
% % NoNghbrs = length(Ntmp);
% % Anm = 9*ones(2,10,NoNghbrs-1);
% % for aa = 1:8 % step through current node's neighbors
% % n = Ntmp(aa);
% % n=n
% % Anmtmp = zeros(length(N(1:min(find(N(:,n)==-1))-1,n)),1);
% % for bb = 1:NoNghbrs
% % bb = bb
% % Anmtmp = Anmtmp - (Ntmp(bb) == N(1:min(find(N(:,n)==-1))-1,n));
% % end
% % Anmtmp = [Anmtmp.',-1;-Anmtmp.',1];
% % Anm(:,:,aa) = [Anmtmp,9*ones(2,length(Anm)-length(Anmtmp))]
% % end
%
%
%
% % for aa = 1:NoNghbrs
% % Nbrtmp = N(1:min(find(N(:,Ntmp(aa))==-1))-1,Ntmp(aa));
% % Nbrtmplen = length(Nbrtmp);
% % for bb = 1: Nbrtmplen
% % Ntmp(bb)==N(1:Nbrtmp,Ntmp(aa))
% % end
% % end
%
%
%
%
% % for aa = 1:NoNghbrs
% % XN(:,aa) = X(:,l,Ntmp(aa));% Observations in the neighborhood
% % Am = zeros(2,NoNghbrs);
% % for bb = 1:NoNghbrs
% % tmp = (Ntmp(bb)==N(1:NoNghbrs,Ntmp(bb))
% % Am = Am + [tmp.';-tmp.'];
% % end
% % Amn(aa,:,:) = Am;
% % end
%
% % Rm = zeros(Khalf,NoNghbrs,NoNghbrs);
% % for k=1:Khalf
% % Rm(k,:,:) = XN(k,:).'*conj(XN(k,:));
% % end
% % Setup Amn and Anm for node m:
% % Step through neighbors n = N(:,m)
% %
%
%
% % Calculate x(l+1)
% % Calculate lambda(l+1)
% % Calculate virtual node primal update
% % Calculate virtual node dual update
% % Calculate zk
% % Transmit to collection node
% end
% end