annotate examples/SMALL_DL_test.m @ 194:9b0595a8478d danieleb

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