annotate DL/RLS-DLA/SMALL_rlsdlaFirstClustTry.m @ 59:23f9dd7b9d78

(none)
author idamnjanovic
date Mon, 14 Mar 2011 17:25:38 +0000
parents 6416fc12f2b8
children
rev   line source
idamnjanovic@40 1 function Dictionary = SMALL_rlsdla(X, params)
idamnjanovic@40 2
idamnjanovic@40 3
idamnjanovic@40 4
idamnjanovic@40 5
idamnjanovic@40 6
idamnjanovic@40 7 global CODE_SPARSITY CODE_ERROR codemode
idamnjanovic@40 8 global MEM_LOW MEM_NORMAL MEM_HIGH memusage
idamnjanovic@40 9 global ompfunc ompparams exactsvd
idamnjanovic@40 10
idamnjanovic@40 11 CODE_SPARSITY = 1;
idamnjanovic@40 12 CODE_ERROR = 2;
idamnjanovic@40 13
idamnjanovic@40 14 MEM_LOW = 1;
idamnjanovic@40 15 MEM_NORMAL = 2;
idamnjanovic@40 16 MEM_HIGH = 3;
idamnjanovic@40 17
idamnjanovic@40 18
idamnjanovic@40 19 % p = randperm(size(X,2));
idamnjanovic@40 20
idamnjanovic@40 21 % coding mode %
idamnjanovic@40 22 X_norm=sqrt(sum(X.^2, 1));
idamnjanovic@40 23 % X_norm_1=sum(abs(X));
idamnjanovic@40 24 % X_norm_inf=max(abs(X));
idamnjanovic@40 25 [X_norm_sort, p]=sort(X_norm);%, 'descend');
idamnjanovic@40 26 % [X_norm_sort1, p5]=sort(X_norm_1);%, 'descend');
idamnjanovic@40 27
idamnjanovic@40 28 % if (isfield(params,'codemode'))
idamnjanovic@40 29 % switch lower(params.codemode)
idamnjanovic@40 30 % case 'sparsity'
idamnjanovic@40 31 % codemode = CODE_SPARSITY;
idamnjanovic@40 32 % thresh = params.Tdata;
idamnjanovic@40 33 % case 'error'
idamnjanovic@40 34 % codemode = CODE_ERROR;
idamnjanovic@40 35 % thresh = params.Edata;
idamnjanovic@40 36 % otherwise
idamnjanovic@40 37 % error('Invalid coding mode specified');
idamnjanovic@40 38 % end
idamnjanovic@40 39 % elseif (isfield(params,'Tdata'))
idamnjanovic@40 40 % codemode = CODE_SPARSITY;
idamnjanovic@40 41 % thresh = params.Tdata;
idamnjanovic@40 42 % elseif (isfield(params,'Edata'))
idamnjanovic@40 43 % codemode = CODE_ERROR;
idamnjanovic@40 44 % thresh = params.Edata;
idamnjanovic@40 45 %
idamnjanovic@40 46 % else
idamnjanovic@40 47 % error('Data sparse-coding target not specified');
idamnjanovic@40 48 % end
idamnjanovic@40 49
idamnjanovic@40 50 thresh = params.Edata;
idamnjanovic@40 51 % max number of atoms %
idamnjanovic@40 52
idamnjanovic@40 53 % if (codemode==CODE_ERROR && isfield(params,'maxatoms'))
idamnjanovic@40 54 % ompparams{end+1} = 'maxatoms';
idamnjanovic@40 55 % ompparams{end+1} = params.maxatoms;
idamnjanovic@40 56 % end
idamnjanovic@40 57
idamnjanovic@40 58
idamnjanovic@40 59 % memory usage %
idamnjanovic@40 60
idamnjanovic@40 61 if (isfield(params,'memusage'))
idamnjanovic@40 62 switch lower(params.memusage)
idamnjanovic@40 63 case 'low'
idamnjanovic@40 64 memusage = MEM_LOW;
idamnjanovic@40 65 case 'normal'
idamnjanovic@40 66 memusage = MEM_NORMAL;
idamnjanovic@40 67 case 'high'
idamnjanovic@40 68 memusage = MEM_HIGH;
idamnjanovic@40 69 otherwise
idamnjanovic@40 70 error('Invalid memory usage mode');
idamnjanovic@40 71 end
idamnjanovic@40 72 else
idamnjanovic@40 73 memusage = MEM_NORMAL;
idamnjanovic@40 74 end
idamnjanovic@40 75
idamnjanovic@40 76
idamnjanovic@40 77 % iteration count %
idamnjanovic@40 78
idamnjanovic@40 79 if (isfield(params,'iternum'))
idamnjanovic@40 80 iternum = params.iternum;
idamnjanovic@40 81 else
idamnjanovic@40 82 iternum = 10;
idamnjanovic@40 83 end
idamnjanovic@40 84
idamnjanovic@40 85
idamnjanovic@40 86 % omp function %
idamnjanovic@40 87
idamnjanovic@40 88 if (codemode == CODE_SPARSITY)
idamnjanovic@40 89 ompfunc = @omp;
idamnjanovic@40 90 else
idamnjanovic@40 91 ompfunc = @omp2;
idamnjanovic@40 92 end
idamnjanovic@40 93
idamnjanovic@40 94
idamnjanovic@40 95 % % status messages %
idamnjanovic@40 96 %
idamnjanovic@40 97 % printiter = 0;
idamnjanovic@40 98 % printreplaced = 0;
idamnjanovic@40 99 % printerr = 0;
idamnjanovic@40 100 % printgerr = 0;
idamnjanovic@40 101 %
idamnjanovic@40 102 % verbose = 't';
idamnjanovic@40 103 % msgdelta = -1;
idamnjanovic@40 104 %
idamnjanovic@40 105
idamnjanovic@40 106 %
idamnjanovic@40 107 % for i = 1:length(verbose)
idamnjanovic@40 108 % switch lower(verbose(i))
idamnjanovic@40 109 % case 'i'
idamnjanovic@40 110 % printiter = 1;
idamnjanovic@40 111 % case 'r'
idamnjanovic@40 112 % printiter = 1;
idamnjanovic@40 113 % printreplaced = 1;
idamnjanovic@40 114 % case 't'
idamnjanovic@40 115 % printiter = 1;
idamnjanovic@40 116 % printerr = 1;
idamnjanovic@40 117 % if (isfield(params,'testdata'))
idamnjanovic@40 118 % printgerr = 1;
idamnjanovic@40 119 % end
idamnjanovic@40 120 % end
idamnjanovic@40 121 % end
idamnjanovic@40 122 %
idamnjanovic@40 123 % if (msgdelta<=0 || isempty(verbose))
idamnjanovic@40 124 % msgdelta = -1;
idamnjanovic@40 125 % end
idamnjanovic@40 126 %
idamnjanovic@40 127 % ompparams{end+1} = 'messages';
idamnjanovic@40 128 % ompparams{end+1} = msgdelta;
idamnjanovic@40 129 %
idamnjanovic@40 130 %
idamnjanovic@40 131 %
idamnjanovic@40 132 % % compute error flag %
idamnjanovic@40 133 %
idamnjanovic@40 134 % comperr = (nargout>=3 || printerr);
idamnjanovic@40 135 %
idamnjanovic@40 136 %
idamnjanovic@40 137 % % validation flag %
idamnjanovic@40 138 %
idamnjanovic@40 139 % testgen = 0;
idamnjanovic@40 140 % if (isfield(params,'testdata'))
idamnjanovic@40 141 % testdata = params.testdata;
idamnjanovic@40 142 % if (nargout>=4 || printgerr)
idamnjanovic@40 143 % testgen = 1;
idamnjanovic@40 144 % end
idamnjanovic@40 145 % end
idamnjanovic@40 146
idamnjanovic@40 147 %
idamnjanovic@40 148 % % data norms %
idamnjanovic@40 149 %
idamnjanovic@40 150 % XtX = []; XtXg = [];
idamnjanovic@40 151 % if (codemode==CODE_ERROR && memusage==MEM_HIGH)
idamnjanovic@40 152 % XtX = colnorms_squared(data);
idamnjanovic@40 153 % if (testgen)
idamnjanovic@40 154 % XtXg = colnorms_squared(testdata);
idamnjanovic@40 155 % end
idamnjanovic@40 156 % end
idamnjanovic@40 157
idamnjanovic@40 158
idamnjanovic@40 159 % mutual incoherence limit %
idamnjanovic@40 160
idamnjanovic@40 161 if (isfield(params,'muthresh'))
idamnjanovic@40 162 muthresh = params.muthresh;
idamnjanovic@40 163 else
idamnjanovic@40 164 muthresh = 0.99;
idamnjanovic@40 165 end
idamnjanovic@40 166 if (muthresh < 0)
idamnjanovic@40 167 error('invalid muthresh value, must be non-negative');
idamnjanovic@40 168 end
idamnjanovic@40 169
idamnjanovic@40 170
idamnjanovic@40 171
idamnjanovic@40 172
idamnjanovic@40 173
idamnjanovic@40 174 % determine dictionary size %
idamnjanovic@40 175
idamnjanovic@40 176 if (isfield(params,'initdict'))
idamnjanovic@40 177 if (any(size(params.initdict)==1) && all(iswhole(params.initdict(:))))
idamnjanovic@40 178 dictsize = length(params.initdict);
idamnjanovic@40 179 else
idamnjanovic@40 180 dictsize = size(params.initdict,2);
idamnjanovic@40 181 end
idamnjanovic@40 182 end
idamnjanovic@40 183 if (isfield(params,'dictsize')) % this superceedes the size determined by initdict
idamnjanovic@40 184 dictsize = params.dictsize;
idamnjanovic@40 185 end
idamnjanovic@40 186
idamnjanovic@40 187 if (size(X,2) < dictsize)
idamnjanovic@40 188 error('Number of training signals is smaller than number of atoms to train');
idamnjanovic@40 189 end
idamnjanovic@40 190
idamnjanovic@40 191
idamnjanovic@40 192 % initialize the dictionary %
idamnjanovic@40 193
idamnjanovic@40 194 if (isfield(params,'initdict'))
idamnjanovic@40 195 if (any(size(params.initdict)==1) && all(iswhole(params.initdict(:))))
idamnjanovic@40 196 D = X(:,params.initdict(1:dictsize));
idamnjanovic@40 197 else
idamnjanovic@40 198 if (size(params.initdict,1)~=size(X,1) || size(params.initdict,2)<dictsize)
idamnjanovic@40 199 error('Invalid initial dictionary');
idamnjanovic@40 200 end
idamnjanovic@40 201 D = params.initdict(:,1:dictsize);
idamnjanovic@40 202 end
idamnjanovic@40 203 else
idamnjanovic@40 204 data_ids = find(colnorms_squared(X) > 1e-6); % ensure no zero data elements are chosen
idamnjanovic@40 205 perm = randperm(length(data_ids));
idamnjanovic@40 206 D = X(:,data_ids(perm(1:dictsize)));
idamnjanovic@40 207 end
idamnjanovic@40 208
idamnjanovic@40 209 % normalize the dictionary %
idamnjanovic@40 210
idamnjanovic@40 211 % D = normcols(D);
idamnjanovic@40 212 % DtD=D'*D;
idamnjanovic@40 213
idamnjanovic@40 214 err = zeros(1,iternum);
idamnjanovic@40 215 gerr = zeros(1,iternum);
idamnjanovic@40 216
idamnjanovic@40 217 if (codemode == CODE_SPARSITY)
idamnjanovic@40 218 errstr = 'RMSE';
idamnjanovic@40 219 else
idamnjanovic@40 220 errstr = 'mean atomnum';
idamnjanovic@40 221 end
idamnjanovic@40 222 %X(:,p(X_norm_sort<thresh))=0;
idamnjanovic@40 223 % if (iternum==4)
idamnjanovic@40 224 % X_im=col2imstep(X, [256 256], [8 8]);
idamnjanovic@40 225 % else
idamnjanovic@40 226 % X_im=col2imstep(X, [512 512], [8 8]);
idamnjanovic@40 227 % end
idamnjanovic@40 228 % figure(10); imshow(X_im);
idamnjanovic@40 229
idamnjanovic@40 230 %p1=p(cumsum(X_norm_sort)./[1:size(X_norm_sort,2)]>thresh);
idamnjanovic@40 231 p1=p(X_norm_sort>thresh);
idamnjanovic@40 232 tic; idx=kmeans(X(:,p1)',4, 'Start', 'cluster','MaxIter',200); toc
idamnjanovic@40 233 D=[D D D D];
idamnjanovic@40 234 dictsize1=4*dictsize;
idamnjanovic@40 235 % X(:,setxor(p1,1:end))=0;
idamnjanovic@40 236 % X_im=col2imstep(X, [256 256], [8 8]);
idamnjanovic@40 237 % figure(10); imshow(X_im);
idamnjanovic@40 238 % if iternum==2
idamnjanovic@40 239 % D(:,1)=D(:,2);
idamnjanovic@40 240 % end
idamnjanovic@40 241 %p1=p1(p2(1:40000));
idamnjanovic@40 242 %end-min(40000, end)+1:end));%1:min(40000, end)));
idamnjanovic@40 243 %p1 = randperm(size(data,2));%size(data,2)
idamnjanovic@40 244 %data=data(:,p1);
idamnjanovic@40 245
idamnjanovic@40 246 C=(100000*thresh)*eye(dictsize1);
idamnjanovic@40 247 % figure(11);
idamnjanovic@40 248 w=zeros(dictsize,1);
idamnjanovic@40 249 replaced=zeros(dictsize,1);
idamnjanovic@40 250 u=zeros(dictsize,1);
idamnjanovic@40 251 % dictimg = showdict(D,[8 8],round(sqrt(size(D,2))),round(sqrt(size(D,2))),'lines','highcontrast');
idamnjanovic@40 252 % figure(11);imshow(imresize(dictimg,2,'nearest'));
idamnjanovic@40 253 % pause(1);
idamnjanovic@40 254 lambda=0.99986;%3+0.0001*params.linc;
idamnjanovic@40 255 for j=1:1
idamnjanovic@40 256 if size(p1,2)>60000
idamnjanovic@40 257 p2 = randperm(size(p1,2));
idamnjanovic@40 258
idamnjanovic@40 259 p2=sort(p2(1:60000));%min(floor(size(p1,2)/2),40000)));
idamnjanovic@40 260 size(p2,2)
idamnjanovic@40 261 data=X(:,p1(p2));
idamnjanovic@40 262 elseif size(p1,2)>0
idamnjanovic@40 263 p2 = randperm(size(p1,2));
idamnjanovic@40 264 size(p2,2)
idamnjanovic@40 265 data=X(:,p1);
idamnjanovic@40 266 else
idamnjanovic@40 267 break;
idamnjanovic@40 268 end
idamnjanovic@40 269 % figure(1);
idamnjanovic@40 270 % plot(sqrt(sum(data.^2, 1)));
idamnjanovic@40 271 % a=size(data,2)/4;
idamnjanovic@40 272 % lambda0=0.99;%1-16/numS+iternum*0.0001-0.0002
idamnjanovic@40 273 %C(1,1)=0;
idamnjanovic@40 274 modi=1000;
idamnjanovic@40 275 for i = 1:size(data,2)
idamnjanovic@40 276 % if norm(data(:,i))>thresh
idamnjanovic@40 277 % par.multA= @(x,par) multMatr(D,x); % user function y=Ax
idamnjanovic@40 278 % par.multAt=@(x,par) multMatrAdj(D,x); % user function y=A'*x
idamnjanovic@40 279 % par.y=data(:,i);
idamnjanovic@40 280 % w=SolveFISTA(D,data(:,i),'lambda',0.5*thresh);
idamnjanovic@40 281 % w=sesoptn(zeros(dictsize,1),par.func_u, par.func_x, par.multA, par.multAt,options,par);
idamnjanovic@40 282 %w = SMALL_chol(D,data(:,i), 256,32, thresh);%
idamnjanovic@40 283 %w = sparsecode(data(:,i), D, [], [], thresh);
idamnjanovic@40 284 w = omp2mex(D(:,((idx(i)-1)*dictsize+1):idx(i)*dictsize),data(:,i),[],[],[],thresh,0,-1,-1,0);
idamnjanovic@40 285
idamnjanovic@40 286 %w(find(w<1))=0;
idamnjanovic@40 287 %^2;
idamnjanovic@40 288 % lambda(i)=1-0.001/(1+i/a);
idamnjanovic@40 289 % if i<a
idamnjanovic@40 290 % lambda(i)=1-0.001*(1-(i/a));
idamnjanovic@40 291 % else
idamnjanovic@40 292 % lambda(i)=1;
idamnjanovic@40 293 % end
idamnjanovic@40 294 % param.lambda=thresh;
idamnjanovic@40 295 % param.mode=2;
idamnjanovic@40 296 % param.L=32;
idamnjanovic@40 297 % w=mexLasso(data(:,i), D, param);
idamnjanovic@40 298 spind=find(w);
idamnjanovic@40 299 %replaced(spind)=replaced(spind)+1;
idamnjanovic@40 300 %-0.001*(1/2)^(i/a);
idamnjanovic@40 301 % w_sp(i)=nnz(w);
idamnjanovic@40 302 residual = data(:,i) - D (:,((idx(i)-1)*dictsize+1):idx(i)*dictsize)* w;
idamnjanovic@40 303 %if ~isempty(spind)
idamnjanovic@40 304 %i
idamnjanovic@40 305 if (j==1)
idamnjanovic@40 306 C = C *(1/ lambda);
idamnjanovic@40 307 end
idamnjanovic@40 308 u = C(((idx(i)-1)*dictsize+1):idx(i)*dictsize,((idx(i)-1)*dictsize)+spind) * w(spind);
idamnjanovic@40 309
idamnjanovic@40 310 %spindu=find(u);
idamnjanovic@40 311 % v = D' * residual;
idamnjanovic@40 312
idamnjanovic@40 313 alfa = 1/(1 + w' * u);
idamnjanovic@40 314
idamnjanovic@40 315 D(:,((idx(i)-1)*dictsize+1):idx(i)*dictsize) = D (:,((idx(i)-1)*dictsize+1):idx(i)*dictsize)+ (alfa * residual) * u';
idamnjanovic@40 316
idamnjanovic@40 317 %uut=;
idamnjanovic@40 318 C (((idx(i)-1)*dictsize+1):idx(i)*dictsize,((idx(i)-1)*dictsize+1):idx(i)*dictsize)= C(((idx(i)-1)*dictsize+1):idx(i)*dictsize,((idx(i)-1)*dictsize+1):idx(i)*dictsize) - (alfa * u)* u';
idamnjanovic@40 319 % lambda=(19*lambda+1)/20;
idamnjanovic@40 320 % DtD = DtD + alfa * ( v*u' + u*v') + alfa^2 * (residual'*residual) * uut;
idamnjanovic@40 321
idamnjanovic@40 322 % if (mod(i,modi)==0)
idamnjanovic@40 323 % Ximd=zeros(size(X));
idamnjanovic@40 324 % Ximd(:,p1((i-modi+1:i)))=data(:,i-modi+1:i);
idamnjanovic@40 325 %
idamnjanovic@40 326 % if (iternum==4)
idamnjanovic@40 327 % X_ima(:,:,1)=col2imstep(Ximd, [256 256], [8 8]);
idamnjanovic@40 328 % X_ima(:,:,2)=col2imstep(X, [256 256], [8 8]);
idamnjanovic@40 329 % X_ima(:,:,3)=zeros(256,256);
idamnjanovic@40 330 % else
idamnjanovic@40 331 % X_ima(:,:,1)=col2imstep(Ximd, [512 512], [8 8]);
idamnjanovic@40 332 % X_ima(:,:,2)=col2imstep(X, [512 512], [8 8]);
idamnjanovic@40 333 % X_ima(:,:,3)=zeros(512,512);
idamnjanovic@40 334 % end
idamnjanovic@40 335 %
idamnjanovic@40 336 % dictimg1=dictimg;
idamnjanovic@40 337 % dictimg = showdict(D,[8 8],...
idamnjanovic@40 338 % round(sqrt(size(D,2))),round(sqrt(size(D,2))),'lines','highcontrast');
idamnjanovic@40 339 % dictimg1=(dictimg-dictimg1);
idamnjanovic@40 340 %
idamnjanovic@40 341 % figure(2);
idamnjanovic@40 342 % subplot(2,2,1); imshow(X_ima); title(sprintf('%d',i));
idamnjanovic@40 343 % subplot(2,2,3); imshow(imresize(dictimg,2,'nearest'));
idamnjanovic@40 344 % subplot(2,2,4); imshow(imresize(dictimg1,2,'nearest'));
idamnjanovic@40 345 % subplot(2,2,2);imshow(C*(255/max(max(C))));
idamnjanovic@40 346 % pause(0.02);
idamnjanovic@40 347 % if (i>=35000)
idamnjanovic@40 348 % modi=100;
idamnjanovic@40 349 % pause
idamnjanovic@40 350 % end;
idamnjanovic@40 351 % end
idamnjanovic@40 352 % end
idamnjanovic@40 353 end
idamnjanovic@40 354 %p1=p1(setxor(p2,1:end));
idamnjanovic@40 355 %[D,cleared_atoms] = cleardict(D,X,muthresh,p1,replaced);
idamnjanovic@40 356 %replaced=zeros(dictsize,1);
idamnjanovic@40 357 % W=sparsecode(data, D, [], [], thresh);
idamnjanovic@40 358 % data=D*W;
idamnjanovic@40 359 lambda=lambda+0.0002
idamnjanovic@40 360 end
idamnjanovic@40 361 %Gamma=mexLasso(data, D, param);
idamnjanovic@40 362 %err=compute_err(D,Gamma, data);
idamnjanovic@40 363 %[y,i]=max(err);
idamnjanovic@40 364 %D(:,1)=data(:,i)/norm(data(:,i));
idamnjanovic@40 365 D=normcols(D);
idamnjanovic@40 366 D_norm=sqrt(sum(D.^2, 1));
idamnjanovic@40 367 D_norm_1=sum(abs(D));
idamnjanovic@40 368 % X_norm_1=sum(abs(X));
idamnjanovic@40 369 % X_norm_inf=max(abs(X));
idamnjanovic@40 370 [D_norm_sort, p]=sort(D_norm_1, 'descend');
idamnjanovic@40 371 Dictionary = D;%D(:,p);
idamnjanovic@40 372 % figure(3);
idamnjanovic@40 373 % plot(lambda);
idamnjanovic@40 374 % mean(lambda);
idamnjanovic@40 375 % figure(4+j);plot(w_sp);
idamnjanovic@40 376 end
idamnjanovic@40 377
idamnjanovic@40 378 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
idamnjanovic@40 379 % sparsecode %
idamnjanovic@40 380 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
idamnjanovic@40 381
idamnjanovic@40 382 function Gamma = sparsecode(data,D,XtX,G,thresh)
idamnjanovic@40 383
idamnjanovic@40 384 global CODE_SPARSITY codemode
idamnjanovic@40 385 global MEM_HIGH memusage
idamnjanovic@40 386 global ompfunc ompparams
idamnjanovic@40 387
idamnjanovic@40 388 if (memusage < MEM_HIGH)
idamnjanovic@40 389 Gamma = ompfunc(D,data,G,thresh,ompparams{:});
idamnjanovic@40 390
idamnjanovic@40 391 else % memusage is high
idamnjanovic@40 392
idamnjanovic@40 393 if (codemode == CODE_SPARSITY)
idamnjanovic@40 394 Gamma = ompfunc(D'*data,G,thresh,ompparams{:});
idamnjanovic@40 395
idamnjanovic@40 396 else
idamnjanovic@40 397 Gamma = ompfunc(D, data, G, thresh,ompparams{:});
idamnjanovic@40 398 end
idamnjanovic@40 399
idamnjanovic@40 400 end
idamnjanovic@40 401
idamnjanovic@40 402 end
idamnjanovic@40 403
idamnjanovic@40 404
idamnjanovic@40 405 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
idamnjanovic@40 406 % compute_err %
idamnjanovic@40 407 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
idamnjanovic@40 408
idamnjanovic@40 409
idamnjanovic@40 410 function err = compute_err(D,Gamma,data)
idamnjanovic@40 411
idamnjanovic@40 412 global CODE_SPARSITY codemode
idamnjanovic@40 413
idamnjanovic@40 414 if (codemode == CODE_SPARSITY)
idamnjanovic@40 415 err = sqrt(sum(reperror2(data,D,Gamma))/numel(data));
idamnjanovic@40 416 else
idamnjanovic@40 417 err = nnz(Gamma)/size(data,2);
idamnjanovic@40 418 end
idamnjanovic@40 419
idamnjanovic@40 420 end
idamnjanovic@40 421
idamnjanovic@40 422
idamnjanovic@40 423
idamnjanovic@40 424 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
idamnjanovic@40 425 % cleardict %
idamnjanovic@40 426 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
idamnjanovic@40 427
idamnjanovic@40 428
idamnjanovic@40 429 function [D,cleared_atoms] = cleardict(D,X,muthresh,unused_sigs,replaced_atoms)
idamnjanovic@40 430
idamnjanovic@40 431 use_thresh = 4; % at least this number of samples must use the atom to be kept
idamnjanovic@40 432
idamnjanovic@40 433 dictsize = size(D,2);
idamnjanovic@40 434
idamnjanovic@40 435 % compute error in blocks to conserve memory
idamnjanovic@40 436 % err = zeros(1,size(X,2));
idamnjanovic@40 437 % blocks = [1:3000:size(X,2) size(X,2)+1];
idamnjanovic@40 438 % for i = 1:length(blocks)-1
idamnjanovic@40 439 % err(blocks(i):blocks(i+1)-1) = sum((X(:,blocks(i):blocks(i+1)-1)-D*Gamma(:,blocks(i):blocks(i+1)-1)).^2);
idamnjanovic@40 440 % end
idamnjanovic@40 441
idamnjanovic@40 442 cleared_atoms = 0;
idamnjanovic@40 443 usecount = replaced_atoms;%sum(abs(Gamma)>1e-7, 2);
idamnjanovic@40 444
idamnjanovic@40 445 for j = 1:dictsize
idamnjanovic@40 446
idamnjanovic@40 447 % compute G(:,j)
idamnjanovic@40 448 Gj = D'*D(:,j);
idamnjanovic@40 449 Gj(j) = 0;
idamnjanovic@40 450
idamnjanovic@40 451 % replace atom
idamnjanovic@40 452 if ( (max(Gj.^2)>muthresh^2 || usecount(j)<use_thresh) && ~replaced_atoms(j) )
idamnjanovic@40 453 % [y,i] = max(err(unused_sigs));
idamnjanovic@40 454 D(:,j) = X(:,unused_sigs(end)) / norm(X(:,unused_sigs(end)));
idamnjanovic@40 455 unused_sigs = unused_sigs([1:end-1]);
idamnjanovic@40 456 cleared_atoms = cleared_atoms+1;
idamnjanovic@40 457 end
idamnjanovic@40 458 end
idamnjanovic@40 459
idamnjanovic@40 460 end
idamnjanovic@40 461
idamnjanovic@40 462
idamnjanovic@40 463
idamnjanovic@40 464 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
idamnjanovic@40 465 % misc functions %
idamnjanovic@40 466 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
idamnjanovic@40 467
idamnjanovic@40 468
idamnjanovic@40 469 function err2 = reperror2(X,D,Gamma)
idamnjanovic@40 470
idamnjanovic@40 471 % compute in blocks to conserve memory
idamnjanovic@40 472 err2 = zeros(1,size(X,2));
idamnjanovic@40 473 blocksize = 2000;
idamnjanovic@40 474 for i = 1:blocksize:size(X,2)
idamnjanovic@40 475 blockids = i : min(i+blocksize-1,size(X,2));
idamnjanovic@40 476 err2(blockids) = sum((X(:,blockids) - D*Gamma(:,blockids)).^2);
idamnjanovic@40 477 end
idamnjanovic@40 478
idamnjanovic@40 479 end
idamnjanovic@40 480
idamnjanovic@40 481
idamnjanovic@40 482 function Y = colnorms_squared(X)
idamnjanovic@40 483
idamnjanovic@40 484 % compute in blocks to conserve memory
idamnjanovic@40 485 Y = zeros(1,size(X,2));
idamnjanovic@40 486 blocksize = 2000;
idamnjanovic@40 487 for i = 1:blocksize:size(X,2)
idamnjanovic@40 488 blockids = i : min(i+blocksize-1,size(X,2));
idamnjanovic@40 489 Y(blockids) = sum(X(:,blockids).^2);
idamnjanovic@40 490 end
idamnjanovic@40 491
idamnjanovic@40 492 end
idamnjanovic@40 493
idamnjanovic@40 494