annotate examples/SMALL_test_mocod.m @ 181:0dc98f1c60bb danieleb

minor edits
author Daniele Barchiesi <daniele.barchiesi@eecs.qmul.ac.uk>
date Thu, 05 Jan 2012 15:46:13 +0000
parents 714fa7b8c1ad
children
rev   line source
daniele@181 1 %% SMALL_test_mocod
daniele@181 2 % Script that tests the twostep dictionary learning algorithm with MOCOD
daniele@181 3 % dictionary update.
daniele@181 4 %
daniele@181 5 % REFERENCES
daniele@181 6 % D. Barchiesi and M. D. Plumbely, Learning incoherenct dictionaries for
daniele@181 7 % sparse approximation using iterative projections and rotations.
daniele@181 8 %% Clear and close
daniele@177 9 clc, clear, close all
daniele@177 10
daniele@177 11 %% Parameteres
daniele@181 12 nTrials = 10; %number of trials of the experiment
daniele@177 13
daniele@177 14 % Dictionary learning parameters
daniele@181 15 toolbox = 'TwoStepDL'; %dictionary learning toolbox
daniele@181 16 dicUpdate = 'mocod'; %dictionary learning updates
daniele@181 17 zeta = logspace(-2,2,10); %range of values for the incoherence term
daniele@181 18 eta = logspace(-2,2,10); %range of values for the unit norm term
daniele@181 19 iterNum = 20; %number of iterations
daniele@181 20 epsilon = 1e-6; %tolerance level
daniele@181 21 dictSize = 512; %number of atoms in the dictionary
daniele@181 22 percActiveAtoms = 5; %percentage of active atoms
daniele@177 23
daniele@177 24 % Test signal parameters
daniele@177 25 signal = audio('music03_16kHz.wav'); %audio signal
daniele@177 26 blockSize = 256; %size of audio frames
daniele@177 27 overlap = 0.5; %overlap between consecutive frames
daniele@177 28
daniele@177 29 % Dependent parameters
daniele@177 30 nActiveAtoms = fix(blockSize/100*percActiveAtoms); %number of active atoms
daniele@177 31
daniele@177 32 % Initial dictionaries
daniele@177 33 gaborParam = struct('N',blockSize,'redundancyFactor',2,'wd',@rectwin);
daniele@177 34 gaborDict = Gabor_Dictionary(gaborParam);
daniele@181 35 initDicts = {[],gaborDict}; %cell containing initial dictionaries
daniele@177 36
daniele@177 37 %% Generate audio approximation problem
daniele@181 38 %buffer frames of audio into columns of the matrix S
daniele@181 39 signal = buffer(signal,blockSize,blockSize*overlap,@rectwin);
daniele@177 40 SMALL.Problem.b = signal.S;
daniele@177 41
daniele@181 42 %copy signals from training set b to test set b1 (needed for later functions)
daniele@181 43 SMALL.Problem.b1 = SMALL.Problem.b;
daniele@181 44
daniele@181 45 %omp2 sparse representation solver
daniele@181 46 ompParam = struct('X',SMALL.Problem.b,...
daniele@181 47 'epsilon',epsilon,...
daniele@181 48 'maxatoms',nActiveAtoms); %parameters
daniele@177 49 solver = SMALL_init_solver('ompbox','omp2',ompParam,false); %solver structure
daniele@177 50
daniele@177 51 %% Test
daniele@181 52 nInitDicts = length(initDicts);%number of initial dictionaries
daniele@181 53 nZetas = length(zeta); %number of incoherence penalty parameters
daniele@181 54 nEtas = length(eta); %number of unit norm penalty parameters
daniele@177 55
daniele@181 56 %create dictionary learning structures
daniele@181 57 SMALL.DL(nTrials,nInitDicts,nZetas,nEtas) = SMALL_init_DL(toolbox);
daniele@177 58 for iTrial=1:nTrials
daniele@177 59 for iInitDicts=1:nInitDicts
daniele@177 60 for iZetas=1:nZetas
daniele@177 61 for iEtas=1:nEtas
daniele@177 62 SMALL.DL(iTrial,iInitDicts,iZetas,iEtas).toolbox = toolbox;
daniele@177 63 SMALL.DL(iTrial,iInitDicts,iZetas,iEtas).name = dicUpdate;
daniele@177 64 SMALL.DL(iTrial,iInitDicts,iZetas,iEtas).profile = true;
daniele@177 65 SMALL.DL(iTrial,iInitDicts,iZetas,iEtas).param = ...
daniele@181 66 struct('data',SMALL.Problem.b,... %observed data
daniele@181 67 'Tdata',nActiveAtoms,... %active atoms
daniele@181 68 'dictsize',dictSize,... %number of atoms
daniele@181 69 'iternum',iterNum,... %number of iterations
daniele@181 70 'memusage','high',... %memory usage
daniele@181 71 'solver',solver,... %sparse approx solver
daniele@181 72 'initdict',initDicts(iInitDicts),...%initial dictionary
daniele@181 73 'zeta',zeta(iZetas),... %incoherence penalty factor
daniele@181 74 'eta',eta(iEtas)); %unit norm penalty factor
daniele@181 75 %learn dictionary
daniele@177 76 SMALL.DL(iTrial,iInitDicts,iZetas,iEtas) = ...
daniele@177 77 SMALL_learn(SMALL.Problem,SMALL.DL(iTrial,iInitDicts,iZetas,iEtas));
daniele@177 78 end
daniele@177 79 end
daniele@177 80 end
daniele@177 81 end
daniele@177 82
daniele@181 83 %% Evaluate coherence and snr of representation
daniele@181 84 sr = zeros(size(SMALL.DL)); %signal to noise ratio
daniele@177 85 mu = zeros(iTrial,iInitDicts,iZetas,iEtas); %coherence
daniele@181 86 dic(size(SMALL.DL)) = dictionary; %initialise dictionary objects
daniele@177 87 for iTrial=1:nTrials
daniele@177 88 for iInitDicts=1:nInitDicts
daniele@177 89 for iZetas=1:nZetas
daniele@177 90 for iEtas=1:nEtas
daniele@177 91 %Sparse representation
daniele@177 92 SMALL.Problem.A = SMALL.DL(iTrial,iInitDicts,iZetas,iEtas).D;
daniele@177 93 tempSolver = SMALL_solve(SMALL.Problem,solver);
daniele@177 94 %calculate snr
daniele@177 95 sr(iTrial,iInitDicts,iZetas,iEtas) = ...
daniele@181 96 snr(SMALL.Problem.b,...
daniele@181 97 SMALL.DL(iTrial,iInitDicts,iZetas,iEtas).D*tempSolver.solution);
daniele@177 98 %calculate mu
daniele@177 99 dic(iTrial,iInitDicts,iZetas,iEtas) = ...
daniele@177 100 dictionary(SMALL.DL(iTrial,iInitDicts,iZetas,iEtas).D);
daniele@177 101 mu(iTrial,iInitDicts,iZetas,iEtas) = ...
daniele@177 102 dic(iTrial,iInitDicts,iZetas,iEtas).coherence;
daniele@177 103 end
daniele@177 104 end
daniele@177 105 end
daniele@177 106 end
daniele@177 107
daniele@177 108 %% Plot results
daniele@181 109 minMu = sqrt((dictSize-blockSize)/(blockSize*(dictSize-1)));%lower bound on coherence
daniele@181 110 initDictsNames = {'Data','Gabor'}; %names of initial dictionaries
daniele@181 111 lineStyles = {'k.-','r*-','b+-'};
daniele@177 112 for iInitDict=1:nInitDicts
daniele@177 113 figure, hold on, grid on
daniele@181 114 %print initial dictionary as figure title
daniele@181 115 DisplayFigureTitle([initDictsNames{iInitDict} ' Initialisation']);
daniele@181 116 % set(gcf,'Units','Normalized');
daniele@181 117 % txh = annotation(gcf,'textbox',[0.4,0.95,0.2,0.05]);
daniele@181 118 % set(txh,'String',[initDictsNames{iInitDict} ' Initialisation'],...
daniele@181 119 % 'LineStyle','none','HorizontalAlignment','center');
daniele@181 120 %calculate mean coherence levels and SNRs over trials
daniele@177 121 coherenceLevels = squeeze(mean(mu(:,iInitDict,:,:),1));
daniele@177 122 meanSNRs = squeeze(mean(sr(:,iInitDict,:,:),1));
daniele@181 123 % stdSNRs = squeeze(std(sr(:,iInitDict,iZetas,iEtas),0,1));
daniele@181 124 %plot coherence levels
daniele@177 125 subplot(2,2,1)
daniele@177 126 surf(eta,zeta,coherenceLevels);
daniele@177 127 set(gca,'Xscale','log','Yscale','log','ZLim',[0 1.4]);
daniele@177 128 view(gca,130,20)
daniele@177 129 xlabel('\eta');
daniele@177 130 ylabel('\zeta');
daniele@177 131 zlabel('\mu');
daniele@177 132 title('Coherence')
daniele@177 133
daniele@181 134 %plot SNRs
daniele@177 135 subplot(2,2,2)
daniele@177 136 surf(eta,zeta,meanSNRs);
daniele@177 137 set(gca,'Xscale','log','Yscale','log','ZLim',[0 25]);
daniele@177 138 view(gca,130,20)
daniele@177 139 xlabel('\eta');
daniele@177 140 ylabel('\zeta');
daniele@177 141 zlabel('SNR (dB)');
daniele@177 142 title('Reconstruction Error')
daniele@177 143
daniele@181 144 %plot mu/SNR scatter
daniele@177 145 subplot(2,2,[3 4])
daniele@177 146 mus = mu(:,iInitDict,:,:);
daniele@177 147 mus = mus(:);
daniele@177 148 SNRs = sr(:,iInitDict,:,:);
daniele@177 149 SNRs = SNRs(:);
daniele@177 150 [un idx] = sort(mus);
daniele@177 151 plot([1 1],[0 25],'k')
daniele@177 152 hold on, grid on
daniele@177 153 scatter(mus(idx),SNRs(idx),'k+');
daniele@177 154 plot([minMu minMu],[0 25],'k--')
daniele@177 155 set(gca,'YLim',[0 25],'XLim',[0 1.4]);
daniele@177 156 xlabel('\mu');
daniele@177 157 ylabel('SNR (dB)');
daniele@177 158 legend([{'\mu_{max}'},'MOCOD',{'\mu_{min}'}]);
daniele@177 159 title('Coherence-Reconstruction Error Tradeoff')
daniele@177 160
daniele@177 161 % plot([minMu minMu],[0 25],'k--')
daniele@177 162 %
daniele@177 163 % set(gca,'YLim',[0 25],'XLim',[0 1.4]);
daniele@177 164 % legend([{'\mu_{max}'},dicDecorrNames,{'\mu_{min}'}]);
daniele@177 165 % xlabel('\mu');
daniele@177 166 % ylabel('SNR (dB)');
daniele@177 167 end