% COURSE 02457 EXERCISE 10 
% MINIPROJECT IN SPEECH PROCESSING
% Lars Kai Hansen (c) 2001
%
%
clear,
% Initialization
N = 320;       %Windos length
deltaN = 80;   % Window spacing
M = 12;        % LPC coeeficients
Q = 12;        % Cepstral coefficients
% load letter database
load letters
% extract training letters
s1=letters.s1;   % extract letter s
o1=letters.o1;   % extract letter o
f1=letters.f2;   % extract letter f
y_s1= hmmfeatures(s1,N,deltaN,M,Q);
y_o1= hmmfeatures(o1,N,deltaN,M,Q);
y_f1= hmmfeatures(f1,N,deltaN,M,Q);

%extract test letters
s2=letters.s2;
o2=letters.o2;
f2=letters.f2;
y_s2= hmmfeatures(s2,N,deltaN,M,Q);
y_o2= hmmfeatures(o2,N,deltaN,M,Q);
y_f2= hmmfeatures(f2,N,deltaN,M,Q);
Ns1=size(y_s1,2);
No1=size(y_o1,2);
Nf1=size(y_f1,2);
Ns2=size(y_s2,2);
No2=size(y_o2,2);
Nf2=size(y_f2,2);


%train pca
x1=[y_s1';y_o1';y_f1']';
xmean1=mean(x1,2);
z1=x1-repmat(xmean1,1,No1+Ns1+Nf1);
scale1=std(z1,0,2);
z1=diag(1./scale1)*z1;
x2=[y_s2';y_o2';y_f2']';
xmean2=mean(x2,2);
z2=x2-repmat(xmean2,1,No2+Ns2+Nf2);
scale2=std(z2,0,2);
z2=diag(1./scale2)*z2;
fh=figure(1),
temp = get(fh,'position');
temp = [1 300 400 600];
set(fh,'position',temp);  

hold off
subplot(2,1,1), imagesc(z1),title('TRAINING DATA'),colormap('gray')
subplot(2,1,2), imagesc(z2),title('TEST DATA'),colormap('gray')

% Project the features
[U,D,V]=svd(z1);
p11=U(:,1)'*z1;
p21=U(:,2)'*z1;
p12=U(:,1)'*z2;
p22=U(:,2)'*z2;

figure(2),
fh=figure(2),
temp = get(fh,'position');
temp = [310 300 610 600];
set(fh,'position',temp);  

subplot(2,1,1),title('TRAINING DATA')
for j=1:Ns1,
   text(p11(j),p21(j),'s'),
   hold on
end
for j=1:No1,
   text(p11((Ns1+j)),p21((Ns1+j)),'o')
   hold on
end
for j=1:Nf1,
   text(p11((Ns1+No1+j)),p21((Ns1+No1+j)),'f')
   hold on
end
plot(p11,p21,'b.')

subplot(2,1,2), title('TEST DATA')
for j=1:Ns2,
   text(p12(j),p22(j),'s'),
   hold on
end
for j=1:No2,
   text(p12((Ns2+j)),p22((Ns2+j)),'o')
   hold on
end
for j=1:Nf2,
   text(p12((Ns2+No2+j)),p22((Ns2+No2+j)),'f')
   hold on
end
plot(p12,p22,'g.')