%This pregram is to perform rbf on the vowel data
%Again, the data should be trained before doing classification

clear;
close all;
start=cputime;
%Load the training data

load initial;

%Then, create a rbf with the following parameters

n_input=2;                  %input dimension is 2

c_per_class=2;              %Set center per class
n_hidden=c_per_class*10;    % totally 10 classes
n_output=10;                %output is 10 classes

%The name of the rbf is 'net'

net=rbf(n_input, n_hidden, n_output, 'gaussian');

%Now, create the option vector

options= foptions;
options(1)=1;   %display error values
options(14) = 100;	% number of maximum iterations of EM

%now, put our training data point into a matrix
in_data=[];
in_data(:,1)=data(:,1);
in_data(:,2)=data(:,2);

%Absorb the classes information into target vector t

t=[];
for i=3:12
    t(:,i-2)=data(:,i);
end

%Set up the rbf by our training data (the name of our training data was set
%to data)
net = rbfsetbf(net, options,in_data ) ;

%use 2 stage train to train the network
net = rbftrain(net, options, in_data, t); 

output=[];
ind=[];
ind2=[];
in_corr=0;
for i=1:338

    output(i,:)=rbffwd(net,in_data(i,:));
    [val,ind(i,1)]=max(output(i,:));
    [val2,ind2(i,1)]=max(t(i,:));
    if ind(i,1)~=ind2(i,1)
        in_corr=in_corr+1;
    end

end

classification_rate=0;

classification_rate=(338-in_corr)./338*100


import_gmm
figure(1),plot(import_class1(:, 1), import_class1(:, 2), 'ro' ,import_class2(:, 1), import_class2(:, 2), 'go', import_class3(:, 1), import_class3(:, 2), 'bo' , import_class4(:, 1), import_class4(:, 2), 'mo' , import_class5(:, 1), import_class5(:, 2), 'co' , import_class6(:, 1), import_class6(:, 2), 'bs' , import_class7(:, 1), import_class7(:, 2), 'cs' , import_class8(:, 1), import_class8(:, 2), 'rs' ,  import_class9(:, 1), import_class9(:, 2), 'ms' , import_class10(:, 1), import_class10(:, 2), 'gs','MarkerSize',5);
hold on;
plot(net.c(:,1),net.c(:,2),'kx','MarkerSize',10);
legend('class 1','class 2', 'class 3', 'class 4', 'class 5', 'class 6', 'class 7', 'class 8', 'class 9', 'class 10','Centres',-1);
title('The training data');

%Now, start to plot the decision boundary by find the class difference
%First, identify the class each points belong to

i=1;
j=0;
classify=[];



%Set the checking interval
interval=20;

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
for x=1:interval:1200
    j=j+1;
    i=1;
    for y=1:interval:4000
        classify(i,j,:)=rbffwd(net,[x,y]);
        [val,ind3(i,j)]=max(classify(i,j,:));
        %classified_result(k,:,ind(i,j))=[x,y];
        %k=k+1;
        i=i+1;
        
    end
end

figure(2)
colors=['ro';'go';'bo';'mo';'co';'bs';'cs';'rs';'ms';'gs'];
[i,j]=size(ind3);

for x=1:j
    for y=1:i
        plot(x.*interval,y.*interval,colors(ind3(y,x),:));
        hold on;
    end
end
legend('class 1','class 2', 'class 3', 'class 4', 'class 5', 'class 6', 'class 7', 'class 8', 'class 9', 'class 10',-1);
title('The Classification');
hold off;


%plot the decision boundary

ind_now=0;
k=1;
boundary_row=[];
boundary_col=[];
[row_no,col_no]=size(ind3);
for i=1:row_no-1
     ind_now=ind3(i,1);
    for j=1:col_no-1
        %Check if the 2 neighbour point belongs to the same clss
        ind_previous=ind_now;
        ind_now=ind3(i,j);
        if ind_now~=ind_previous
            %record the area with class changing
            boundary_row(k,:)=[(i-1)*interval+1,((((j-1)*interval+1))+((((j-1)*interval+1))-interval))./2];
            k=k+1;
        end
    end
end

k=1;
for j=1:col_no-1
    ind_now=ind3(1,j);
for i=1:row_no-1
    
        %Check if the 2 neighbour point belongs to the same clss
        ind_previous=ind_now;
        ind_now=ind3(i,j);
        if ind_now~=ind_previous
            %record the area with class changing
            boundary_col(k,:)=[((((i-1)*interval+1))+((((i-1)*interval+1))-interval))./2,(j-1)*interval+1];
            k=k+1;
        end
    end
end


%plot the boundary

figure(3),plot(import_class1(:, 1), import_class1(:, 2), 'ro' ,import_class2(:, 1), import_class2(:, 2), 'go', import_class3(:, 1), import_class3(:, 2), 'bo' , import_class4(:, 1), import_class4(:, 2), 'mo' , import_class5(:, 1), import_class5(:, 2), 'co' , import_class6(:, 1), import_class6(:, 2), 'bs' , import_class7(:, 1), import_class7(:, 2), 'cs' , import_class8(:, 1), import_class8(:, 2), 'rs' ,  import_class9(:, 1), import_class9(:, 2), 'ms' , import_class10(:, 1), import_class10(:, 2), 'gs','MarkerSize',5)
hold on
plot(abs(boundary_col(:,2)-1),boundary_col(:,1),'.b',boundary_row(:,2),boundary_row(:,1),'.k','MarkerSize',15)
legend('Class 1', 'Class 2'  ,'Class 3','Class 4','Class 5','Class 6','Class 7','Class 8','Class 9','Class 10','The Boundary', -1)
title('The Decision Boundary');
hold off

time=cputime-start;
