
% Example to construct orthogonal wavelet filters

clear,

c = [0.25 0.5 1 0.25];

disp('given filter'), c

C = [1    1    1    1
     c(1) c(2) c(3) c(4)
     0    0    c(1) c(2)
     c(3) c(4) 0    0];
b = [2 2 0 0]';

% C*ct = b
% expresses sum(ct)=2, SUM_n[c(n)*ct(n-2*k)] = 2*delta(k)
disp('The system C*ct = b'), bT = b', C

ct = C\b;

disp('biorthog PR filter'),ctT=ct'
disp('Check SUM(even)=1=SUM(odd)')

even = ct(1) + ct(3);
odd = ct(2) + ct(4);

disp(['even = ',num2str(even),',  odd = ',num2str(odd)]);
disp(' ')

disp('These are rational coefficients since ct*7 =')
ct_x_7 = ct'*7


b = [2 1 0 0]';
disp('With another normalization'), bT = b'

ct = C\b;

disp('biorthog PR filter'),ctT=ct'
disp(' ')

disp('These are rational coefficients since ct*7 =')
ct_x_7 = ct'*7

disp('...type any key to continue'),pause

% CDF 2,2

disp('The CDF(2,2) filter')
disp('The given filter')

c = [0 0.5 1 0.5 0],
C = [ 1    1    1    1    1
     c(1) c(2) c(3) c(4) c(5)
     0    0    c(1) c(2) c(3)
     c(3) c(4) c(5)  0    0
     0    -1    2   -3    4]
disp('Express SUM(ct)=1, SUM(c(n)*ct(n-2*k))=delta(k)');
disp('and the zero moment SUM((-1)^n*n*ct(n))=0 viz. H`(pi)=0');
disp('But the system is singular');

det_C = det(C)

disp('Express SUM(c(n)*ct(n-2*k))=delta(k)');
disp('and the zero moments H(pi)=H`(pi)=0');
disp('Now the system is non singular');
C = [c(1) c(2) c(3) c(4) c(5)
     0    0    c(1) c(2) c(3)
     c(3) c(4) c(5)  0    0
     1    -1   1    -1    1
     0    -1    2   -3    4]

det_C = det(C)
b = [2 0 0 0 0]';
ct = C\b;
disp('Right-hand side'),bT = b'
disp('Solution'),btT = ct'

% remark: for orthogonality delete rows
%     0    0    0   0   c(1)
%     c(5) 0    0   0   0
% because c(1) = 0 = c(5)
% This would not give an extra condition anyway
% CONCLUSION: the sum of the ct-coefficients follows automatically.

disp('...type any key to continue'),pause
disp('4-tap filter')
disp('The given filter'),c(1:4)
disp('Express SUM(ct)=1, SUM(c(n)*ct(n-2*k))=delta(k)');
disp('and the zero moment SUM((-1)^n*ct(n))=0 viz. H(pi)=0');

C = [c(1) c(2) c(3) c(4)
     0    0    c(1) c(2)
     c(3) c(4) 0    0
     1    -1   1    -1]

b = [2 0 0 0]';
ct = C\b;
disp('Right-hand side'), bT = b'
disp('Solution'), btT = ct'
