Mercurial > hg > smallbox
comparison solvers/SMALL_chol.m @ 1:7750624e0c73 version0.5
(none)
author | idamnjanovic |
---|---|
date | Thu, 05 Nov 2009 16:36:01 +0000 |
parents | |
children | 524cc3fff5ac |
comparison
equal
deleted
inserted
replaced
0:5181bee80bc1 | 1:7750624e0c73 |
---|---|
1 function [A]=SMALL_chol(Dict,X, m, maxNumCoef, errorGoal, varargin) | |
2 %% | |
3 %============================================= | |
4 % Sparse coding of a group of signals based on a given | |
5 % dictionary and specified number of atoms to use. | |
6 % input arguments: Dict - the dictionary | |
7 % X - the signals to represent | |
8 % m - number of atoms in Dictionary | |
9 % errorGoal - the maximal allowed representation error for | |
10 % each signal. | |
11 % | |
12 % optional: if Dict is function handle then Transpose Dictionary | |
13 % handle needs to be specified. | |
14 % | |
15 % output arguments: A - sparse coefficient matrix. | |
16 % | |
17 % based on KSVD toolbox solver found on Miki Elad webpage (finding inverse | |
18 % with pinv() is changed with OMP Cholesky update) | |
19 % Ivan Damnjanovic 2009 | |
20 %============================================= | |
21 %% | |
22 % This Dictionary check is based on Thomas Blumensath work in sparsify 0_4 greedy solvers | |
23 explicitD=0; | |
24 if isa(Dict,'float') | |
25 D =@(z) Dict*z; | |
26 Dt =@(z) Dict'*z; | |
27 explicitD=1; | |
28 elseif isobject(Dict) | |
29 D =@(z) Dict*z; | |
30 Dt =@(z) Dict'*z; | |
31 elseif isa(Dict,'function_handle') | |
32 try | |
33 DictT=varargin{1}; | |
34 if isa(DictT,'function_handle'); | |
35 D=Dict; | |
36 Dt=DictT; | |
37 else | |
38 error('If Dictionary is a function handle,Transpose Dictionary also needs to be a function handle. '); | |
39 end | |
40 catch | |
41 error('If Dictionary is a function handle, Transpose Dictionary needs to be specified. Exiting.'); | |
42 end | |
43 else | |
44 error('Dictionary is of unsupported type. Use explicit matrix, function_handle or object. Exiting.'); | |
45 end | |
46 %% | |
47 [n,P]=size(X); | |
48 | |
49 | |
50 | |
51 global opts opts_tr machPrec | |
52 opts.UT = true; | |
53 opts_tr.UT = true; opts_tr.TRANSA = true; | |
54 machPrec = 1e-5; | |
55 | |
56 A = sparse(m,size(X,2)); | |
57 for k=1:1:P, | |
58 | |
59 R_I = []; | |
60 x=X(:,k); | |
61 residual=x; | |
62 indx = []; | |
63 a = zeros(m,1); | |
64 currResNorm = norm(residual); | |
65 errorGoal=errorGoal*currResNorm; | |
66 j = 0; | |
67 while currResNorm>errorGoal & j < maxNumCoef, | |
68 j = j+1; | |
69 dir=Dt(residual); | |
70 | |
71 [tmp__, pos]=max(abs(dir)); | |
72 | |
73 [R_I, flag] = updateChol(R_I, n, m, D, explicitD, indx, pos, Dt); | |
74 | |
75 | |
76 indx(j)=pos; | |
77 dx=zeros(m,1); | |
78 | |
79 z = linsolve(R_I,dir(indx),opts_tr); | |
80 | |
81 dx(indx) = linsolve(R_I,z,opts); | |
82 a(indx) = a(indx) + dx(indx); | |
83 | |
84 residual=x-D(a); | |
85 currResNorm = norm(residual); | |
86 | |
87 | |
88 end; | |
89 if (~isempty(indx)) | |
90 A(indx,k)=a(indx); | |
91 end | |
92 end; | |
93 return; | |
94 | |
95 | |
96 function [R, flag] = updateChol(R, n, N, A, explicitA, activeSet, newIndex, varargin) | |
97 | |
98 % updateChol: Updates the Cholesky factor R of the matrix | |
99 % A(:,activeSet)'*A(:,activeSet) by adding A(:,newIndex) | |
100 % If the candidate column is in the span of the existing | |
101 % active set, R is not updated, and flag is set to 1. | |
102 | |
103 global opts_tr machPrec | |
104 flag = 0; | |
105 | |
106 if (explicitA) | |
107 newVec = A(:,newIndex); | |
108 else | |
109 At=varargin{1}; | |
110 e = zeros(N,1); | |
111 e(newIndex) = 1; | |
112 newVec = A(e);%feval(A,1,n,N,e,1:N,N); | |
113 end | |
114 | |
115 if isempty(activeSet), | |
116 R = sqrt(sum(newVec.^2)); | |
117 else | |
118 if (explicitA) | |
119 p = linsolve(R,A(:,activeSet)'*A(:,newIndex),opts_tr); | |
120 else | |
121 AnewVec = At(newVec);%feval(A,2,n,length(activeSet),newVec,activeSet,N); | |
122 p = linsolve(R,AnewVec(activeSet),opts_tr); | |
123 end | |
124 q = sum(newVec.^2) - sum(p.^2); | |
125 if (q <= machPrec) % Collinear vector | |
126 flag = 1; | |
127 else | |
128 R = [R p; zeros(1, size(R,2)) sqrt(q)]; | |
129 end | |
130 end |