-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSparseRepresentationSolve_OMP.m
198 lines (170 loc) · 6.55 KB
/
SparseRepresentationSolve_OMP.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
function [a, k]=SparseRepresentationSolve_OMP(A, x, varargin)
%稀疏向量求解,OMP方法
%输出:稀疏向量a(N*1),即“丰度”;实际迭代次数k
%输入:字典A(n*N),原始像素向量x(n*1),解向量a的长度N,最大迭代次数maxIters,
%%%%%%%迭代停止条件最后系数残余相关小于特定阈值lambdaStop,是否显示每次迭代详细过程verbose,默认不显示
%%%%%%%容差Error tolerance,默认值1e-5
% SolveOMP: Orthogonal Matching Pursuit
% Usage
% [sols, iters, activationHist] = SolveOMP(A, x, N, maxIters, lambdaStop, solFreq, verbose, OptTol)
% Input
% A Either an explicit nxN matrix, with rank(A) = min(N,n)
% by assumption, or a string containing the name of a
% function implementing an implicit matrix (see below for
% details on the format of the function).
% x vector of length n.
% N length of solution vector.
% maxIters maximum number of iterations to perform. If not
% specified, runs to stopping condition (default)
% lambdaStop If specified, the algorithm stops when the last coefficient
% entered has residual correlation <= lambdaStop.
% verbose 1 to print out detailed progress at each iteration, 0 for
% no output (default)
% OptTol Error tolerance, default 1e-5
% Outputs
% sols solution(s) of OMP
% iters number of iterations performed
% Description
% SolveOMP is a greedy algorithm to estimate the solution
% of the sparse approximation problem
% min ||a||_0 s.t. A*a = b
% The implementation implicitly factors the active set matrix A(:,I)
% using Cholesky updates.
% The matrix A can be either an explicit matrix, or an implicit operator
% implemented as an m-file. If using the implicit form, the user should
% provide the name of a function of the following format:
% x = OperatorName(mode, m, n, a, I, dim)
% This function gets as input a vector a and an index set I, and returns
% x = A(:,I)*a if mode = 1, or x = A(:,I)'*a if mode = 2.
% A is the m by dim implicit matrix implemented by the function. I is a
% subset of the columns of A, i.e. a subset of 1:dim of length n. a is a
% vector of length n is mode = 1, or a vector of length m is mode = 2.
% See Also
% SolveLasso, SolveBP, SolveStOMP
%
global isNonnegative
isNonnegative = true;
DEBUG = 0;
STOPPING_GROUND_TRUTH = -1;
STOPPING_DUALITY_GAP = 1;
STOPPING_SPARSE_SUPPORT = 2;
STOPPING_OBJECTIVE_VALUE = 3;
STOPPING_SUBGRADIENT = 4;
STOPPING_DEFAULT = STOPPING_OBJECTIVE_VALUE;
stoppingCriterion = STOPPING_DEFAULT;
OptTol =0;
lambdaStop =1e-25;
maxIters = 1000;
[n,N]= size(A);
% Parameters for linsolve function
% Global variables for linsolve function
global opts opts_tr machPrec
opts.UT = true;
opts_tr.UT = true; opts_tr.TRANSA = true;
machPrec = 1e-25;
% Parse the optional inputs.
if (mod(length(varargin), 2) ~= 0 ),
error(['Extra Parameters passed to the function ''' mfilename ''' must be passed in pairs.']);
end
parameterCount = length(varargin)/2;
for parameterIndex = 1:parameterCount,
parameterName = varargin{parameterIndex*2 - 1};
parameterValue = varargin{parameterIndex*2};
switch lower(parameterName)
case 'lambda'
lambda = parameterValue;
case 'maxiteration'
if parameterValue>maxIters
if DEBUG>0
warning('Parameter maxIteration is larger than the possible value: Ignored.');
end
else
maxIters = parameterValue;
end
case 'tolerance'
OptTol = parameterValue;
case 'stoppingcriterion'
stoppingCriterion = parameterValue;
case 'groundtruth'
xG = parameterValue;
case 'isnonnegative'
isNonnegative = parameterValue;
otherwise
error(['The parameter ''' parameterName ''' is not recognized by the function ''' mfilename '''.']);
end
end
% Initialize
a = zeros(N,1);
k = 1;
R_I = [];
activeSet = [];
res = x;
normy = norm(x);
resnorm = normy;
done = 0;
while ~done && k<maxIters
corr = A'*res;
if isNonnegative
[maxcorr i] = max(corr);
else
[maxcorr i] = max(abs(corr));
end
if maxcorr<=0
done = 1;
else
newIndex = i(1);
% Update Cholesky factorization of A_I
[R_I, done] = updateChol(R_I, n, N, A, activeSet, newIndex);
end
if ~done
activeSet = [activeSet newIndex];
% Solve for the least squares update: (A_I'*A_I)dx_I = corr_I
dx = zeros(N,1);
z = linsolve(R_I,corr(activeSet),opts_tr);
dx(activeSet) = linsolve(R_I,z,opts);
a(activeSet) = a(activeSet) + dx(activeSet);
% Compute new residual
res = x - A(:,activeSet) * a(activeSet);
switch stoppingCriterion
case STOPPING_SUBGRADIENT
error('Subgradient is not a valid stopping criterion for OMP.');
case STOPPING_DUALITY_GAP
error('Duality gap is not a valid stopping criterion for OMP.');
case STOPPING_SPARSE_SUPPORT
error('Sparse support is not a valid stopping criterion for OMP.');
case STOPPING_OBJECTIVE_VALUE
resnorm = norm(res);
if ((resnorm <= OptTol*normy) || ((lambdaStop > 0) && (maxcorr <= lambdaStop)))
done = 1;
end
case STOPPING_GROUND_TRUTH
done = norm(xG-a)<OptTol;
otherwise
error('Undefined stopping criterion');
end
if DEBUG>0
fprintf('Iteration %d: Adding variable %d\n', k, newIndex);
end
k = k+1;
end
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function [R, flag] = updateChol(R, n, N, A, activeSet, newIndex)
% updateChol: Updates the Cholesky factor R of the matrix
% A(:,activeSet)'*A(:,activeSet) by adding A(:,newIndex)
% If the candidate column is in the span of the existing
% active set, R is not updated, and flag is set to 1.
global opts_tr machPrec
flag = 0;
newVec = A(:,newIndex);
if isempty(activeSet),
R = sqrt(sum(newVec.^2));
else
p = linsolve(R,A(:,activeSet)'*A(:,newIndex),opts_tr);
q = sum(newVec.^2) - sum(p.^2);
if (q <= machPrec) % Collinear vector
flag = 1;
else
R = [R p; zeros(1, size(R,2)) sqrt(q)];
end
end