comparison solvers/SMALL_cgp.m @ 1:7750624e0c73 version0.5

(none)
author idamnjanovic
date Thu, 05 Nov 2009 16:36:01 +0000
parents
children 01cad25206d6
comparison
equal deleted inserted replaced
0:5181bee80bc1 1:7750624e0c73
1 function [A]=SMALL_cgp(Dict,X, m, maxNumCoef, errorGoal, varargin)
2 %=============================================
3 % Sparse coding of a group of signals based on a given
4 % dictionary and specified number of atoms to use.
5 % input arguments: Dict - the dictionary
6 % X - the signals to represent
7 % m - number of atoms in Dictionary
8 % errorGoal - the maximal allowed representation error for
9 % each signal.
10 %
11 % optional: if Dict is function handle then Transpose Dictionary
12 % handle needs to be specified.
13 %
14 % output arguments: A - sparse coefficient matrix.
15 %
16 % based on KSVD toolbox solver found on Miki Elad webpage (finding inverse
17 % with pinv() is changed with conjugate gradient method)
18 % Ivan Damnjanovic 2009
19 %=============================================
20 %%
21 %%
22
23 % This Dictionary check is based on Thomas Blumensath work in sparsify 0_4 greedy solvers
24 if isa(Dict,'float')
25 D =@(z) Dict*z;
26 Dt =@(z) Dict'*z;
27 elseif isobject(Dict)
28 D =@(z) Dict*z;
29 Dt =@(z) Dict'*z;
30 elseif isa(Dict,'function_handle')
31 try
32 DictT=varargin{1};
33 if isa(DictT,'function_handle');
34 D=Dict;
35 Dt=DictT;
36 else
37 error('If Dictionary is a function handle,Transpose Dictionary also needs to be a function handle. ');
38 end
39 catch
40 error('If Dictionary is a function handle, Transpose Dictionary needs to be specified. Exiting.');
41 end
42 else
43 error('Dictionary is of unsupported type. Use explicit matrix, function_handle or object. Exiting.');
44 end
45 %%
46
47 [n,P]=size(X);
48
49
50
51 A = sparse(m,size(X,2));
52
53 for k=1:1:P,
54
55 x = X(:,k);
56 residual = x;
57 indx =[];
58 j=0;
59
60
61 currResNorm = residual'*residual/n;
62 errorGoal=errorGoal*currResNorm;
63 a = zeros(m,1);
64 p = zeros(m,1);
65
66 while j < maxNumCoef,
67
68 j = j+1;
69 dir=Dt(residual);
70
71 [tmp__, pos]=max(abs(dir));
72 indx(j)=pos;
73
74 p(indx)=dir(indx);
75 Dp=D(p);
76 pDDp=Dp'*Dp;
77
78 if (j==1)
79 beta=0;
80 else
81 beta=-Dp'*residual/pDDp;
82 end
83
84 alfa=residual'*Dp/pDDp;
85 a=a+alfa*p;
86 p(indx)=dir(indx)+beta*p(indx);
87
88 residual=residual-alfa*Dp;
89
90 currResNorm = residual'*residual/n;
91 if currResNorm<errorGoal
92 fprintf('\nFound exact representation! \n');
93 break;
94 end
95 end;
96 if (~isempty(indx))
97 A(indx,k)=a(indx);
98 end
99 end;
100 return;
101
102
103