function task2()

%--------------------------------------------------------------------------
% Discrete Haar Transformation
%--------------------------------------------------------------------------

dx = 1/32;
x = 0:dx:1-dx;

% box signal
y = zeros(size(x));
y(abs(x-10*dx) < 4*dx) = 1;

figure;
subplot(4, 1, 1);
stairs(x, y, '-k.');
title('Original')

% compute the discrete Haar transformation of y
% [approx, detail] = TODO;

subplot(4, 1, 2);
% plot the approximation coefficients using the 'stairs' command 
% stairs(x(1:2:end), TODO, '-b.');
title('Approximation');
subplot(4, 1, 3);
% plot the detail coefficients using the 'stairs' command
% TODO;
title('Detail');

% compute the inverse Haar transformation
% recon = TODO;

subplot(4, 1, 4);
stairs(x, recon, '-k.');
title('Reconstructed')


%--------------------------------------------------------------------------
% Haar multi-resolution analysis
%--------------------------------------------------------------------------
dx = 1/128;
x = 0:dx:1-dx;

% cosine
y = cos(4*2*pi*x);

% number of iterated decompositions
ndec = 3;
% the number of decompositions can not be larger than log2(signal length)
if (ndec < 1)
    error('Number of decompositions has to be at least 1!')
elseif (ndec > log2(length(x)))
    error('Number of decompositions is chosen too high for the given signal length!');
end

maxnumfig = 3 + ndec;
numfig = 1;

% on/off switch for the plotting of the signal decomposition
displayfig = 1;

if displayfig
    scrsz = get(0, 'ScreenSize');
    h = figure('Position', [10 scrsz(4)*0.1 scrsz(3)*0.8 scrsz(4)*0.8]);
    subplot(maxnumfig, 1, numfig);
    plot(x, y, '-k.')
    title('Original');
end

% ----------- Analysis ----------
% Initially, y contains the current approximation
approx = y;
waveletcoeffs = [];
% iteratively compute the discrete Haar transform. Remember that ndec contains the number of decompositions.
% for TODO
    
    % increment numfig
    % numfig = TODO;
    
    % compute the dht for the current approximation
    % TODO;
    
    % append the detail coefficient to the left of the wavelet coefficient vector
	% TODO;
    
    if displayfig
        subplot(maxnumfig, 1, numfig);
        % plot the detail coefficients
        % stairs(x(1:2^i:end), TODO, '-r.');
        xlim([0 1]);
        title(['Detail ' int2str(i)]);
        subplot(maxnumfig, 1, numfig + 1);
        % plot approximation coefficients using the 'stairs' command
        % stairs TODO;
        xlim([0 1]);
        title(['Approximation ' int2str(i)]);

        % wait for keyboard input
        % TODO;
    end

% end

% append the approximation of the last decomposition to the left of the wavelet
% coefficient vector
% TODO;


% -------- Further processing ---------
% *** NOTE: Please test first your wavelet analysis and wavelet synthesis
% *** (below). If those two parts work, then continue to operate on this part
% -------------------------------------

% compute the number of wavelet coefficients at the ndec-th decomposition level
% ncoeffs = TODO;

% initialize the energy vector of length ndec with 0
% energy = TODO;

% define a threshold
% s = 0.1;

% iterate over all decompositions, from the coarsest to the finest resolution
% for TODO
    
    % store detail coefficients of the i-th level in a vector curdetail.
    % curdetail = waveletcoeffs(TODO);
    
    % compute the energy of the wavelet coefficients at the i-th level
    % energy(TODO) = TODO;
    
    % apply the threshold s to the wavelet coefficients
    % TODO;
    
    % after each iteration, the number of approximation coefficients is doubled.
    % ncoeffs = TODO;
    
% end

% plot the energy of the detail coefficients, in dependency of their level of
% wavelet decomposition.
% figure;
% bar TODO;



% ----------- Synthesis ----------
% Compute the number of approximation coefficients and detail coefficients
% after ndec decompositions
%
% ncoeffs = TODO;
%
% Iteratively reconstruct the original signal. Start at the coarsest decomposition.
% for TODO
    
	% compute the inverse Haar transformation. In vector waveletcoeffs, replace
	% the respective approximation coefficients and detail coefficients with
	% the reconstructed signal of the next higher resolution.
    % waveletcoeffs(TODO) = TODO;
    
    % The number of approximation coefficients doubles after each iteration
    % ncoeffs = TODO;
    
% end

if displayfig
    figure(h);
    subplot(maxnumfig, 1, numfig + 2);
    % plot here the reconstructed signal
    % plot(x, TODO, '-k.');
    title('Reconstructed')
end


% -------- Further processing ---------
% *** NOTE: Please test first your wavelet analysis and wavelet synthesis
% *** (below). If those two parts work, then implement the upper section on
% *** 'Further processing', then finally this part
% -------------------------------------
% figure;
% subplot(3, 1, 1);
% plot(x, y, '-k.');
% title('Original');
% subplot(3, 1, 2);
% plot(x, waveletcoeffs, '-k.');
% title('Modified');
% subplot(3, 1, 3);
% plot(x, waveletcoeffs - y, '-k.');
% title('Difference');


end

%--------------------------------------------------------------------------
% 1-D DHT
%--------------------------------------------------------------------------
function [approximation, detail] = dht(signal)

% Define the low pass (lp) and high pass (hp) filters
% lp = TODO;
% hp = TODO;

% Periodically supplement the signal for one element
% TODO

% Compute convolutions with the high pass and low pass filter
% approximation = conv2(TODO, TODO, 'valid');
% detail = TODO;

% downsample approximation and detail coefficients by a factor of 2
% approximation = TODO;
% detail = TODO;

end

%--------------------------------------------------------------------------
% 1-D IDHT
%--------------------------------------------------------------------------
function [recon] = idht(approximation, detail)

% definition of the low pass and high pass filters for Haar synthesis
% rlp = TODO;
% rhp = TODO;

% upsample the approximation and detail coefficients by a factor of 2
% usapprox = TODO;
% usdetail = TODO;

% periodically supplement both coefficients by one element
% usapprox = TODO;
% usdetail = TODO;

% convolve both parts with the high pass and low pass filters. Add the results
% recon = TODO;


end

