% Basis vector example, from Shapoir -Stockman p 182

% Roberts Basis

W1 = 1/2*[
1 1 
  1 1];

W2 = 1/sqrt(2)*[
0 1
-1 0];

W3 = 1/sqrt(2)*[
1 0
  0 -1];

W4 = 1/2*[
-1 1
   1 -1];

% Test regions

% Constant:

CR = [
5 5 
  5 5];

% Coefficients:

c1 = sum(sum(W1.*CR))
% c1 = 10

c2 = sum(sum(W2.*CR))
% c2 = 0

% Do same for W3 and W4

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% Better approach using matix algebra:

% Construct basis matrix

B = [
W1(:)'
W2(:)'
W3(:)'
W4(:)']

% Flatten the test area:

CRf = CR(:)

% Compute coordinate change:

NewCoord = B*CRf

% NewCoord =
%    10
%     0
%     0
%     0

% The other tests:

% Step edge:

SE = [
-1 1
-1 1]

SEf = SE(:)

NewCoordSE = B*SEf

% NewCoordSE =
%         0
%    1.4142
%   -1.4142
%         0

% Verify that we can transform back:

SEtestf = B'*NewCoordSE

SEtest = reshape(SEtestf, 2, 2)
% SEtest =
%   -1.0000    1.0000
%   -1.0000    1.0000

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% Sine function:

t = 0:pi/50:10*pi
I = sin(t)
subplot(221)
plot(t,I)
subplot(222)
image(64*(I+1)/2)
colormap gray