comparison DL/RLS-DLA/SMALL_rlsdla.m @ 40:6416fc12f2b8

(none)
author idamnjanovic
date Mon, 14 Mar 2011 15:35:24 +0000
parents
children 55faa9b5d1ac
comparison
equal deleted inserted replaced
39:8f734534839a 40:6416fc12f2b8
1 function Dictionary = SMALL_rlsdla(X, params)
2
3
4
5
6
7
8 CODE_SPARSITY = 1;
9 CODE_ERROR = 2;
10
11
12 % Determine which method will be used for sparse representation step -
13 % Sparsity or Error mode
14
15 if (isfield(params,'codemode'))
16 switch lower(params.codemode)
17 case 'sparsity'
18 codemode = CODE_SPARSITY;
19 thresh = params.Tdata;
20 case 'error'
21 codemode = CODE_ERROR;
22 thresh = params.Edata;
23
24 otherwise
25 error('Invalid coding mode specified');
26 end
27 elseif (isfield(params,'Tdata'))
28 codemode = CODE_SPARSITY;
29 thresh = params.Tdata;
30 elseif (isfield(params,'Edata'))
31 codemode = CODE_ERROR;
32 thresh = params.Edata;
33
34 else
35 error('Data sparse-coding target not specified');
36 end
37
38
39 % max number of atoms %
40
41 if (codemode==CODE_ERROR && isfield(params,'maxatoms'))
42 maxatoms = params.maxatoms;
43 else
44 maxatoms = -1;
45 end
46
47
48 % Forgetting factor
49
50 if (isfield(params,'forgettingMode'))
51 switch lower(params.forgettingMode)
52 case 'fix'
53 if (isfield(params,'forgettingFactor'))
54 lambda=params.forgettingFactor;
55 else
56 lambda=1;
57 end
58 otherwise
59 error('This mode is still not implemented');
60 end
61 elseif (isfield(params,'forgettingFactor'))
62 lambda=params.forgettingFactor;
63 else
64 lambda=1;
65 end
66
67 % determine dictionary size %
68
69 if (isfield(params,'initdict'))
70 if (any(size(params.initdict)==1) && all(iswhole(params.initdict(:))))
71 dictsize = length(params.initdict);
72 else
73 dictsize = size(params.initdict,2);
74 end
75 end
76 if (isfield(params,'dictsize')) % this superceedes the size determined by initdict
77 dictsize = params.dictsize;
78 end
79
80 if (size(X,2) < dictsize)
81 error('Number of training signals is smaller than number of atoms to train');
82 end
83
84
85 % initialize the dictionary %
86
87 if (isfield(params,'initdict'))
88 if (any(size(params.initdict)==1) && all(iswhole(params.initdict(:))))
89 D = X(:,params.initdict(1:dictsize));
90 else
91 if (size(params.initdict,1)~=size(X,1) || size(params.initdict,2)<dictsize)
92 error('Invalid initial dictionary');
93 end
94 D = params.initdict(:,1:dictsize);
95 end
96 else
97 data_ids = find(colnorms_squared(X) > 1e-6); % ensure no zero data elements are chosen
98 perm = randperm(length(data_ids));
99 D = X(:,data_ids(perm(1:dictsize)));
100 end
101
102
103 % normalize the dictionary %
104
105 D = normcols(D);
106
107 % Training data
108
109 data=X;
110
111 %
112
113 C=(100000*thresh)*eye(dictsize);
114 w=zeros(dictsize,1);
115 u=zeros(dictsize,1);
116
117
118 for i = 1:size(data,2)
119
120 if (codemode == CODE_SPARSITY)
121 w = ompmex(D,data(:,i),[],[],thresh,1,-1,0);
122 else
123 w = omp2mex(D,data(:,i),[],[],[],thresh,0,-1,maxatoms,0);
124 end
125
126 spind=find(w);
127
128 residual = data(:,i) - D * w;
129
130 if (lambda~=1)
131 C = C *(1/ lambda);
132 end
133
134 u = C(:,spind) * w(spind);
135
136
137 alfa = 1/(1 + w' * u);
138
139 D = D + (alfa * residual) * u';
140
141
142 C = C - (alfa * u)* u';
143
144 end
145
146 Dictionary = D;
147
148 end