%%  Audio Declipping Example
%   
%   CHANGE!!! This example is based on the experiment suggested by Professor Pierre
%   Vandergheynst on the SMALL meeting in Villars.
%   The idea behind is to use patches from source image as a dictionary in
%   which we represent target image using matching pursuit algorithm.
%   Calling Pierre_Problem function to get src image to be used as dictionary
%   and target image to be represented using MP with 3 patches from source image

%
%   Centre for Digital Music, Queen Mary, University of London.
%   This file copyright 2011 Ivan Damnjanovic.
%
%   This program is free software; you can redistribute it and/or
%   modify it under the terms of the GNU General Public License as
%   published by the Free Software Foundation; either version 2 of the
%   License, or (at your option) any later version.  See the file
%   COPYING included with this distribution for more information.
%
%%

clear all;

%   Defining the Problem structure

SMALL.Problem = generateAudioDeclippingProblem('male01_8kHz.wav', 0.6, 256, 0.5, @wRect, @wSine, @wRect, @Gabor_Dictionary, 2);

% %   Show original image and image that is used as a dictionary
% figure('Name', 'Original and Dictionary Image');
% 
% subplot(1,2,1); imagesc(SMALL.Problem.imageTrg/SMALL.Problem.maxval);
% title('Original Image');colormap(gray);axis off; axis image;
% subplot(1,2,2); imagesc(SMALL.Problem.imageSrc/SMALL.Problem.maxval);
% title('Dictionary image:');colormap(gray);axis off; axis image;
time=0;
error2=0.001^2;
coeffFrames = zeros(SMALL.Problem.p, SMALL.Problem.n);

for i=1:SMALL.Problem.n
    
    idx = find(SMALL.Problem.M(:,i));
    if size(idx,1)==SMALL.Problem.m
        continue
    end
    Dict = SMALL.Problem.B(idx,:);
    wDict = 1./sqrt(diag(Dict'*Dict));
    
    SMALL.Problem.A  = Dict*diag(wDict);
    
    SMALL.Problem.b1 = SMALL.Problem.b(idx,i);
    
    
    
    %   Defining the parameters sparse representation
    SMALL.solver=SMALL_init_solver;
    SMALL.solver.toolbox='ompbox';
    SMALL.solver.name='omp2Gabor';
    
    SMALL.solver.param=struct(...
        'epsilon', error2*size(idx,1),...
        'maxatoms', 128); 
    
    % Find solution
    
    SMALL.solver=SMALL_solve(SMALL.Problem, SMALL.solver);
    
    
    coeffFrames(:,i) = wDict .* SMALL.solver.solution;
    time = time + SMALL.solver.time;
    
    
      
end

%%   Set reconstruction function

SMALL.Problem.reconstruct=@(x) AudioDeclipping_reconstruct(x, SMALL.Problem);
reconstructed=SMALL.Problem.reconstruct(coeffFrames);



%% Plot results

xClipped = SMALL.Problem.clipped;
xClean   = SMALL.Problem.original;
xEst1    = reconstructed.audioAllSamples;
xEst2    = reconstructed.audioOnlyClipped;
fs       = SMALL.Problem.fs;

figure
hold on
plot(xClipped,'r')
plot(xClean)
plot(xEst2,'--g')
plot([1;length(xClipped)],[1;1]*[-1,1]*max(abs(xClipped)),':r')
legend('Clipped','True solution','Estimate')

% Normalized and save sounds
normX = 1.1*max(abs([xEst1(:);xEst2(:);xClean(:)]));
L = min([length(xEst2),length(xEst1),length(xClean),length(xEst1),length(xClipped)]);
xEst1 = xEst1(1:L)/normX;
xEst2 = xEst2(1:L)/normX;
xClipped = xClipped(1:L)/normX;
xClean = xClean(1:L)/normX;
% wavwrite(xEst1,fs,[expParam.destDir 'xEst1.wav']);
% wavwrite(xEst2,fs,[expParam.destDir 'xEst2.wav']);
% wavwrite(xClipped,fs,[expParam.destDir 'xClipped.wav']);
% wavwrite(xClean,fs,[expParam.destDir 'xClean.wav']);
