%% Exercises from Linear algebra, signal processing, and wavelets. A unified approach.\\ Matlab version
% 
% Author: \O yvind Ryan
% 
% Date: Feb 12, 2017
% 
% % Externaldocuments: applinalg
% % Mapping from exercise labels to numbers: label2numbers = {'example:freqresp3first': '6.4', 'oblig2ex2': '6.20'}
% 
% 
% Table of contents:
% 
%  Sound and Fourier series 
%  Digital sound and Discrete Fourier analysis 
%  Operations on digital sound: digital filters 
%  Symmetric filters and the DCT 
%  Motivation for wavelets and some simple examples 
%  The filter representation of wavelets 
%      Example 6.4: Plotting the frequency responses 
%      Exercise 6.20: Computing filters and frequency responses 
%  Constructing interesting wavelets 
%  The polyphase representation and wavelets 
%  Digital images 
%  Using tensor products to apply wavelets to images 
%  Appendix: Basic Linear Algebra 
%  Appendix: Signal processing and linear algebra: a translation guide 
% 
% 
% 
%% Sound and Fourier series
%% Digital sound and Discrete Fourier analysis
%% Operations on digital sound: digital filters
%% Symmetric filters and the DCT
%% Motivation for wavelets and some simple examples
%% The filter representation of wavelets
% 
% 
% % --- begin exercise ---
% 
%% Example 6.4: Plotting the frequency responses
% 
% % keywords = ipynbwavfilters; mwavfilters
% 
% In order to verify the low-pass/high-pass characteristics of $G_0$ and $G_1$, 
% let us plot the frequency responses of the wavelets we have considered. 
% To plot $\lambda_{G_0}(\omega)$ and $\lambda_{G_1}(\omega)$ for the Haar wavelet, we can write 
% 
freqresp_alg('Haar', 1, 0)
freqresp_alg('Haar', 0, 0)
% To plot the same frequency response for the alternative piecewise linear wavelet, we can write 
% 
freqresp_alg('pwl2', 1, 0)
freqresp_alg('pwl2', 0, 0)
% 
% % --- end exercise ---
% 
% 
% 
% 
% % --- begin exercise ---
% 
%% Exercise 6.20: Computing filters and frequency responses
% 
% % keywords = ipynbwavfilters; mwavfilters
% 
% Let us return to the piecewise linear wavelet from Exercise 5.39. 
% 
A = zeros(4);
b = zeros(4, 1);
for k = 0:3
A(k + 1, :) = [quad(@(t)t.^k.*(1 - abs(t)), - 1, 1)...
quad(@(t)t.^k.*(1 - abs(t - 1)), 0, 2)...
quad(@(t)t.^k.*(1 - abs(t + 1)), -2, 0)...
quad(@(t)t.^k.*(1 - abs(t - 2)), 1, 3)];
b(k + 1) = quad(@(t)t.^k.*(1 - 2*abs(t - 1/2)), 0, 1);
end
coeffs=A\b;
% 
% 
% *a)* With $\hat{\psi}$ as defined as in b) in Exercise 5.39,
% compute the coordinates of $\hat{\psi}$ in the basis $\vphi_1$ (i.e. $[\hat\psi]_{\vphi_1}$) with $N=8$, i.e. compute the IDWT of
% 
% $$\begin{equation*}[\hat{\psi}]_{(\vphi_0,\vpsi_0)}=(-\alpha,-\beta,-\delta,0,0,0,0,-\gamma)\oplus(1,0,0,0,0,0,0,0),\end{equation*}$$
% which is the coordinate vector you computed in d) in Exercise 5.39.
% For this, you should use the function |IDWTImpl|, with the kernel of the piecewise linear wavelet without symmetric extension as input.
% Explain that this gives you the filter coefficients of $G_1$.
% 
% 
% % --- begin solution of exercise ---
% *Solution.* The code which can be used looks like this:
% 
g1=IDWTImpl([-coeffs(1);-coeffs(2);-coeffs(4);0;0;0;0;-coeffs(3);...
1; 0; 0; 0; 0; 0; 0; 0], 1, 'pwl0', 'per');
g1 = [g1(14:16); g1(1:6)]; % Compact filter notation
% Note that we have used a kernel which does not make symmetric extensions.
% 
% % --- end solution of exercise ---
% 
% *b)* Plot the frequency response of $G_1$.
% 
% 
% % --- begin solution of exercise ---
% *Solution.* The code can look as follows:
% 
omega = linspace(0,2*pi,100);
plot(omega, g1(5) + g1(6)*2*cos(omega) + g1(7)*2*cos(2*omega)...
+ g1(8)*2*cos(3*omega) + g1(9)*2*cos(4*omega))
% 
% % --- end solution of exercise ---
% 
% % --- end exercise ---
% 
% 
%% Constructing interesting wavelets
%% The polyphase representation and wavelets
%% Digital images
%% Using tensor products to apply wavelets to images
%% Appendix: Basic Linear Algebra
%% Appendix: Signal processing and linear algebra: a translation guide

