clear all
close all
load TRAINLPC
load TESTLPC
s=[];
for i=1:1:3
    s=[s size(TRAINLPC{i},1)];
end
mx=min(s);
DATA=[];
MEAN=[];
for i=1:1:3
    DATA{i}=[TRAINLPC{i}(1:1:mx,:) TESTLPC{i}(1:1:mx,:) ];
    MEAN=[MEAN mean(DATA{i}')'];
end
% function [E,E2,DATAPI,DATANSP]=ldaforspeech(DATA)
%DATA is the cell, with each cell belongs to the individual cluster
%Computation of within-class scatter matrix
SW=0;
for i=1:1:3
    SW=SW+cov(DATA{i}');
end
MEAN=[];
for i=1:1:3
    MEAN=[MEAN mean(DATA{i}')'];
end
%projection using LDA bases using pseudo inverse
SB=cov(MEAN');
[E,D]=eig(pinv(SW)*SB);
[P,Q]=find(D>0.01);
E=E(:,P(length(Q)):-1:P(1));
DATAPI=E'*cell2mat(DATA);
% figure
% subplot(2,2,1)
% plot(DATAPI(1,1:1:10),zeros(1,length(DATAPI(1,1:1:10))),'r*')
% hold on
% plot(DATAPI(1,11:1:20),zeros(1,length(DATAPI(1,11:1:20))),'gs')
% plot(DATAPI(1,21:1:30),zeros(1,length(DATAPI(1,21:1:30))),'bd')
% subplot(2,2,2)
% plot(DATAPI(1,1:1:10),DATAPI(2,1:1:10),'r*')
% hold on
% plot(DATAPI(1,11:1:20),DATAPI(2,11:1:20),'gs')
% plot(DATAPI(1,21:1:30),DATAPI(2,21:1:30),'bd')
%projection using LDA bases using Null-space LDA
[E1,D1]=eig(SW);
[P1,Q1]=find(D>0.01);
DATA1=cell2mat(DATA);
DATAN=DATA1-E1(P1(length(Q)):-1:P1(1))*E1(P(length(Q)):-1:P1(1))'*DATA1;
M=[mean(DATAN(:,1:1:10)')'  mean(DATAN(:,11:1:20)')'  mean(DATAN(:,1:21:30)')' ];
SBB=cov(M');
[E2,D2]=eig(SBB);
[P2,Q2]=find(D2>0.01)
E2=E2(:,P2(length(Q)):-1:P2(1));
DATANSP=E2'*cell2mat(DATA);
% subplot(2,2,3)
% plot(DATANSP(1,1:1:10),zeros(1,length(DATANSP(1,1:1:10))),'r*')
% hold on
% plot(DATANSP(1,11:1:20),zeros(1,length(DATANSP(1,11:1:20))),'gs')
% plot(DATANSP(1,21:1:30),zeros(1,length(DATANSP(1,21:1:30))),'bd')
% subplot(2,2,4)
% plot(DATANSP(1,1:1:10),DATANSP(2,1:1:10),'r*')
% hold on
% plot(DATANSP(1,11:1:20),DATANSP(2,11:1:20),'gs')
% plot(DATANSP(1,21:1:30),DATANSP(2,21:1:30),'bd')


