-
Notifications
You must be signed in to change notification settings - Fork 1
/
SISAL.m
716 lines (632 loc) · 22 KB
/
SISAL.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
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
function [M,Up,my,sing_values] = sisal(Y,p,varargin)
%% [M,Up,my,sing_values] = sisal(Y,p,varargin)
%
% Simplex identification via split augmented Lagrangian (SISAL)
%
%% --------------- Description ---------------------------------------------
%
% SISAL Estimates the vertices M={m_1,...m_p} of the (p-1)-dimensional
% simplex of minimum volume containing the vectors [y_1,...y_N], under the
% assumption that y_i belongs to a (p-1) dimensional affine set. Thus,
% any vector y_i belongs to the convex hull of the columns of M; i.e.,
%
% y_i = M*x_i
%
% where x_i belongs to the probability (p-1)-simplex.
%
% As described in the papers [1], [2], matrix M is obtained by implementing
% the following steps:
%
% 1-Project y onto a p-dimensional subspace containing the data set y
%
% yp = Up'*y; Up is an isometric matrix (Up'*Up=Ip)
%
% 2- solve the optimization problem
%
% Q^* = arg min_Q -\log abs(det(Q)) + tau*|| Q*yp ||_h
%
% subject to: ones(1,p)*Q=mq,
%
% where mq = ones(1,N)*yp'inv(yp*yp) and ||x||_h is the "hinge"
% induced norm (see [1])
% 3- Compute
%
% M = Up*inv(Q^*);
%
%% -------------------- Line of Attack -----------------------------------
%
% SISAL replaces the usual fractional abundance positivity constraints,
% forcing the spectral vectors to belong to the convex hull of the
% endmember signatures, by soft constraints. This new criterion brings
% robustnes to noise and outliers
%
% The obtained optimization problem is solved by a sequence of
% augmented Lagrangian optimizations involving quadractic and one-sided soft
% thresholding steps. The resulting algorithm is very fast and able so
% solve problems far beyond the reach of the current state-of-the art
% algorithms. As examples, in a standard PC, SISAL, approximatelly, the
% times:
%
% p = 10, N = 1000 ==> time = 2 seconds
%
% p = 20, N = 50000 ==> time = 3 minutes
%
%% ===== Required inputs =============
%
% y - matrix with L(channels) x N(pixels).
% each pixel is a linear mixture of p endmembers
% signatures y = M*x + noise,
%
% SISAL assumes that y belongs to an affine space. It may happen,
% however, that the data supplied by the user is not in an affine
% set. For this reason, the first step this code implements
% is the estimation of the affine set the best represent
% (in the l2 sense) the data.
%
% p - number of independent columns of M. Therefore, M spans a
% (p-1)-dimensional affine set.
%
%
%% ====================== Optional inputs =============================
%
% 'MM_ITERS' = double; Default 80;
%
% Maximum number of constrained quadratic programs
%
%
% 'TAU' = double; Default; 1
%
% Regularization parameter in the problem
%
% Q^* = arg min_Q -\log abs(det(Q)) + tau*|| Q*yp ||_h
%
% subject to:ones(1,p)*Q=mq,
%
% where mq = ones(1,N)*yp'inv(yp*yp) and ||x||_h is the "hinge"
% induced norm (see [1]).
%
% 'MU' = double; Default; 1
%
% Augmented Lagrange regularization parameter
%
% 'spherize' = {'yes', 'no'}; Default 'yes'
%
% Applies a spherization step to data such that the spherized
% data spans over the same range along any axis.
%
% 'TOLF' = double; Default; 1e-2
%
% Tolerance for the termination test (relative variation of f(Q))
%
%
% 'M0' = <[Lxp] double>; Given by the VCA algorithm
%
% Initial M.
%
%
% 'verbose' = {0,1,2,3}; Default 1
%
% 0 - work silently
% 1 - display simplex volume
% 2 - display figures
% 3 - display SISAL information
% 4 - display SISAL information and figures
%
%
%
%
%% =========================== Outputs ==================================
%
% M = [Lxp] estimated mixing matrix
%
% Up = [Lxp] isometric matrix spanning the same subspace as M
%
% my = mean value of y
%
% sing_values = (p-1) eigenvalues of Cy = (y-my)*(y-my)/N. The dynamic range
% of these eigenvalues gives an idea of the difficulty of the
% underlying problem
%
%
% NOTE: the identified affine set is given by
%
% {z\in R^p : z=Up(:,1:p-1)*a+my, a\in R^(p-1)}
%
%% -------------------------------------------------------------------------
%
% Copyright (May, 2009): José Bioucas-Dias ([email protected])
%
% SISAL is distributed under the terms of
% the GNU General Public License 2.0.
%
% Permission to use, copy, modify, and distribute this software for
% any purpose without fee is hereby granted, provided that this entire
% notice is included in all copies of any software which is or includes
% a copy or modification of this software and in all copies of the
% supporting documentation for such software.
% This software is being provided "as is", without any express or
% implied warranty. In particular, the authors do not make any
% representation or warranty of any kind concerning the merchantability
% of this software or its fitness for any particular purpose."
% ----------------------------------------------------------------------
%
% More details in:
%
% [1] José M. Bioucas-Dias
% "A variable splitting augmented lagrangian approach to linear spectral unmixing"
% First IEEE GRSS Workshop on Hyperspectral Image and Signal
% Processing - WHISPERS, 2009 (submitted). http://arxiv.org/abs/0904.4635v1
%
%
%
% -------------------------------------------------------------------------
%
%%
%--------------------------------------------------------------
% test for number of required parametres
%--------------------------------------------------------------
if (nargin-length(varargin)) ~= 2
error('Wrong number of required parameters');
end
% data set size
[L,N] = size(Y);
if (L<p)
error('Insufficient number of columns in y');
end
%%
%--------------------------------------------------------------
% Set the defaults for the optional parameters
%--------------------------------------------------------------
% maximum number of quadratic QPs
MMiters = 80;
spherize = 'yes';
% display only volume evolution
verbose = 0;
% soft constraint regularization parameter
tau = 1;
% Augmented Lagrangian regularization parameter
mu = p*1000/N;
% no initial simplex
M = 0;
% tolerance for the termination test
tol_f = 1e-2;
%%
%--------------------------------------------------------------
% Local variables
%--------------------------------------------------------------
% maximum violation of inequalities
slack = 1e-3;
% flag energy decreasing
energy_decreasing = 0;
% used in the termination test
f_val_back = inf;
%
% spherization regularization parameter
lam_sphe = 1e-8;
% quadractic regularization parameter for the Hesssian
% Hreg = = mu*I
lam_quad = 1e-6;
% minimum number of AL iterations per quadratic problem
AL_iters = 4;
% flag
flaged = 0;
%--------------------------------------------------------------
% Read the optional parameters
%--------------------------------------------------------------
if (rem(length(varargin),2)==1)
error('Optional parameters should always go by pairs');
else
for i=1:2:(length(varargin)-1)
switch upper(varargin{i})
case 'MM_ITERS'
MMiters = varargin{i+1};
case 'SPHERIZE'
spherize = varargin{i+1};
case 'MU'
mu = varargin{i+1};
case 'TAU'
tau = varargin{i+1};
case 'TOLF'
tol_f = varargin{i+1};
case 'M0'
M = varargin{i+1};
case 'VERBOSE'
verbose = varargin{i+1};
otherwise
% Hmmm, something wrong with the parameter string
error(['Unrecognized option: ''' varargin{i} '''']);
end;
end;
end
%%
%--------------------------------------------------------------
% set display mode
%--------------------------------------------------------------
if (verbose == 3) | (verbose == 4)
warning('off','all');
else
warning('on','all');
end
%%
%--------------------------------------------------------------
% identify the affine space that best represent the data set y
%--------------------------------------------------------------
my = mean(Y,2);
Y = Y-repmat(my,1,N);
[Up,D] = svds(Y*Y'/N,p-1);
% represent y in the subspace R^(p-1)
Y = Up*Up'*Y;
% lift y
Y = Y + repmat(my,1,N); %
% compute the orthogonal component of my
my_ortho = my-Up*Up'*my;
% define another orthonormal direction
Up = [Up my_ortho/sqrt(sum(my_ortho.^2))];
sing_values = diag(D);
% get coordinates in R^p
Y = Up'*Y;
%%
%------------------------------------------
% spherize if requested
%------------------------------------------
if strcmp(spherize,'yes')
Y = Up*Y;
Y = Y-repmat(my,1,N);
C = diag(1./sqrt((diag(D+lam_sphe*eye(p-1)))));
IC = inv(C);
Y=C*Up(:,1:p-1)'*Y;
% lift
Y(p,:) = 1;
% normalize to unit norm
Y = Y/sqrt(p);
end
%%
% ---------------------------------------------
% Initialization
%---------------------------------------------
if M == 0
% Initialize with VCA
Mvca = VCAsisal(Y,'Endmembers',p,'verbose','off');
M = Mvca;
% expand Q
Ym = mean(M,2);
Ym = repmat(Ym,1,p);
dQ = M - Ym;
% fraction: multiply by p is to make sure Q0 starts with a feasible
% initial value.
M = M + p*dQ;
else
% Ensure that M is in the affine set defined by the data
M = M-repmat(my,1,p);
M = Up(:,1:p-1)*Up(:,1:p-1)'*M;
M = M + repmat(my,1,p);
M = Up'*M; % represent in the data subspace
% is sherization is set
if strcmp(spherize,'yes')
M = Up*M-repmat(my,1,p);
M=C*Up(:,1:p-1)'*M;
% lift
M(p,:) = 1;
% normalize to unit norm
M = M/sqrt(p);
end
end
Q0 = inv(M);
Q=Q0;
% plot initial matrix M
if verbose == 2 | verbose == 4
set(0,'Units','pixels')
%get figure 1 handler
H_1=figure;
pos1 = get(H_1,'Position');
pos1(1)=50;
pos1(2)=100+400;
set(H_1,'Position', pos1)
hold on
M = inv(Q);
p_H(1) = plot(Y(1,:),Y(2,:),'.');
p_H(2) = plot(M(1,:), M(2,:),'ok');
leg_cell = cell(1);
leg_cell{1} = 'data points';
leg_cell{end+1} = 'M(0)';
title('SISAL: Endmember Evolution')
end
%%
% ---------------------------------------------
% Build constant matrices
%---------------------------------------------
AAT = kron(Y*Y',eye(p)); % size p^2xp^2
B = kron(eye(p),ones(1,p)); % size pxp^2
qm = sum(inv(Y*Y')*Y,2);
H = lam_quad*eye(p^2);
F = H+mu*AAT; % equation (11) of [1]
IF = inv(F);
% auxiliar constant matrices
G = IF*B'*inv(B*IF*B');
qm_aux = G*qm;
G = IF-G*B*IF;
%%
% ---------------------------------------------------------------
% Main body- sequence of quadratic-hinge subproblems
%----------------------------------------------------------------
% initializations
Z = Q*Y;
Bk = 0*Z;
for k = 1:MMiters
IQ = inv(Q);
g = -IQ';
g = g(:);
baux = H*Q(:)-g;
q0 = Q(:);
Q0 = Q;
% display the simplex volume
if verbose == 1
if strcmp(spherize,'yes')
% unscale
M = IQ*sqrt(p);
%remove offset
M = M(1:p-1,:);
% unspherize
M = Up(:,1:p-1)*IC*M;
% sum ym
M = M + repmat(my,1,p);
M = Up'*M;
else
M = IQ;
end
fprintf('\n iter = %d, simplex volume = %4f \n', k, 1/abs(det(M)))
end
%Bk = 0*Z;
if k==MMiters
AL_iters = 100;
%Z=Q*Y;
%Bk = 0*Z;
end
% initial function values (true and quadratic)
% f0_val = -log(abs(det(Q0)))+ tau*sum(sum(hinge(Q0*Y)));
% f0_quad = f0_val; % (q-q0)'*g+1/2*(q-q0)'*H*(q-q0);
while 1 > 0
q = Q(:);
% initial function values (true and quadratic)
f0_val = -log(abs(det(Q)))+ tau*sum(sum(hinge(Q*Y)));
f0_quad = (q-q0)'*g+1/2*(q-q0)'*H*(q-q0) + tau*sum(sum(hinge(Q*Y)));
for i=2:AL_iters
%-------------------------------------------
% solve quadratic problem with constraints
%-------------------------------------------
dq_aux= Z+Bk; % matrix form
dtz_b = dq_aux*Y';
dtz_b = dtz_b(:);
b = baux+mu*dtz_b; % (11) of [1]
q = G*b+qm_aux; % (10) of [1]
Q = reshape(q,p,p);
%-------------------------------------------
% solve hinge
%-------------------------------------------
Z = soft_neg(Q*Y -Bk,tau/mu);
%norm(B*q-qm)
%-------------------------------------------
% update Bk
%-------------------------------------------
Bk = Bk - (Q*Y-Z);
if verbose == 3 || verbose == 4
fprintf('\n ||Q*Y-Z|| = %4f \n',norm(Q*Y-Z,'fro'))
end
if verbose == 2 || verbose == 4
M = inv(Q);
plot(M(1,:), M(2,:),'.r');
if ~flaged
p_H(3) = plot(M(1,:), M(2,:),'.r');
leg_cell{end+1} = 'M(k)';
flaged = 1;
end
end
end
f_quad = (q-q0)'*g+1/2*(q-q0)'*H*(q-q0) + tau*sum(sum(hinge(Q*Y)));
if verbose == 3 || verbose == 4
fprintf('\n MMiter = %d, AL_iter, = % d, f0 = %2.4f, f_quad = %2.4f, \n',...
k,i, f0_quad,f_quad)
end
f_val = -log(abs(det(Q)))+ tau*sum(sum(hinge(Q*Y)));
if f0_quad >= f_quad %quadratic energy decreased
while f0_val < f_val;
if verbose == 3 || verbose == 4
fprintf('\n line search, MMiter = %d, AL_iter, = % d, f0 = %2.4f, f_val = %2.4f, \n',...
k,i, f0_val,f_val)
end
% do line search
Q = (Q+Q0)/2;
f_val = -log(abs(det(Q)))+ tau*sum(sum(hinge(Q*Y)));
end
break
end
end
end
if verbose == 2 || verbose == 4
p_H(4) = plot(M(1,:), M(2,:),'*g');
leg_cell{end+1} = 'M(final)';
legend(p_H', leg_cell);
end
if strcmp(spherize,'yes')
M = inv(Q);
% refer to the initial affine set
% unscale
M = M*sqrt(p);
%remove offset
M = M(1:p-1,:);
% unspherize
M = Up(:,1:p-1)*IC*M;
% sum ym
M = M + repmat(my,1,p);
else
M = Up*inv(Q);
end
% % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % %
function [Ae, indice, Rp] = VCAsisal(R,varargin)
% Vertex Component Analysis
%
% [Ae, indice, Rp ]= vca(R,'Endmembers',p,'SNR',r,'verbose',v)
%
% ------- Input variables -------------
% R - matrix with dimensions L(channels) x N(pixels)
% each pixel is a linear mixture of p endmembers
% signatures R = M x s, where s = gamma x alfa
% gamma is a illumination perturbation factor and
% alfa are the abundance fractions of each endmember.
% for a given R, we need to decide the M and s
% 'Endmembers'
% p - positive integer number of endmembers in the scene
%
% ------- Output variables -----------
% A - estimated mixing matrix (endmembers signatures)
% indice - pixels that were chosen to be the most pure
% Rp - Data matrix R projected.
%
% ------- Optional parameters---------
% 'SNR'
% r - (double) signal to noise ratio (dB)
% 'verbose'
% v - [{'on'} | 'off']
% ------------------------------------
%
% Authors: Jos?Nascimento ([email protected])
% Jos?Bioucas Dias ([email protected])
% Copyright (c)
% version: 2.1 (7-May-2004)
%
% For any comment contact the authors
%
% more details on:
% Jos?M. P. Nascimento and Jos?M. B. Dias
% "Vertex Component Analysis: A Fast Algorithm to Unmix Hyperspectral Data"
% submited to IEEE Trans. Geosci. Remote Sensing, vol. .., no. .., pp. .-., 2004
%
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Default parameters
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
verbose = 'on'; % default
snr_input = 0; % default this flag is zero,
% which means we estimate the SNR
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Looking for input parameters
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
dim_in_par = length(varargin);
if (nargin - dim_in_par)~=1
error('Wrong parameters');
elseif rem(dim_in_par,2) == 1
error('Optional parameters should always go by pairs');
else
for i = 1 : 2 : (dim_in_par-1)
switch lower(varargin{i})
case 'verbose'
verbose = varargin{i+1};
case 'endmembers'
p = varargin{i+1};
case 'snr'
SNR = varargin{i+1};
snr_input = 1; % flag meaning that user gives SNR
otherwise
fprintf(1,'Unrecognized parameter:%s\n', varargin{i});
end %switch
end %for
end %if
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Initializations
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
if isempty(R)
error('there is no data');
else
[L N]=size(R); % L number of bands (channels)
% N number of pixels (LxC)
end
if (p<0 | p>L | rem(p,1)~=0),
error('ENDMEMBER parameter must be integer between 1 and L');
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% SNR Estimates
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
if snr_input==0,
r_m = mean(R,2);
R_m = repmat(r_m,[1 N]); % mean of each band
R_o = R - R_m; % data with zero-mean
[Ud,Sd,Vd] = svds(R_o*R_o'/N,p); % computes the p-projection matrix
x_p = Ud' * R_o; % project the zero-mean data onto p-subspace
SNR = estimate_snr(R,r_m,x_p);
if strcmp (verbose, 'on'), fprintf(1,'SNR estimated = %g[dB]\n',SNR); end
else
if strcmp (verbose, 'on'), fprintf(1,'input SNR = %g[dB]\t',SNR); end
end
SNR_th = 15 + 10*log10(p);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Choosing Projective Projection or
% projection to p-1 subspace
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
if SNR < SNR_th,
if strcmp (verbose, 'on'), fprintf(1,'... Select the projective proj.\n',SNR); end
d = p-1;
if snr_input==0, % it means that the projection is already computed
Ud= Ud(:,1:d);
else
r_m = mean(R,2);
R_m = repmat(r_m,[1 N]); % mean of each band
R_o = R - R_m; % data with zero-mean
[Ud,Sd,Vd] = svds(R_o*R_o'/N,d); % computes the p-projection matrix
x_p = Ud' * R_o; % project thezeros mean data onto p-subspace
end
Rp = Ud * x_p(1:d,:) + repmat(r_m,[1 N]); % again in dimension L
x = x_p(1:d,:); % x_p = Ud' * R_o; is on a p-dim subspace
c = max(sum(x.^2,1))^0.5;
y = [x ; c*ones(1,N)] ;
else
if strcmp (verbose, 'on'), fprintf(1,'... Select proj. to p-1\n',SNR); end
d = p;
[Ud,Sd,Vd] = svds(R*R'/N,d); % computes the p-projection matrix
x_p = Ud'*R;
Rp = Ud * x_p(1:d,:); % again in dimension L (note that x_p has no null mean)
x = Ud' * R;
u = mean(x,2); %equivalent to u = Ud' * r_m
y = x./ repmat( sum( x .* repmat(u,[1 N]) ) ,[d 1]);
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% VCA algorithm
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
indice = zeros(1,p);
A = zeros(p,p);
A(p,1) = 1;
for i=1:p
w = rand(p,1);
f = w - A*pinv(A)*w;
f = f / sqrt(sum(f.^2));
v = f'*y;
[v_max indice(i)] = max(abs(v));
A(:,i) = y(:,indice(i)); % same as x(:,indice(i))
end
Ae = Rp(:,indice);
return;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% End of the vca function
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Internal functions
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function snr_est = estimate_snr(R,r_m,x)
[L N]=size(R); % L number of bands (channels)
% N number of pixels (Lines x Columns)
[p N]=size(x); % p number of endmembers (reduced dimension)
P_y = sum(R(:).^2)/N;
P_x = sum(x(:).^2)/N + r_m'*r_m;
snr_est = 10*log10( (P_x - p/L*P_y)/(P_y- P_x) );
return;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function z = hinge(y)
% z = hinge(y)
%
% hinge function
z = max(-y,0);
function z = soft_neg(y,tau)
% z = soft_neg(y,tau);
%
% negative soft (proximal operator of the hinge function)
z = max(abs(y+tau/2) - tau/2, 0);
z = z./(z+tau/2) .* (y+tau/2);