%% Sricpt for the project work for the course principles of neural computing.
% Written by Sampsa Laine. 24th of January 2001.
% modified by Matti Aksela, 19.5.2006

% first, make sure that we have the som-toolbox version 2
% 
% Please note that if you are using the som toolbox on your home 
% computer, you might experience problems with som_use_vs2 - in the 
% case that you have downloaded the newest version of the som toolbox 
% (2.0) the command is entirely unneccessary and can be commented out 
% if it is causing any probelms. This is only neccessary if also an older
% version of the toolbox exists on the system.
%
if exist('som_use_vs2')==2
  som_use_vs2;
end

%% Variables to be modified by the user
SOMwidth = 12;
RoughFin = 2;
FinetuneFin = 1;

%% Create a batch of test data
data = [randn(300,1)+0,randn(300,1)+0;...
	randn(300,1)+10,randn(300,1)+0;...
	randn(300,1)+0,randn(300,1)+10;];
% scale the data
sData = som_data_struct(data);
sDataN = som_normalize(sData, 'var');

%% train the SOM
sMapN = som_map_struct(size(data,2), 'msize', [SOMwidth, 12]); 
sMapN = som_randinit(sDataN, sMapN);

% run the rough training phase
figure(1)
sTrainN = som_train_struct(sMapN,'phase','rough');
sMapN = som_batchtrain(sMapN,sDataN,sTrainN,'radius_fin', RoughFin, ...
		       'tracking',3);

% run the fine tuning phase
sTrainN = som_train_struct(sMapN,'phase','finetune');
sMapN = som_batchtrain(sMapN,sDataN,sTrainN,'radius_fin', FinetuneFin, ...
		       'tracking',3);

% The training could be done in one step using som_make.
% The som_make determines the training parameters automatically
% from data.
% sMapN = som_make(sDataN);

%% visualize the result using the U-matrix and the 
figure(2)
som_show(sMapN, 'norm', 'd');
