%This demonstrates how bank digits scaled by an unknown factor can be classified
%using logarithmic spatial scaling to transform the problem to an unknown shift
%and then using cross-correlation. No noise, to make easier to follow.
clear;load bank.mat;
%Each digit XX(:,:,k) is a 30X18 portion of X. (XX is a 3D array):
%XX(:,:,k) represents digit k. (XX(:,:,10) represents 0). Digit to be classified is 3:
XX(:,:,1)=X(:,17:34);XX(:,:,2)=X(:,50:67);XX(:,:,3)=X(:,78:95);
XX(:,:,4)=X(:,111:128);XX(:,:,5)=X(:,140:157);XX(:,:,6)=X(:,170:187);
XX(:,:,7)=X(:,202:219);XX(:,:,8)=X(:,232:249);XX(:,:,9)=X(:,262:279);
XX(:,:,10)=X(:,292:309);XX1(4:33,3:20,:)=XX;Z1=XX1(1:2:33,1:2:17,3);
%XX1(:,:,k) is digit k. %Z1 is digit 3 decimated (downsampled) by 2.
%Now transform to logarithmic spatial coordinates (base 2).
%YY(:,:,k) is XX1(:,:,k) (digit k) in logarithmic spatial coordinates:
%Z2 is Z1 (digit 3 decimated by 2 in logarithmic spatial coordinates:
YY=XX1([2.^[0:5]+1],[2.^[0:4]+1],:);Z2=Z1([2.^[0:4]+1],[2.^[0:3]+1]);
figure,subplot(2,2,1),imagesc(XX1(:,:,3)),colormap(gray),title('Digit 3, unscaled (note axes)')
subplot(2,2,2),imagesc(Z1),colormap(gray),title('Digit 3, scaled by 1/2')
subplot(2,2,3),imagesc(YY(:,:,3)),colormap(gray),title('Digit 3 in logarithmic coordinates')
subplot(2,2,4),imagesc(Z2),colormap(gray),...
title('Digit 3, scaled by 1/2, in log coordinates')
%These are identical except for a relative shift of [1,1].
%Now see which dugut 0...9 the scaled by 1/2 digit in Z1 represents by cross-correlating
%Z1 in logarithmic coordinates (Z2) to each digit 0....9 in logarithmic coordinates (YY(:,:,k)).
for k=1:10;Y2=YY(:,:,k);R(k)=2*max(max(conv2(Z2,fliplr(flipud(Y2)))))-sum(sum(Y2.^2));end;
figure,subplot(211),stem([0:9],[R(10) R(1:9)]),...
title('\Lambda[k] for classifying scaled digits in log coordinates (shown below)')
for k=1:10;subplot(4,5,k+10),imagesc(YY(:,:,k)),colormap(gray),axis off,end;

