# HG changeset patch # User Daniele Barchiesi # Date 1331661226 0 # Node ID 9b0595a8478d1c6264b42694ec2cef717efe23d9 # Parent cc540df790f4afc751361d7bc926d9b995b31775 Debugged SMALL_DL_test and added copyright info diff -r cc540df790f4 -r 9b0595a8478d examples/SMALL_DL_test.m --- a/examples/SMALL_DL_test.m Fri Mar 09 15:12:01 2012 +0000 +++ b/examples/SMALL_DL_test.m Tue Mar 13 17:53:46 2012 +0000 @@ -1,55 +1,69 @@ function SMALL_DL_test clear, clc, close all % Create a 2-dimensional dataset of points that are oriented in 3 -% directions on a x-y plane +% directions on a x/y plane + +% +% Centre for Digital Music, Queen Mary, University of London. +% This file copyright 2012 Daniele Barchiesi. +% +% This program is free software; you can redistribute it and/or +% modify it under the terms of the GNU General Public License as +% published by the Free Software Foundation; either version 2 of the +% License, or (at your option) any later version. See the file +% COPYING included with this distribution for more information. + nData = 10000; %number of data theta = [pi/6 pi/3 4*pi/6]; %angles -m = length(theta); +nAngles = length(theta); %number of angles Q = [cos(theta); sin(theta)]; %rotation matrix -X = Q*randmog(m,nData); +X = Q*randmog(nAngles,nData); %training data -% find principal directions using PCA and plot them -XXt = X*X'; -[U ~] = svd(XXt); -scale = 3; +% find principal directions using PCA +XXt = X*X'; %cross correlation matrix +[U ~] = svd(XXt); %svd of XXt + +scale = 3; %scale factor for plots subplot(1,2,1), hold on title('Principal Component Analysis') -scatter(X(1,:), X(2,:),'.'); -O = zeros(size(U)); -quiver(O(1,1:2),O(2,1:2),scale*U(1,:),scale*U(2,:),'LineWidth',2,'Color','k') -axis equal +scatter(X(1,:), X(2,:),'.'); %scatter training data +O = zeros(size(U)); %origin +quiver(O(1,1:2),O(2,1:2),scale*U(1,:),scale*U(2,:),... + 'LineWidth',2,'Color','k') %plot atoms +axis equal %scale axis subplot(1,2,2), hold on title('K-SVD Dictionary') scatter(X(1,:), X(2,:),'.'); axis equal -nAtoms = 3; -initDict = randn(2,nAtoms); -nIter = 10; -O = zeros(size(initDict)); + +nAtoms = 3; %number of atoms in the dictionary +nIter = 1; %number of dictionary learning iterations +initDict = normc(randn(2,nAtoms)); %random initial dictionary +O = zeros(size(initDict)); %origin + % apply dictionary learning algorithm -ksvd_params = struct('data',X,... %training data - 'Tdata',1,... %sparsity level +ksvd_params = struct('data',X,... %training data + 'Tdata',1,... %sparsity level 'dictsize',nAtoms,... %number of atoms - 'initdict',initDict,... - 'iternum',10); %number of iterations -DL = SMALL_init_DL('ksvd','ksvd',ksvd_params); -DL.D = initDict; + 'initdict',initDict,...%initial dictionary + 'iternum',10); %number of iterations +DL = SMALL_init_DL('ksvd','ksvd',ksvd_params); %dictionary learning structure +DL.D = initDict; %copy initial dictionary in solution variable +problem = struct('b',X); %copy training data in problem structure + xdata = DL.D(1,:); ydata = DL.D(2,:); qPlot = quiver(O(1,:),O(2,:),scale*initDict(1,:),scale*initDict(2,:),... - 'LineWidth',2,'Color','k','XDataSource','xdata','YDataSource','ydata'); -problem = struct('b',X); %training data + 'LineWidth',2,'Color','k','UDataSource','xdata','VDataSource','ydata'); -%plot dictionary and learn for iIter=1:nIter DL.ksvd_params.initdict = DL.D; + pause DL = SMALL_learn(problem,DL); %learn dictionary - xdata = DL.D(1,:); - ydata = DL.D(2,:); - pause + xdata = scale*DL.D(1,:); + ydata = scale*DL.D(2,:); refreshdata(gcf,'caller'); - %quiver(O(1,:),O(2,:),scale*DL.D(1,:),scale*DL.D(2,:),'LineWidth',2,'Color','k'); end