clc
clear


Data = [10
    10
    -10
    -10
];
delta = 10
nn = size(Data);
n=nn(1,1);
m=nn(1,2);

W = zeros(n,n);
www=zeros(n,n);
% create the norm
for i = 1:n
   for j = i:n
if i==j
www(i,j)=0;
else
for k=1:1:m
temp(1,m)= (Data(i,m)-Data(j,m))^2;
summ = summ+temp(1,m);
end
www(i,j)= sqrt(summ);
end
temp=0;
summ=0;
end
end

www
for i = 1:n
   for j = i:n
  W(i,j) = exp(-1*((www(i,j)/delta)^2));
end
end



% Weights in matrix are symmetric
for i = 1:n
    for j = 1:n
        if i>j
            W(i,j) = W(j,i);
        end
    end
end
W
% Compute the D matrix 
D = zeros(n,n);
for i = 1:n
    for j = 1:n
        D(i,i) = D(i,i) + W(i,j);
    end
end
D
% Compute P matrix 
P = inv(D)*W;
P
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
IminusL = (D^0.5)*P*(D^-0.5);
T = IminusL
% Sparse factorization provides the translations
% Columns of Q provide the orthogonal basis functions on the space V1
[Q,R] = qr(T);
Q
R
% Using the self-adjoint property of T = T* = R*Q*
T2 = R*cjt(R)
T2 = R*conj(R')

% Columns of Q1 provide the orthogonal basis functions for sub space V2
[Q1,R1] = qr(T2);
Q1
R1
% To obtain the wavelet basis functions 
% set

I = eye(n);
phi0 = I;
f=[ 0.7071       -0.0000        
   0.7071       0.0000    
   -0.0000      0.7071   
   -0.0000    0.7071   ];
A = I - f*f'

[Q0p, R0p] = qr(A);
w1 = Q0p
R0p

ff= [ 0.7071             0        
   -0.7071            0        
         0          0.7071   
         0           -0.7071    ];
     
   
     % already downsampled so divide by 2 not needed c=c2
     c=(Data'*f)
c2=(Data'*f)
d=(Data'*ff)

x=f*c'
xx=f*c2' +ff*d'




%******************************************
% not downsampled so divide by 2 cc/2= cc2

%basis functions in full (no downsampling) can be applied on any data of similar length in row (not necessary in column)  (in our case it is the
%V- value function)

Data = [258
  -678
    -24
    -4563
];
[Qw,Rw] = qr(phi0 -( Q*Q)); % transpose not needed becuase Q'= inv(Q) and QQ' will be = I
Wv = Qw
    cc=(Data'*Q)
cc2=(Data'*Q)/2
dd=(Data'*Wv)/2

y=Q*cc'
yy=Q*cc2' +Wv*dd'
% it is enough to use scaling and wavelet is not needed if the function is
% applied on static data. However, in a dynamic environment use of both scaling wavelet
% function is useful.


