daniele@195: function SMALL_DL_test daniele@195: clear, clc, close all daniele@195: % Create a 2-dimensional dataset of points that are oriented in 3 daniele@195: % directions on a x/y plane daniele@195: daniele@195: % daniele@195: % Centre for Digital Music, Queen Mary, University of London. daniele@195: % This file copyright 2012 Daniele Barchiesi. daniele@195: % daniele@195: % This program is free software; you can redistribute it and/or daniele@195: % modify it under the terms of the GNU General Public License as daniele@195: % published by the Free Software Foundation; either version 2 of the daniele@195: % License, or (at your option) any later version. See the file daniele@195: % COPYING included with this distribution for more information. daniele@195: daniele@195: nData = 10000; %number of data daniele@195: theta = [pi/6 pi/3 4*pi/6]; %angles daniele@195: nAngles = length(theta); %number of angles daniele@195: Q = [cos(theta); sin(theta)]; %rotation matrix daniele@195: X = Q*randmog(nAngles,nData); %training data daniele@195: daniele@195: % find principal directions using PCA daniele@195: XXt = X*X'; %cross correlation matrix daniele@195: [U ~] = svd(XXt); %svd of XXt daniele@195: daniele@195: scale = 3; %scale factor for plots daniele@195: subplot(1,2,1), hold on daniele@195: title('Principal Component Analysis') daniele@195: scatter(X(1,:), X(2,:),'.'); %scatter training data daniele@195: O = zeros(size(U)); %origin daniele@195: quiver(O(1,1:2),O(2,1:2),scale*U(1,:),scale*U(2,:),... daniele@195: 'LineWidth',2,'Color','k') %plot atoms daniele@195: axis equal %scale axis daniele@195: daniele@195: subplot(1,2,2), hold on daniele@195: title('K-SVD Dictionary') daniele@195: scatter(X(1,:), X(2,:),'.'); daniele@195: axis equal daniele@195: daniele@195: nAtoms = 3; %number of atoms in the dictionary daniele@195: nIter = 1; %number of dictionary learning iterations daniele@195: initDict = normc(randn(2,nAtoms)); %random initial dictionary daniele@195: O = zeros(size(initDict)); %origin daniele@195: daniele@195: % apply dictionary learning algorithm daniele@195: ksvd_params = struct('data',X,... %training data daniele@195: 'Tdata',1,... %sparsity level daniele@195: 'dictsize',nAtoms,... %number of atoms daniele@195: 'initdict',initDict,...%initial dictionary daniele@195: 'iternum',10); %number of iterations daniele@195: DL = SMALL_init_DL('ksvd','ksvd',ksvd_params); %dictionary learning structure daniele@195: DL.D = initDict; %copy initial dictionary in solution variable daniele@195: problem = struct('b',X); %copy training data in problem structure daniele@195: daniele@195: xdata = DL.D(1,:); daniele@195: ydata = DL.D(2,:); daniele@195: qPlot = quiver(O(1,:),O(2,:),scale*initDict(1,:),scale*initDict(2,:),... daniele@195: 'LineWidth',2,'Color','k','UDataSource','xdata','VDataSource','ydata'); daniele@195: daniele@195: for iIter=1:nIter daniele@195: DL.ksvd_params.initdict = DL.D; daniele@195: pause daniele@195: DL = SMALL_learn(problem,DL); %learn dictionary daniele@195: xdata = scale*DL.D(1,:); daniele@195: ydata = scale*DL.D(2,:); daniele@195: refreshdata(gcf,'caller'); daniele@195: end daniele@195: daniele@195: daniele@195: function X = randmog(m, n) daniele@195: % RANDMOG - Generate mixture of Gaussians daniele@195: s = [0.2 2]; daniele@195: % Choose which Gaussian daniele@195: G1 = (rand(m, n) < 0.9); daniele@195: % Make them daniele@195: X = (G1.*s(1) + (1-G1).*s(2)) .* randn(m,n);