0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017 clear
0018 close all force
0019
0020
0021
0022
0023
0024 mname = mfilename;
0025 mpath = mfilename('fullpath');
0026 mdir = mpath(1:end-length(mname));
0027 cd(mdir);
0028
0029 addpath utils/
0030 addpath utils/utils_Wavelet
0031 addpath utils/utils_LOT
0032 addpath solvers/
0033
0034 fprintf(['----------',datestr(now),'-------%s------------------\n'],mname)
0035
0036
0037
0038 rseed = 2013;
0039 rand('state',rseed);
0040 randn('state',rseed);
0041
0042
0043 mType = 'sign';
0044 sType = 'LinChirp';
0045 SNR = 35;
0046
0047 N = 256;
0048 R = 8;
0049 M = round(N/R);
0050
0051
0052 sig_length = 2^15;
0053
0054
0055 P = 5;
0056
0057
0058 eType = 'sym';
0059
0060
0061 avg_output = 0;
0062
0063
0064 delx_mode = 'mil';
0065
0066 verbose = 0;
0067
0068
0069
0070 eta = 1/2*N;
0071 eta1 = eta;
0072 lp = N;
0073
0074 in_Psi = []; in_Psi.L = lp; in_Psi.eta = eta; in_Psi.eta1 = eta1;
0075 Psi = create_LOT(in_Psi);
0076
0077 in_Psi.eta1 = 0;
0078 Psi_right = create_LOT(in_Psi);
0079
0080 ETA = [eta; eta1];
0081 Lp = [lp];
0082
0083
0084
0085
0086
0087
0088
0089
0090 SM = 3;
0091 SM3_overlap = 0;
0092 if SM3_overlap == 0
0093
0094 LM = N;
0095 LeftEdge_trunc = 0;
0096 else
0097
0098
0099 LM = 2*N;
0100 LeftEdge_trunc = 0;
0101 end
0102 LeftProj_cancel = 1;
0103
0104 fprintf('Streaming signal measureed and reconstructed with overlapping windows using LOT ... \n');
0105
0106 str0 = sprintf('mType-%s, sType-%s, SNR = %d, (N,M,R) = %d, %d, %d, P = %d, SM = %d, LM = %d, \n eta_frac = %4.3g, eType = %s; specified signal-length = %d.', mType, sType, SNR, N, round(N/R), R, P, SM, LM, eta/N, eType, sig_length);
0107 disp(str0);
0108
0109
0110 in = []; in.type = sType; in.take_fwt = 0; in.Np = 4*N;
0111
0112 sig = genSignal(sig_length,in);
0113 sig = [zeros(2*eta+1,1); sig];
0114 sig_length = length(sig);
0115
0116
0117 alpha_vec = apply_LOT(sig,N,eta);
0118 figure(123); imagesc(reshape(alpha_vec,N,length(alpha_vec)/N));
0119 axis xy;
0120
0121
0122
0123
0124 streaming_iter = ceil(length(sig)/N);
0125 SIM_stack = cell(streaming_iter,1);
0126 SIM_memory = cell(streaming_iter,1);
0127
0128 x_vec = zeros(N*streaming_iter,1);
0129 xh_vec = zeros(N*streaming_iter,3);
0130 sig_vec = zeros(length(sig),1);
0131 sigh_vec = zeros(length(sig),3);
0132
0133
0134 in = [];
0135 in.P = P; in.Psi = Psi;
0136 ETA = eta*ones(P+1,1);
0137 Lp = N*ones(P,1);
0138 eta = ETA(1);
0139 eta1 = ETA(end);
0140 if length(ETA)~=P+1
0141 error('number of LOT parameters not correct');
0142 end
0143 in.ETA = ETA; in.Lp = Lp;
0144 PSI = create_PSI(in);
0145
0146
0147 in = []; in.type = mType; in.parts = 4;
0148 genAmat_h = @(M,N) genAmat(M,N,in);
0149 in.P = P+1-floor(LM/N);
0150 in.LM = LM; in.M = M; in.N = N;
0151 PHI = create_PHI(in);
0152
0153
0154 T_length = 2*eta+P*N;
0155 t_ind = 1:T_length;
0156
0157 sigt = sig(t_ind);
0158 x = PSI'*sigt;
0159
0160 if SM3_overlap == 0
0161 PSI_M = PSI(1:end-2*eta1,:);
0162 else
0163 PSI_M = PSI(1:end-2*eta1,:);
0164 end
0165 A = PHI*PSI_M;
0166 if SM3_overlap == 0
0167 y = PHI*sigt(1:end-2*eta1);
0168 else
0169 y = PHI*sigt(1:end-2*eta1);
0170 end
0171
0172 leny = length(y);
0173 sigma = sqrt(norm(y)^2/10^(SNR/10)/leny);
0174 e = randn(leny,1)*sigma;
0175 y = y+e;
0176
0177
0178
0179 tau = max(1e-2*max(abs(A'*y)),sigma*sqrt(log(P*N)));
0180
0181 maxiter = 2*P*N;
0182 err_fun = @(z) (norm(x-z)/norm(x))^2;
0183
0184
0185 in = [];
0186 in.tau = tau; W = tau;
0187 in.W = W;
0188 in.delx_mode = delx_mode;
0189 in.debias = 0;
0190 in.verbose = 0;
0191 in.plots = 0;
0192 in.record = 1;
0193 in.err_fun = err_fun;
0194 tic
0195 for wt_itr = 1:5
0196
0197 out = l1homotopy(A,y,in);
0198 xh = out.x_out;
0199 iter_bpdn = out.iter;
0200 time_bpdn = toc;
0201 gamma_bpdn = out.gamma;
0202
0203
0204 xh_old = xh;
0205
0206 alpha = 1; epsilon = 1;
0207 beta = M*(norm(xh,2)/norm(xh,1))^2;
0208 W = tau/alpha./(beta*abs(xh)+epsilon);
0209
0210 W_old = W;
0211 yh = A*xh;
0212 Atr = A'*(A*xh-y);
0213 u = -W.*sign(xh)-Atr;
0214 pk_old = Atr+u;
0215
0216 in = out;
0217 in.xh_old = xh;
0218 in.pk_old = pk_old;
0219 in.u = u;
0220 in.W_old = W_old;
0221 in.W = W;
0222 end
0223 W = W_old;
0224
0225 sim = 1;
0226 x_vec((sim-1)*N+1:sim*N,1) = x(1:N);
0227 xh_vec((sim-1)*N+1:sim*N,1:3) = [xh(1:N) xh(1:N) xh(1:N)];
0228
0229 s_ind = 1:N+2*eta1;
0230 sig_temp = Psi*xh(1:N);
0231 s_offset = 0;
0232
0233 sig_vec(s_ind) = sigt(s_ind);
0234 sigh_vec(s_ind,1:3) = sigh_vec(s_ind,1:3)+[sig_temp sig_temp sig_temp];
0235
0236
0237 estimate_buffer = repmat(xh(1:(P-1)*N,1),1,P-1)/(P-1);
0238
0239 xh_streamingRWT = xh;
0240 x_sparsa = xh;
0241 x_yall1 = xh;
0242
0243 done = 0;
0244 while ~done
0245
0246
0247
0248
0249 y_old = y; x_old = x;
0250
0251 sigt_old = sigt; t_ind_old = t_ind;
0252 PHI_old = PHI;
0253 PSI_old = PSI;
0254
0255
0256
0257 Psi_out = PSI(1:N+ETA(1)+ETA(2),1:N);
0258 ETA_old = ETA;
0259 ETA = [ETA(2:end); eta];
0260 Lp_old = Lp;
0261 Lp = [Lp(2:end); N];
0262 eta = ETA(1); eta1 = ETA(end);
0263 in.ETA = ETA; in.Lp = Lp;
0264
0265
0266
0267 t_ind = t_ind+N;
0268 if t_ind(end) > length(sig)
0269 break;
0270 end
0271 sigt = sig(t_ind);
0272
0273
0274
0275 PHI(1:end-M,1:end-N) = PHI(M+1:end,N+1:end);
0276
0277 Phi = genAmat_h(M,LM);
0278 PHI(end-M+1:end,end-LM+1:end) = Phi;
0279
0280 A = PHI*PSI_M;
0281 if SM3_overlap == 0
0282 y = PHI*sigt(1:end-2*eta);
0283 else
0284 y = PHI*sigt(1:end-2*eta1);
0285 end
0286
0287
0288
0289
0290 x = PSI'*sigt;
0291
0292
0293 e(1:end-M) = e(M+1:end);
0294 e(end-M+1:end) = randn(M,1)*sigma;
0295 y= y+e;
0296
0297 A0 = A; x0 = x; y0 = y;
0298 for solver = {'l1homotopy','sparsa','yall1'}
0299 solver = char(solver);
0300 switch solver
0301 case 'l1homotopy'
0302 xh = xh_streamingRWT;
0303 case 'sparsa'
0304 xh = x_sparsa;
0305 case 'yall1'
0306 xh = x_yall1;
0307 end
0308 y = y0; A = A0; x = x0;
0309 xh_old = xh;
0310
0311
0312
0313
0314
0315
0316
0317
0318
0319 if LeftProj_cancel
0320 y(1:M) = y(1:M)-PHI(1:M,1:2*eta)*(Psi_out(end-2*eta+1:end,:)*xh_old(1:N));
0321 end
0322
0323
0324 xh(1:end-N) = xh(N+1:end);
0325
0326
0327
0328
0329
0330
0331
0332 xh1 = xh(end-N+1:end);
0333
0334 if strcmpi(eType,'per')
0335
0336 xh1 = xh(end-N+1:end);
0337 else
0338 sig_temp = PSI_M(1:end-N,1:(P-1)*N)*xh(1:end-N);
0339 sig_temp(1:2*eta) = sig_temp(1:2*eta)+Psi(end-2*eta+1:end,:)*xh_old(1:N);
0340
0341
0342
0343 switch eType
0344 case 'asym'
0345
0346 sig_temp2 = [sig_temp; -sig_temp(end:-1:1); sig_temp];
0347 case 'sym'
0348
0349 sig_temp2 = [sig_temp; sig_temp(end:-1:1); sig_temp];
0350 otherwise
0351 disp('cant do it sire');
0352 end
0353 sig_temp2 = sig_temp2(1:P*N+2*eta);
0354 xh_temp = PSI'*sig_temp2;
0355 xh1 = xh_temp(end-N+1:end);
0356 end
0357
0358 gamma1 = find(abs(xh1)>tau);
0359 [val ind] = sort(abs(xh1),'descend');
0360 gamma1 = ind(1:min(length(gamma1),ceil(M/log2(N))));
0361
0362 yt = y(end-M+1:end)-A(end-M+1:end,1:end-N)*xh(1:end-N);
0363 At = A(end-M+1:end,end-N+1:end);
0364 gamma3 = gamma1;
0365
0366
0367
0368
0369
0370
0371
0372 xh_t1 = 0*xh(end-N+1:end);
0373 xh_t1(gamma3) = pinv(At(:,gamma3)'*At(:,gamma3)+2*tau*eye(length(gamma3)))*(At(:,gamma3)'*yt);
0374 xh_t1(abs(xh_t1)<tau/sqrt(log(N))) = 0;
0375 xh(end-N+1:end) = xh_t1;
0376
0377
0378
0379
0380
0381
0382
0383
0384
0385
0386
0387
0388
0389
0390
0391
0392
0393
0394
0395 fig(111);
0396 plot([x xh]);
0397
0398
0399 if LeftEdge_trunc
0400
0401 alpha0h = xh(1:N);
0402 xh = xh(N+1:end);
0403 y = y-A(:,1:N)*alpha0h;
0404
0405 A = A(:,N+1:end);
0406 alpha0 = x(1:N);
0407 x = x(N+1:end);
0408 end
0409
0410
0411 epsilon = 1;
0412 beta = max(epsilon,M*(norm(xh,2)/norm(xh,1))^1);
0413 W = tau/alpha./(beta*abs(xh)+epsilon);
0414
0415 W_old = W;
0416
0417
0418 if strcmpi(solver,'l1homotopy')
0419
0420 homotopy_mode = 'dummy';
0421 switch homotopy_mode
0422 case 'dummy'
0423
0424
0425
0426
0427 gamma = find(xh);
0428 M_trunc = size(A,1);
0429
0430 if length(gamma) >= M_trunc
0431 disp('length of gamma exceeded number of rows');
0432 [xh_sort ind_sort] = sort(abs(xh),'descend');
0433 xh(ind_sort(M_trunc+1:end)) = 0;
0434 gamma = ind_sort(1:M_trunc);
0435 end
0436 Atr = A'*(A*xh-y);
0437 u = -W.*sign(xh)-Atr;
0438 pk_old = Atr+u;
0439 otherwise
0440 didp('Go back ... no escape');
0441 end
0442
0443
0444 in = out;
0445 gamma_old = gamma;
0446 in.gamma = gamma_old;
0447 switch delx_mode
0448 case 'mil';
0449
0450
0451
0452
0453 AtAgx = A(:,gamma_old)'*A(:,gamma_old);
0454 iAtAgx = pinv(AtAgx);
0455 in.iAtA = iAtAgx;
0456 case {'qr','chol'};
0457
0458 [Q R] = qr(A(:,gamma_old),0);
0459 in.Q = Q; in.R = R;
0460 case 'qrM'
0461
0462 [Q0 R0] = qr(A(:,gamma_old));
0463 in.Q0 = Q0; in.R0 = R0;
0464 end
0465
0466 in.xh_old = xh;
0467 in.pk_old = pk_old;
0468 in.u = u;
0469 in.W = W;
0470 in.delx_mode = delx_mode;
0471 in.debias = 0;
0472 in.verbose = 0;
0473 in.plots = 0;
0474 in.record = 1;
0475 in.err_fun = @(z) (norm(x-z)/norm(x))^2;
0476 tic
0477 out = l1homotopy(A,y,in);
0478 time_streamingRWT = toc;
0479 xh_streamingRWT = out.x_out;
0480 gamma_streamingRWT = out.gamma;
0481 iter_streamingRWT = out.iter;
0482
0483
0484
0485
0486
0487
0488
0489
0490
0491
0492
0493
0494
0495
0496
0497
0498
0499
0500
0501
0502
0503
0504
0505
0506 if LeftEdge_trunc
0507 xh_streamingRWT = [alpha0h; xh_streamingRWT];
0508 x = [alpha0; x];
0509 end
0510 elseif strcmpi(solver,'sparsa')
0511
0512 x_sparsa = xh; W_sparsa = W/tau; iter_sparsa = 0; time_sparsa = 0;
0513 if norm(y) > 1e-3
0514 psi_function = @(x,tau) soft(x,tau*W_sparsa);
0515 phi_function = @(x) sum(abs(W_sparsa.*x));
0516 tic;
0517 [x_sparsa,x_debias_SpaRSA_m,obj_SpaRSA_m_cont,...
0518 times_SpaRSA_m_cont,debias_start_SpaRSA_m,mse_SpaRSA_m,taus_SpaRSA_m, numA, numAt]= ...
0519 SpaRSA_adpW(y,A,tau,...
0520 'Monotone',0,...
0521 'adp_wt',0,...
0522 'W_new',W_sparsa,...
0523 'Debias',0,...
0524 'Initialization',x_sparsa,...
0525 'StopCriterion',2,...
0526 'ToleranceA',1e-4,...
0527 'psi',psi_function,...
0528 'phi',phi_function,...
0529 'Safeguard',1,...
0530 'MaxiterA',maxiter,...
0531 'Verbose',0,...
0532 'True_x',x,...
0533 'Continuation',1,...
0534 'Continuationsteps',-1);
0535
0536 time_sparsa = toc;
0537 iter_sparsa = (numA+numAt)/2;
0538 error_sparsa = norm(x-x_sparsa)/norm(x);
0539 end
0540 if LeftEdge_trunc
0541 x_sparsa = [alpha0h; x_sparsa];
0542 x = [alpha0; x];
0543 end
0544 elseif strcmpi(solver,'yall1')
0545
0546
0547 digit = 4; if sigma > 0; digit = 4; end
0548 opts = [];
0549 opts.tol = 10^(-digit);
0550 opts.weights = W/tau;
0551 opts.print = 0;
0552 opts.maxit = maxiter;
0553 opts.x0 = xh;
0554 opts.nu = 0; opts.rho = tau;
0555 tic;
0556 [x_yall1,Out_yall1] = yall1(A,y,opts);
0557
0558 time_yall1 = toc;
0559 iter_yall1 = (Out_yall1.cntA+Out_yall1.cntAt)/2;
0560 err_yall1 = norm(x-x_yall1)/norm(x);
0561 if LeftEdge_trunc
0562 x_yall1 = [alpha0h; x_yall1];
0563 x = [alpha0; x];
0564 end
0565 end
0566 end
0567
0568 sim = sim+1;
0569 SIM_stack{sim} = [sim, tau, ...
0570 norm(x-xh_streamingRWT)^2/norm(x)^2, sum(iter_streamingRWT,2), sum(time_streamingRWT,2), ...
0571 norm(x-x_sparsa)^2/norm(x)^2, sum(iter_sparsa,2), sum(time_sparsa,2), ...
0572 norm(x-x_yall1)^2/norm(x)^2, sum(iter_yall1,2), sum(time_yall1,2)];
0573
0574
0575 if mod(sim-1,verbose) == 0 && verbose
0576 fprintf('streaming iter. %d. tau = %3.4g, (err,iter,time): streamingRWT homotopy-%3.4g,%3.4g,%3.4g, SpaRSA-%3.4g,%3.4g,%3.4g, YALL1-%3.4g,%3.4g,%3.4g. \n', ...
0577 SIM_stack{sim});
0578 end
0579
0580
0581 fig(1); subplot(211);
0582 plot([x xh_streamingRWT x_sparsa x_yall1]);
0583 title('Comparison betweeen the original and reconstructed signal')
0584
0585
0586 xh = xh_streamingRWT;
0587 x_vec((sim-1)*N+1:sim*N,1) = x(1:N);
0588
0589
0590 estimate_buffer = [[estimate_buffer(N+1:end,2:end); zeros(N,P-2)] xh(1:end-N)/(P-1)];
0591 if avg_output
0592 xh_est = xh(1:N);
0593 xh(1:N) = sum(estimate_buffer(1:N,:),2);
0594
0595 if sim == 2
0596 disp('output is averaged');
0597 end
0598 end
0599 xh_vec((sim-1)*N+1:sim*N,1) = xh(1:N);
0600 xh_vec((sim-1)*N+1:sim*N,2) = x_sparsa(1:N);
0601 xh_vec((sim-1)*N+1:sim*N,3) = x_yall1(1:N);
0602
0603 if SM == 0
0604
0605 s_ind = t_ind(1:N+2*eta)-2*eta;
0606 sig_vec(t_ind(1:N)) = sigt(1:N);
0607 sigh_vec(s_ind,1) = sigh_vec(s_ind,1)+Psi*xh(1:N);
0608 else
0609 s_ind = t_ind(1:N+2*eta);
0610 sig_vec(s_ind) = sigt(1:N+2*eta);
0611 sigh_vec(s_ind,1) = sigh_vec(s_ind,1)+Psi*xh(1:N);
0612 sigh_vec(s_ind,2) = sigh_vec(s_ind,2)+Psi*x_sparsa(1:N);
0613 sigh_vec(s_ind,3) = sigh_vec(s_ind,3)+Psi*x_yall1(1:N);
0614 end
0615
0616
0617
0618 fig(1); subplot(212)
0619 if sim < P*N+eta*P
0620 plot([sig_vec(1:s_ind(end)) sigh_vec(1:s_ind(end),1)]);
0621 else
0622 plot([sig_vec(s_ind(end)-P*N-2*eta+1:s_ind(end)) sigh_vec(s_ind(end)-P*N-2*eta+1:s_ind(end),1)]);
0623 end
0624
0625
0626 if mod(sim,25) == -1
0627 fig(3);
0628 subplot(211); spectrogram(sig_vec(1:s_ind(end)),N,0,N,length(sig),'yaxis');
0629 subplot(212); spectrogram(sigh_vec(1:s_ind(end),1),N,N-1,N,length(sig),'yaxis'); shg
0630 end
0631 drawnow;
0632 end
0633
0634 mS = sum(cell2mat(SIM_stack),1);
0635 fprintf('Summed results: streaming_iter %d. tau = %3.4g, \n solver-(err,iter,time): \n streamingRWT homotopy-%3.4g,%3.4g,%3.4g; \n SpaRSA-%3.4g,%3.4g,%3.4g; \n YALL1-%3.4g,%3.4g,%3.4g. \n', streaming_iter, mS(2:end));
0636 mS = mean(cell2mat(SIM_stack),1);
0637 fprintf('Average results: streaming_iter %d. tau = %3.4g, \n solver-(err,iter,time): \n streamingRWT homotopy-%3.4g,%3.4g,%3.4g; \n SpaRSA-%3.4g,%3.4g,%3.4g; \n YALL1-%3.4g,%3.4g,%3.4g. \n', streaming_iter, mS(2:end));
0638
0639
0640 err_l1homotopy = norm(sig_vec(2*eta+1:N*sim)-sigh_vec(2*eta+1:N*sim,1))^2/norm(sig_vec(2*eta+1:N*sim))^2;
0641 err_sparsa = norm(sig_vec(2*eta+1:N*sim)-sigh_vec(2*eta+1:N*sim,2))^2/norm(sig_vec(2*eta+1:N*sim))^2;
0642 err_yall1 = norm(sig_vec(2*eta+1:N*sim)-sigh_vec(2*eta+1:N*sim,3))^2/norm(sig_vec(2*eta+1:N*sim))^2;
0643 fprintf('Signal MSE: l1homotopy-%3.4g, sparsa-%3.4g, yall1-%3.4g.\n',([err_l1homotopy,err_sparsa,err_yall1]));
0644 fprintf('Signal SER (in dB): l1homotopy-%3.4g, sparsa-%3.4g, yall1-%3.4g.\n',-10*log10([err_l1homotopy,err_sparsa,err_yall1]));
0645
0646
0647
0648 x_len = min(length(x_vec),length(xh_vec))-(P-1)*N;
0649 sig_len = min(length(sig_vec),length(sigh_vec))-(P-1)*N-2*eta;
0650 x_vec1 = x_vec(1:x_len);
0651 xh_vec1 = xh_vec(1:x_len,1);
0652 sig_vec1 = sig_vec(1:sig_len);
0653 sigh_vec1 = sigh_vec(1:sig_len,1);
0654
0655 fig(123);
0656 subplot(221);
0657 plot((1:sig_len)/N,sig_vec1, 'LineWidth',1);
0658 axis tight;
0659 title('original signal')
0660 subplot(2,2,2)
0661 plot((1:sig_len)/N,sigh_vec1-sig_vec1, 'LineWidth',1);
0662 axis tight
0663 title('reconstruction error')
0664 subplot(2,2,3);
0665 imagesc(reshape(x_vec1,N,x_len/N)); axis xy;
0666 axis tight;
0667 title('LOT coefficients');
0668 colorbar
0669 subplot(2,2,4);
0670 imagesc(reshape(x_vec1-xh_vec1,N,x_len/N),[0 max(abs(x_vec1))/20]); axis xy
0671 axis tight
0672 title('reconstruction error (LOT)');
0673 colorbar
0674
0675
0676 fig(3);
0677 subplot(211); spectrogram(sig_vec(1:s_ind(end)),N,N-1,N,length(sig),'yaxis');
0678 subplot(212); spectrogram(sigh_vec(1:s_ind(end),1),N,N-1,N,length(sig),'yaxis'); shg