annotate examples/SMALL_test_mocod.m @ 177:714fa7b8c1ad danieleb

added ramirez dl (to be completed) and MOCOD dictionary update
author Daniele Barchiesi <daniele.barchiesi@eecs.qmul.ac.uk>
date Thu, 17 Nov 2011 11:18:25 +0000
parents
children 0dc98f1c60bb
rev   line source
daniele@177 1 clc, clear, close all
daniele@177 2
daniele@177 3 %% Parameteres
daniele@177 4 nTrials = 10; %number of trials of the experiment
daniele@177 5
daniele@177 6 % Dictionary learning parameters
daniele@177 7 toolbox = 'TwoStepDL'; %dictionary learning toolbox
daniele@177 8 dicUpdate = 'mocod'; %dictionary learning updates
daniele@177 9 zeta = logspace(-2,2,10);
daniele@177 10 eta = logspace(-2,2,10);
daniele@177 11
daniele@177 12 iterNum = 20; %number of iterations
daniele@177 13 epsilon = 1e-6; %tolerance level
daniele@177 14 dictSize = 512; %number of atoms in the dictionary
daniele@177 15 percActiveAtoms = 5; %percentage of active atoms
daniele@177 16
daniele@177 17 % Test signal parameters
daniele@177 18 signal = audio('music03_16kHz.wav'); %audio signal
daniele@177 19 blockSize = 256; %size of audio frames
daniele@177 20 overlap = 0.5; %overlap between consecutive frames
daniele@177 21
daniele@177 22 % Dependent parameters
daniele@177 23 nActiveAtoms = fix(blockSize/100*percActiveAtoms); %number of active atoms
daniele@177 24
daniele@177 25 % Initial dictionaries
daniele@177 26 gaborParam = struct('N',blockSize,'redundancyFactor',2,'wd',@rectwin);
daniele@177 27 gaborDict = Gabor_Dictionary(gaborParam);
daniele@177 28 initDicts = {[],gaborDict};
daniele@177 29
daniele@177 30 %% Generate audio approximation problem
daniele@177 31 signal = buffer(signal,blockSize,blockSize*overlap,@rectwin); %buffer frames of audio into columns of the matrix S
daniele@177 32 SMALL.Problem.b = signal.S;
daniele@177 33 SMALL.Problem.b1 = SMALL.Problem.b; % copy signals from training set b to test set b1 (needed for later functions)
daniele@177 34
daniele@177 35 % omp2 sparse representation solver
daniele@177 36 ompParam = struct('X',SMALL.Problem.b,'epsilon',epsilon,'maxatoms',nActiveAtoms); %parameters
daniele@177 37 solver = SMALL_init_solver('ompbox','omp2',ompParam,false); %solver structure
daniele@177 38
daniele@177 39 %% Test
daniele@177 40 nInitDicts = length(initDicts); %number of initial dictionaries
daniele@177 41 nZetas = length(zeta);
daniele@177 42 nEtas = length(eta);
daniele@177 43
daniele@177 44 SMALL.DL(nTrials,nInitDicts,nZetas,nEtas) = SMALL_init_DL(toolbox); %create dictionary learning structures
daniele@177 45 for iTrial=1:nTrials
daniele@177 46 for iInitDicts=1:nInitDicts
daniele@177 47 for iZetas=1:nZetas
daniele@177 48 for iEtas=1:nEtas
daniele@177 49 SMALL.DL(iTrial,iInitDicts,iZetas,iEtas).toolbox = toolbox;
daniele@177 50 SMALL.DL(iTrial,iInitDicts,iZetas,iEtas).name = dicUpdate;
daniele@177 51 SMALL.DL(iTrial,iInitDicts,iZetas,iEtas).profile = true;
daniele@177 52 SMALL.DL(iTrial,iInitDicts,iZetas,iEtas).param = ...
daniele@177 53 struct('data',SMALL.Problem.b,...
daniele@177 54 'Tdata',nActiveAtoms,...
daniele@177 55 'dictsize',dictSize,...
daniele@177 56 'iternum',iterNum,...
daniele@177 57 'memusage','high',...
daniele@177 58 'solver',solver,...
daniele@177 59 'initdict',initDicts(iInitDicts),...
daniele@177 60 'zeta',zeta(iZetas),...
daniele@177 61 'eta',eta(iEtas));
daniele@177 62 SMALL.DL(iTrial,iInitDicts,iZetas,iEtas) = ...
daniele@177 63 SMALL_learn(SMALL.Problem,SMALL.DL(iTrial,iInitDicts,iZetas,iEtas));
daniele@177 64 end
daniele@177 65 end
daniele@177 66 end
daniele@177 67 end
daniele@177 68
daniele@177 69 %% Evaluate coherence and snr of representation for the various methods
daniele@177 70 sr = zeros(size(SMALL.DL)); %signal to noise ratio
daniele@177 71 mu = zeros(iTrial,iInitDicts,iZetas,iEtas); %coherence
daniele@177 72 dic(size(SMALL.DL)) = dictionary; %initialise dictionary objects
daniele@177 73 for iTrial=1:nTrials
daniele@177 74 for iInitDicts=1:nInitDicts
daniele@177 75 for iZetas=1:nZetas
daniele@177 76 for iEtas=1:nEtas
daniele@177 77 %Sparse representation
daniele@177 78 SMALL.Problem.A = SMALL.DL(iTrial,iInitDicts,iZetas,iEtas).D;
daniele@177 79 tempSolver = SMALL_solve(SMALL.Problem,solver);
daniele@177 80 %calculate snr
daniele@177 81 sr(iTrial,iInitDicts,iZetas,iEtas) = ...
daniele@177 82 snr(SMALL.Problem.b,SMALL.DL(iTrial,iInitDicts,iZetas,iEtas).D*tempSolver.solution);
daniele@177 83 %calculate mu
daniele@177 84 dic(iTrial,iInitDicts,iZetas,iEtas) = ...
daniele@177 85 dictionary(SMALL.DL(iTrial,iInitDicts,iZetas,iEtas).D);
daniele@177 86 mu(iTrial,iInitDicts,iZetas,iEtas) = ...
daniele@177 87 dic(iTrial,iInitDicts,iZetas,iEtas).coherence;
daniele@177 88 end
daniele@177 89 end
daniele@177 90 end
daniele@177 91 end
daniele@177 92
daniele@177 93 save('MOCOD.mat')
daniele@177 94
daniele@177 95 %% Plot results
daniele@177 96 minMu = sqrt((dictSize-blockSize)/(blockSize*(dictSize-1))); %lowe bound on coherence
daniele@177 97 initDictsNames = {'Data','Gabor'};
daniele@177 98 lineStyles = {'k.-','r*-','b+-'};
daniele@177 99 for iInitDict=1:nInitDicts
daniele@177 100 figure, hold on, grid on
daniele@177 101 title([initDictsNames{iInitDict} ' Initialisation']);
daniele@177 102 coherenceLevels = squeeze(mean(mu(:,iInitDict,:,:),1));
daniele@177 103 meanSNRs = squeeze(mean(sr(:,iInitDict,:,:),1));
daniele@177 104 %stdSNRs = squeeze(std(sr(:,iInitDict,iZetas,iEtas),0,1));
daniele@177 105 subplot(2,2,1)
daniele@177 106 surf(eta,zeta,coherenceLevels);
daniele@177 107 set(gca,'Xscale','log','Yscale','log','ZLim',[0 1.4]);
daniele@177 108 view(gca,130,20)
daniele@177 109 xlabel('\eta');
daniele@177 110 ylabel('\zeta');
daniele@177 111 zlabel('\mu');
daniele@177 112 title('Coherence')
daniele@177 113
daniele@177 114 subplot(2,2,2)
daniele@177 115 surf(eta,zeta,meanSNRs);
daniele@177 116 set(gca,'Xscale','log','Yscale','log','ZLim',[0 25]);
daniele@177 117 view(gca,130,20)
daniele@177 118 xlabel('\eta');
daniele@177 119 ylabel('\zeta');
daniele@177 120 zlabel('SNR (dB)');
daniele@177 121 title('Reconstruction Error')
daniele@177 122
daniele@177 123 subplot(2,2,[3 4])
daniele@177 124 mus = mu(:,iInitDict,:,:);
daniele@177 125 mus = mus(:);
daniele@177 126 SNRs = sr(:,iInitDict,:,:);
daniele@177 127 SNRs = SNRs(:);
daniele@177 128 [un idx] = sort(mus);
daniele@177 129 plot([1 1],[0 25],'k')
daniele@177 130 hold on, grid on
daniele@177 131 scatter(mus(idx),SNRs(idx),'k+');
daniele@177 132 plot([minMu minMu],[0 25],'k--')
daniele@177 133 set(gca,'YLim',[0 25],'XLim',[0 1.4]);
daniele@177 134 xlabel('\mu');
daniele@177 135 ylabel('SNR (dB)');
daniele@177 136 legend([{'\mu_{max}'},'MOCOD',{'\mu_{min}'}]);
daniele@177 137 title('Coherence-Reconstruction Error Tradeoff')
daniele@177 138
daniele@177 139 % plot([minMu minMu],[0 25],'k--')
daniele@177 140 %
daniele@177 141 % set(gca,'YLim',[0 25],'XLim',[0 1.4]);
daniele@177 142 % legend([{'\mu_{max}'},dicDecorrNames,{'\mu_{min}'}]);
daniele@177 143 % xlabel('\mu');
daniele@177 144 % ylabel('SNR (dB)');
daniele@177 145 end