%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% 
% FI:PA171 - Digital Filters
%
% topic: Bases & Unitary matrices & Decomposition
% author: David Svoboda (c) 2019
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%



%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Task: 
% -----
% Prove orthogonality & orthonormality of Fourier basis.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% Convolution of any signal/image with several Dirac impulses %%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% read sample image and downsample it
I = readim;
J = resample(I,0.25)

% prepare 2D array of Dirac impulses
Z = zeros(1000,1000);
Z(1:200:end, 1:200:end)=1;

% perform convolution
dip_image(conv2(double(J),Z))

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% Unitary matrices and their inversion %%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% prepare the matrix A and vector 'v'
A = [-sqrt(2)/2 -sqrt(2)/2; sqrt(2)/2 -sqrt(2)/2]
v = [-1 3]'

% test for orthogonality and orthonormality of the basis
dot(A(1,:),A(2,:))
dot(A(1,:),A(1,:))
dot(A(2,:),A(2,:))

% forward mapping (transform)
w = A*v

% backward mapping (transform)
A'*w

%
% Try basis formed by `hadamard(4)` or `hadamard(8)`.
% Does it work if not normalized?
%

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% Decomposition of functions in n-dimensional space %%
%% (how to compute the inner product of functions)   %%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
base1 = sin([0:pi/16:2*pi]); base1(round(end/2):end)=0;
base2 = sin([0:pi/16:2*pi]); base2(1:round(end/2))=0;
f = 2*sin([0:pi/16:2*pi]);
plot(f, 's', 'color', 'blue');
hold on;
plot(base1, 's', 'color', 'red');
plot(base2, 's', 'color', 'green');
hold off;

% (1) Check the orthogonality of 'base1' and 'base2'
% (2) Check their orthonormality. 
% (3) Decompose 'f' into 'base1' and 'base2' and choose the appropriate
% weights for each basis function.
% (4) Validate your decomposition by reverse mapping of the 'f' into the
% standard basis.

