annotate util/classes/@dictionary/dictionary.m @ 193:cc540df790f4 danieleb

Simple example that demonstrated dictionary learning... to be completed
author Daniele Barchiesi <daniele.barchiesi@eecs.qmul.ac.uk>
date Fri, 09 Mar 2012 15:12:01 +0000
parents 8fc38e8df8c6
children
rev   line source
daniele@160 1 classdef dictionary
daniele@160 2 %% Dictionary for sparse representation
daniele@160 3 properties
daniele@160 4 phi %Matrix containing the dictionary
daniele@160 5 len %Length of basis functions
daniele@160 6 nAtoms %Number of basis function
daniele@160 7 name %String containing the matrix ensemble from which the dictionary is drawn
daniele@160 8 end
daniele@160 9 properties (Dependent = true)
daniele@160 10 redundancy %Redundancy of the dictionary: nAtoms/len
daniele@160 11 coherence %Maximum inner product of different basis
daniele@160 12 isNormalised %True if the atoms have unit norm
daniele@160 13 rank %rank of the dictionary
daniele@160 14 end
daniele@160 15
daniele@160 16 methods
daniele@160 17 %% Constructor
daniele@160 18 function obj = dictionary(phi,len,nAtoms)
daniele@160 19 % obj = dictionary(phi,len,nAtoms)
daniele@160 20 % INPUTS:
daniele@160 21 % - phi: either a string specifying a matrix ensamble or a
daniele@160 22 % matrix defining an explicit dictionary
daniele@160 23 % - len: length of the atoms (only for implicit dictionaries)
daniele@160 24 % - nAtoms: number of atoms (only for implicit dictionaries)
daniele@160 25 if nargin
daniele@160 26 if ~ischar(phi)
daniele@160 27 [obj.len obj.nAtoms] = size(phi);
daniele@160 28 obj.phi = phi;
daniele@160 29 obj.name = 'explicit';
daniele@160 30 else
daniele@160 31 switch lower(phi)
daniele@160 32 case 'dct'
daniele@160 33 obj.phi = dctmatrix(len,nAtoms);
daniele@184 34 case 'grassmannian'
daniele@160 35 obj.phi = grassmanian(len,nAtoms);
daniele@160 36 otherwise
daniele@160 37 obj.phi = MatrixEnsemble(len,nAtoms,phi);
daniele@160 38 end
daniele@160 39 obj.len = len;
daniele@160 40 obj.nAtoms = nAtoms;
daniele@160 41 obj.name = lower(phi);
daniele@160 42 end
daniele@160 43 end
daniele@184 44 end
daniele@160 45 %% Dependent properties
daniele@160 46 function redundancy = get.redundancy(obj)
daniele@160 47 redundancy = obj.nAtoms/obj.len;
daniele@160 48 end
daniele@160 49 function coherence = get.coherence(obj)
daniele@160 50 obj.phi = normcol(obj.phi);
daniele@160 51 G = obj.phi'*obj.phi;
daniele@160 52 G = G - eye(size(G));
daniele@160 53 coherence = max(abs(G(:)));
daniele@160 54 end
daniele@160 55 function isNormalised = get.isNormalised(obj)
daniele@160 56 isNormalised = norm(sum(conj(obj.phi).*obj.phi) - ...
daniele@160 57 ones(1,obj.nAtoms))<1e-9;
daniele@160 58 end
daniele@160 59 function r = get.rank(obj)
daniele@160 60 r = rank(obj.phi);
daniele@160 61 end
daniele@160 62 %% Operations
daniele@160 63 function obj = normalize(obj)
daniele@160 64 obj.phi = normcol(obj.phi);
daniele@184 65 end
daniele@160 66 %% Visualization
daniele@160 67 function image(obj)
daniele@160 68 %Image of the dictionary
daniele@160 69 if isreal(obj.phi)
daniele@160 70 imagesc(obj.phi);
daniele@160 71 title('Dictionary');
daniele@160 72 xlabel('Atom number');
daniele@160 73 else
daniele@160 74 subplot(2,1,1)
daniele@160 75 imagesc(real(obj.phi));
daniele@160 76 title('Real');
daniele@160 77 xlabel('Atom number');
daniele@160 78 subplot(2,1,2)
daniele@160 79 imagesc(imag(obj.phi));
daniele@160 80 title('Imaginary');
daniele@160 81 xlabel('Atom number');
daniele@160 82 end
daniele@160 83 end
daniele@160 84 function imagegram(obj)
daniele@160 85 G = obj.phi'*obj.phi;
daniele@160 86 imagesc(G);
daniele@160 87 title('Gram Matrix')
daniele@160 88 end
daniele@160 89 function plot(obj,n)
daniele@160 90 %Plot of the n-th basis
daniele@160 91 if isreal(obj.phi)
daniele@160 92 plot(obj.phi(:,n));
daniele@160 93 title(['Atom number ' num2str(n) '/' num2str(size(obj.phi,2))]);
daniele@160 94 else
daniele@160 95 subplot(2,1,1)
daniele@160 96 plot(real(obj.phi(:,n)));
daniele@160 97 title(['Atom number ' num2str(n) '/' num2str(size(obj.phi,2)) ' - Real']);
daniele@160 98 subplot(2,1,2)
daniele@160 99 plot(imag(obj.phi(:,n)));
daniele@160 100 title(['Atom number ' num2str(n) '/' num2str(size(obj.phi,2)) ' - Imaginary']);
daniele@160 101 end
daniele@184 102 end
daniele@160 103 function movie(obj)
daniele@160 104 %Movie of the basis
daniele@160 105 for i=1:size(obj.phi,2)
daniele@160 106 obj.plot(i);
daniele@160 107 pause(1/25);
daniele@160 108 end
daniele@160 109 end
daniele@160 110 end
daniele@160 111 end