clear

%% Parameteres

% Dictionary learning parameters
toolbox   = 'TwoStepDL'; %dictionary learning toolbox
dicUpdate = {'ksvd','mailhe'}; %dictionary updates
iterNum   = 20; %number of iterations

% Test signal parameters
signal    = audio('music03_16kHz.wav'); %audio signal
blockSize = 256; %size of audio frames
dictSize  = 512; %number of atoms in the dictionary
overlap   = 0.5; %overlap between consecutive frames
sigma     = 1e6; %snr of noise (set to be negligible so that the problem becomes approximation rather than denoising)
percActiveAtoms = 5; %percentage of active atoms

% Dependent parameters
nActiveAtoms = fix(blockSize/100*percActiveAtoms); %number of active atoms
epsilon      = 1/sigma; %error constraint for sparse representation step (corresponds to noise applied to signals)
%minCoherence = sqrt((dictSize-blockSize)/(blockSize*(dictSize-1))); %target coherence (based on coherence lower bound)
minCoherence = 0.4;		%target coherence

% Initial dictionaries
dctDict = dictionary('dct',blockSize,dictSize);
dctDict = dctDict.phi;
gaborParam = struct('N',blockSize,'redundancyFactor',2,'wd',@rectwin);
gaborDict = Gabor_Dictionary(gaborParam);

%% Generate audio denoising problem with low noise (audio representation)
SMALL.Problem = generateAudioDenoiseProblem(signal.s,[],blockSize,...
    dictSize,overlap,sigma); % generate representation problem
SMALL.Problem.b1 = SMALL.Problem.b; % copy signals from training set b to test set b1 (needed for later functions)

% omp2 sparse representation solver
ompParam = struct('X',SMALL.Problem.b,'epsilon',epsilon,'maxatoms',nActiveAtoms); %parameters
solver = SMALL_init_solver('ompbox','omp2',ompParam,false); %solver structure


%% Test ksvd dictionary update
name = dicUpdate{1}; %use ksvd update
SMALL.DL(1:9) = SMALL_init_DL(toolbox,name); %create dictionary learning structures

% learn with random initialisation and no decorrelation
SMALL.DL(1).param = struct('data',SMALL.Problem.b,'Tdata',nActiveAtoms,...
    'dictsize',dictSize,'iternum',iterNum,'memusage','high','solver',solver,...
    'decFcn','none'); %parameters for the dictionary learning
SMALL.DL(1) = SMALL_learn(SMALL.Problem,SMALL.DL(1)); %learn dictionary

% learn with random initialisation and mailhe decorrelation
SMALL.DL(2).param = struct('data',SMALL.Problem.b,'Tdata',nActiveAtoms,...
    'dictsize',dictSize,'iternum',iterNum,'memusage','high','solver',solver,...
    'decFcn','mailhe','coherence',minCoherence); %parameters for the dictionary learning
SMALL.DL(2) = SMALL_learn(SMALL.Problem,SMALL.DL(2)); %learn dictionary

% learn with random initialisation and tropp decorrelation
SMALL.DL(3).param = struct('data',SMALL.Problem.b,'Tdata',nActiveAtoms,...
    'dictsize',dictSize,'iternum',iterNum,'memusage','high','solver',solver,...
    'decFcn','tropp','coherence',minCoherence); %parameters for the dictionary learning
SMALL.DL(3) = SMALL_learn(SMALL.Problem,SMALL.DL(3)); %learn dictionary

% Learn with dct initialisation and no decorrelation
SMALL.DL(4).param = struct('data',SMALL.Problem.b,'Tdata',nActiveAtoms,...
    'dictsize',dictSize,'iternum',iterNum,'memusage','high','solver',solver,...
    'decFcn','none','initdict',dctDict); %parameters for the dictionary learning
SMALL.DL(4) = SMALL_learn(SMALL.Problem,SMALL.DL(4)); %learn dictionary

% learn with dct initialisation and mailhe decorrelation
SMALL.DL(5).param = struct('data',SMALL.Problem.b,'Tdata',nActiveAtoms,...
    'dictsize',dictSize,'iternum',iterNum,'memusage','high','solver',solver,...
    'decFcn','mailhe','coherence',minCoherence,'initdict',dctDict); %parameters for the dictionary learning
SMALL.DL(5) = SMALL_learn(SMALL.Problem,SMALL.DL(5)); %learn dictionary

% learn with dct initialisation and tropp decorrelation
SMALL.DL(6).param = struct('data',SMALL.Problem.b,'Tdata',nActiveAtoms,...
    'dictsize',dictSize,'iternum',iterNum,'memusage','high','solver',solver,...
    'decFcn','tropp','coherence',minCoherence,'initdict',dctDict); %parameters for the dictionary learning
SMALL.DL(6) = SMALL_learn(SMALL.Problem,SMALL.DL(6)); %learn dictionary

% Learn with gabor initialisation and no decorrelation
SMALL.DL(7).param = struct('data',SMALL.Problem.b,'Tdata',nActiveAtoms,...
    'dictsize',dictSize,'iternum',iterNum,'memusage','high','solver',solver,...
    'decFcn','none','initdict',gaborDict); %parameters for the dictionary learning
SMALL.DL(7) = SMALL_learn(SMALL.Problem,SMALL.DL(7)); %learn dictionary

% learn with gabor initialisation and mailhe decorrelation
SMALL.DL(8).param = struct('data',SMALL.Problem.b,'Tdata',nActiveAtoms,...
    'dictsize',dictSize,'iternum',iterNum,'memusage','high','solver',solver,...
    'decFcn','mailhe','coherence',minCoherence,'initdict',gaborDict); %parameters for the dictionary learning
SMALL.DL(8) = SMALL_learn(SMALL.Problem,SMALL.DL(8)); %learn dictionary

% learn with gabor initialisation and tropp decorrelation
SMALL.DL(9).param = struct('data',SMALL.Problem.b,'Tdata',nActiveAtoms,...
    'dictsize',dictSize,'iternum',iterNum,'memusage','high','solver',solver,...
    'decFcn','tropp','coherence',minCoherence,'initdict',gaborDict); %parameters for the dictionary learning
SMALL.DL(9) = SMALL_learn(SMALL.Problem,SMALL.DL(9)); %learn dictionary

%% Test mailhe dictionary update
name = dicUpdate{2}; %use mailhe update
SMALL.DL(10:18) = SMALL_init_DL(toolbox,name); %create dictionary learning structure

% learn with random initialisation and no decorrelation
SMALL.DL(10).param = struct('data',SMALL.Problem.b,'Tdata',nActiveAtoms,...
    'dictsize',dictSize,'iternum',iterNum,'memusage','high','solver',solver,...
    'decFcn','none'); %parameters for the dictionary learning
SMALL.DL(10) = SMALL_learn(SMALL.Problem,SMALL.DL(10)); %learn dictionary

% learn with random initialisation and mailhe decorrelation
SMALL.DL(11).param = struct('data',SMALL.Problem.b,'Tdata',nActiveAtoms,...
    'dictsize',dictSize,'iternum',iterNum,'memusage','high','solver',solver,...
    'decFcn','mailhe','coherence',minCoherence); %parameters for the dictionary learning
SMALL.DL(11) = SMALL_learn(SMALL.Problem,SMALL.DL(11)); %learn dictionary

% learn with random initialisation and tropp decorrelation
SMALL.DL(12).param = struct('data',SMALL.Problem.b,'Tdata',nActiveAtoms,...
    'dictsize',dictSize,'iternum',iterNum,'memusage','high','solver',solver,...
    'decFcn','tropp','coherence',minCoherence); %parameters for the dictionary learning
SMALL.DL(12) = SMALL_learn(SMALL.Problem,SMALL.DL(12)); %learn dictionary

% Learn with dct initialisation and no decorrelation
SMALL.DL(13).param = struct('data',SMALL.Problem.b,'Tdata',nActiveAtoms,...
    'dictsize',dictSize,'iternum',iterNum,'memusage','high','solver',solver,...
    'decFcn','none','initdict',dctDict); %parameters for the dictionary learning
SMALL.DL(13) = SMALL_learn(SMALL.Problem,SMALL.DL(13)); %learn dictionary

% learn with dct initialisation and mailhe decorrelation
SMALL.DL(14).param = struct('data',SMALL.Problem.b,'Tdata',nActiveAtoms,...
    'dictsize',dictSize,'iternum',iterNum,'memusage','high','solver',solver,...
    'decFcn','mailhe','coherence',minCoherence,'initdict',dctDict); %parameters for the dictionary learning
SMALL.DL(14) = SMALL_learn(SMALL.Problem,SMALL.DL(14)); %learn dictionary

% learn with dct initialisation and tropp decorrelation
SMALL.DL(15).param = struct('data',SMALL.Problem.b,'Tdata',nActiveAtoms,...
    'dictsize',dictSize,'iternum',iterNum,'memusage','high','solver',solver,...
    'decFcn','tropp','coherence',minCoherence,'initdict',dctDict); %parameters for the dictionary learning
SMALL.DL(15) = SMALL_learn(SMALL.Problem,SMALL.DL(15)); %learn dictionary

% Learn with gabor initialisation and no decorrelation
SMALL.DL(16).param = struct('data',SMALL.Problem.b,'Tdata',nActiveAtoms,...
    'dictsize',dictSize,'iternum',iterNum,'memusage','high','solver',solver,...
    'decFcn','none','initdict',gaborDict); %parameters for the dictionary learning
SMALL.DL(16) = SMALL_learn(SMALL.Problem,SMALL.DL(16)); %learn dictionary

% learn with gabor initialisation and mailhe decorrelation
SMALL.DL(17).param = struct('data',SMALL.Problem.b,'Tdata',nActiveAtoms,...
    'dictsize',dictSize,'iternum',iterNum,'memusage','high','solver',solver,...
    'decFcn','mailhe','coherence',minCoherence,'initdict',gaborDict); %parameters for the dictionary learning
SMALL.DL(17) = SMALL_learn(SMALL.Problem,SMALL.DL(17)); %learn dictionary

% learn with gabor initialisation and tropp decorrelation
SMALL.DL(18).param = struct('data',SMALL.Problem.b,'Tdata',nActiveAtoms,...
    'dictsize',dictSize,'iternum',iterNum,'memusage','high','solver',solver,...
    'decFcn','tropp','coherence',minCoherence,'initdict',gaborDict); %parameters for the dictionary learning
SMALL.DL(18) = SMALL_learn(SMALL.Problem,SMALL.DL(18)); %learn dictionary

%% Evaluate coherence and snr of representation for the various methods
sigNoiseRatio = zeros(18,1);
mu = zeros(18,1);
for i=1:18
    SMALL.Problem.A = SMALL.DL(i).D;
    tempSolver = SMALL_solve(SMALL.Problem,solver);
    sigNoiseRatio(i) = snr(SMALL.Problem.b,SMALL.DL(i).D*tempSolver.solution);
    dic(i) = dictionary(SMALL.DL(i).D);
    mu(i) = dic(i).coherence;
end

%% Plot results
minMu = sqrt((dictSize-blockSize)/(blockSize*(dictSize-1)));
maxSNR = max(sigNoiseRatio);

figure, subplot(1,2,1)
snrMat = buffer(sigNoiseRatio(1:9),3);
bar(snrMat');
title('Signal to noise ratio')
xlabel('Initial dictionary')
ylabel('SNR (dB)')
set(gca,'XTickLabel',{'data','dct','gabor'});
legend('none','Mailhe','Tropp')
grid on

% subplot(2,2,2), grid on
% snrMat = buffer(sigNoiseRatio(10:18),3);
% bar(snrMat');
% title('SNR - Mailhe Update')
% xlabel('Initial dictionary')
% ylabel('SNR (dB)')
% set(gca,'XTickLabel',{'data','dct','gabor'},'YLim',[0 maxSNR+1]);
% legend('none','Mailhe','Tropp')
% grid on

subplot(1,2,2), hold on, grid on
title('Coherence')
muMat = buffer(mu(1:9),3);
line([0.5 3.5],[1 1],'Color','r');
bar(muMat');
line([0.5 3.5],[minMu minMu],'Color','k');
set(gca,'XTick',1:3,'XTickLabel',{'data','dct','gabor'},'YLim',[0 1.05])
legend('\mu_{max}','none','Mailhe','Tropp','\mu_{min}')
ylabel('\mu')
xlabel('Initial dictionary')

% subplot(2,2,4), hold on, grid on
% title('Coherence - Mailhe Update')
% muMat = buffer(mu(10:18),3);
% line([0.5 3.5],[1 1],'Color','r');
% bar(muMat');
% line([0.5 3.5],[minMu minMu],'Color','k');
% set(gca,'XTick',1:3,'XTickLabel',{'data','dct','gabor'},'YLim',[0 1.05])
% legend('\mu_{max}','none','Mailhe','Tropp','\mu_{min}')
% ylabel('\mu')
% xlabel('Initial dictionary')
