annotate examples/SMALL_DL_test.m @ 195:d50f5bdbe14c luisf_dev

- Added SMALL_DL_test: simple DL showcase - Added dico_decorr_symmetric: improved version of INK-SVD decorrelation step - Debugged SMALL_learn, SMALLBoxInit and SMALL_two_step_DL
author Daniele Barchiesi <daniele.barchiesi@eecs.qmul.ac.uk>
date Wed, 14 Mar 2012 14:42:52 +0000
parents
children fd0b5d36f6ad
rev   line source
daniele@195 1 function SMALL_DL_test
daniele@195 2 clear, clc, close all
daniele@195 3 % Create a 2-dimensional dataset of points that are oriented in 3
daniele@195 4 % directions on a x/y plane
daniele@195 5
daniele@195 6 %
daniele@195 7 % Centre for Digital Music, Queen Mary, University of London.
daniele@195 8 % This file copyright 2012 Daniele Barchiesi.
daniele@195 9 %
daniele@195 10 % This program is free software; you can redistribute it and/or
daniele@195 11 % modify it under the terms of the GNU General Public License as
daniele@195 12 % published by the Free Software Foundation; either version 2 of the
daniele@195 13 % License, or (at your option) any later version. See the file
daniele@195 14 % COPYING included with this distribution for more information.
daniele@195 15
daniele@195 16 nData = 10000; %number of data
daniele@195 17 theta = [pi/6 pi/3 4*pi/6]; %angles
daniele@195 18 nAngles = length(theta); %number of angles
daniele@195 19 Q = [cos(theta); sin(theta)]; %rotation matrix
daniele@195 20 X = Q*randmog(nAngles,nData); %training data
daniele@195 21
daniele@195 22 % find principal directions using PCA
daniele@195 23 XXt = X*X'; %cross correlation matrix
daniele@195 24 [U ~] = svd(XXt); %svd of XXt
daniele@195 25
daniele@195 26 scale = 3; %scale factor for plots
daniele@195 27 subplot(1,2,1), hold on
daniele@195 28 title('Principal Component Analysis')
daniele@195 29 scatter(X(1,:), X(2,:),'.'); %scatter training data
daniele@195 30 O = zeros(size(U)); %origin
daniele@195 31 quiver(O(1,1:2),O(2,1:2),scale*U(1,:),scale*U(2,:),...
daniele@195 32 'LineWidth',2,'Color','k') %plot atoms
daniele@195 33 axis equal %scale axis
daniele@195 34
daniele@195 35 subplot(1,2,2), hold on
daniele@195 36 title('K-SVD Dictionary')
daniele@195 37 scatter(X(1,:), X(2,:),'.');
daniele@195 38 axis equal
daniele@195 39
daniele@195 40 nAtoms = 3; %number of atoms in the dictionary
daniele@195 41 nIter = 1; %number of dictionary learning iterations
daniele@195 42 initDict = normc(randn(2,nAtoms)); %random initial dictionary
daniele@195 43 O = zeros(size(initDict)); %origin
daniele@195 44
daniele@195 45 % apply dictionary learning algorithm
daniele@195 46 ksvd_params = struct('data',X,... %training data
daniele@195 47 'Tdata',1,... %sparsity level
daniele@195 48 'dictsize',nAtoms,... %number of atoms
daniele@195 49 'initdict',initDict,...%initial dictionary
daniele@195 50 'iternum',10); %number of iterations
daniele@195 51 DL = SMALL_init_DL('ksvd','ksvd',ksvd_params); %dictionary learning structure
daniele@195 52 DL.D = initDict; %copy initial dictionary in solution variable
daniele@195 53 problem = struct('b',X); %copy training data in problem structure
daniele@195 54
daniele@195 55 xdata = DL.D(1,:);
daniele@195 56 ydata = DL.D(2,:);
daniele@195 57 qPlot = quiver(O(1,:),O(2,:),scale*initDict(1,:),scale*initDict(2,:),...
daniele@195 58 'LineWidth',2,'Color','k','UDataSource','xdata','VDataSource','ydata');
daniele@195 59
daniele@195 60 for iIter=1:nIter
daniele@195 61 DL.ksvd_params.initdict = DL.D;
daniele@195 62 pause
daniele@195 63 DL = SMALL_learn(problem,DL); %learn dictionary
daniele@195 64 xdata = scale*DL.D(1,:);
daniele@195 65 ydata = scale*DL.D(2,:);
daniele@195 66 refreshdata(gcf,'caller');
daniele@195 67 end
daniele@195 68
daniele@195 69
daniele@195 70 function X = randmog(m, n)
daniele@195 71 % RANDMOG - Generate mixture of Gaussians
daniele@195 72 s = [0.2 2];
daniele@195 73 % Choose which Gaussian
daniele@195 74 G1 = (rand(m, n) < 0.9);
daniele@195 75 % Make them
daniele@195 76 X = (G1.*s(1) + (1-G1).*s(2)) .* randn(m,n);