%%
%%%% DON"T USE is ROB's MAGIC STUFF
restoredefaultpath; matlabrc 
clear all
clc
%%close all
warning off all

disp('Startup promet stuff');

addpath( ...
    genpath(fullfile('/Volumes/','VOLUME2_MAC_INT_DH/D_DRIVE/CURRENT_WORK/PROMET_2009_MATLAB/')), ...
	genpath(fullfile('/Applications/','MATLAB_R2009a.app/toolbox/nnet/')));

	

cd '/Volumes/VOLUME2_MAC_INT_DH/D_DRIVE/CURRENT_WORK/PROMET_2009_MATLAB'
%%

%%%%% NETWORK PARAMETERS %%%%%


	 
% 	 %coclea
% 	 Gain_freq = 1.0;
% 	 Exp_freq  = 40.0;
% 	 Sig_freq  = 4;
% 	 nstimuli=10;
% 	 Freq_min=100;
% 	 Freq_max=10000;
% 	 
% 	 %eye pos motor neuronen
% 	 Nmsc=100;
% 	 Gain_ocul = 1/400;
% 	 Gain_pos = 7/10;
% 	 
% 	 %teacher
% 	 Gain_motor_error     = 0.7;
% 	 mot_sigma            = 10;
% 	 Motor_error_range    = 100;
	 
     %INPUT / OUTPUT Ranges
	 Azi_range = 60;        % sound position                         [-60 60]
	 Int_range = [0 4];     % sound intensity range                  [1.0 2.0]
	 Eye_range = 40;        % eye position                           [-40 40]
	 Mot_range = 100;       % motor error                            [-100 100]
	 Num_freq  = 5;         % number of frequency components         5
	 
%%%%% GENERAL INPUT: azi / Is  / Hor / Motor_error = azi-hor
%%%%% GENERAL INPUT: azi / Is  / Hor / Motor_error = azi-hor
%%%%% GENERAL INPUT: azi / Is  / Hor / Motor_error = azi-hor

     % VECTOR sizes
     Nfreq	 = 30; %coclea
	 Naud	 = 4;  % first hidden binaural
	 Nhidden = 10; %second hidden audiomotor
	 Nmsc	 = 10; % eye pos
	 Nmot	 = 20; % motor error teacher
	 
	
	 N=250
	 p=zeros(80,N);
	 t=zeros(20,N);
	 
tic
for i=1:N
%[eyepos]=eye_pos__input_V4(Nmsc,hor)

hor = randi([-Eye_range , Eye_range], 1 ,1);
[eyepos]  = eye_pos_input_V4(Nmsc, hor);

%[cochlea]=coch_input_V4(Nfreq, azi, nstimuli, Is, Freq_flag)
azi = randi( [-Azi_range , Azi_range], 1, 1);
[cochlea] = coch_input_V4(Nfreq, azi, randi(Num_freq,1,1),(randi([Int_range(1), Int_range(2)],1,1))/10+1, 0);

motor_error= azi - hor;
%[motmap]=mot_input_V4(Nmot,Motor_error,Azi_range,Eye_range)
[motmap]=mot_input_V4(Nmot,motor_error,Azi_range,Eye_range);

coch_input=reshape(cochlea', numel(cochlea), 1);
eyepos_input=reshape(eyepos', numel(eyepos), 1);
motmap_ouput=motmap';

p(:,i)=[coch_input ; eyepos_input];
t(:,i)=[motmap_ouput];
end
toc

figure(1)
subplot(221)
     hold off
     plot((cochlea(1,:)),'k');
	 hold on
	 plot((cochlea(2,:)),'r');

	 
subplot(223)
     hold off
     plot((eyepos(1,:)),'k');
	 hold on
	 plot((eyepos(2,:)),'r');
	 axis([1 Nmsc 0 .8])
	 		
subplot(122)
x=linspace(-100,100,Nmot);
plot(x,motmap)	




	 
% %%
% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% %--   Train Neuronal Network   --%
% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% 
%     %%%% normalizing data
%      [pn,stdp] = mapstd(p);
%      [tn,stdt] = mapstd(t);
%  
%         %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%         %--  Create Custom Neuronal Network   --%
%         %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% 
%         promet_netv2
% 
% 
%      %%%%%  MAKE SURE TRAINING REACHES
%      %%%%%  MAX MU  BASIAN REGULARIZATION
%             net.trainParam.epochs = 5000;
%             randn('seed',192736547);
%             net = init(net);
%%

 %%%% normalizing data
     [pn,stdp] = mapstd(p);
     [tn,stdt] = mapstd(t);


net = newff(pn,tn,[4 10 20],{'purelin','purelin', 'purelin'},'trainbr')

promet_netv3
 
net.trainFcn='trainrp'; %%%LEARNS VER fast WITH LARGE DATA set
net.trainFcn='trainbr'; %%%LEARNS VER good WITH small DATA set


randn('seed',192736547);
net = init(net);

net.trainParam.epochs = 5000;
 

    %-- Train the network --%
    [net,tr] = train(net,pn,tn);
    
    %-- Simulate the network --%
    outn = sim(net,pn);
    
    %-- convert the network output back into the original units
    out=mapstd('reverse',outn,stdt);


%%
%%%%% LEARNED RESPOSE (OUT) compared to the teacher (t)
figure(5)
for i=1:1:N
subplot(211)
hold off
plot(t(:,i),'b')
hold on
plot(out(:,i),'r')
pause(0.1)
	i
	
subplot(212)
hold off
plot(t(:,i),out(:,i),'x')
axis([0 1 0 1])
axis square
end

%% WEIGHT ANALYSIS COCH TO 1st HIDDEN

audw=net.IW{1,1}';

figure(4)
clf
for i=1:1:Naud
subplot(2,2,i)
hold off
plot(audw(1:end/2,i),'-bx')
hold on
plot(audw(end/2+1:end,i),'-ro')
axis([1 30 -10 +10])

sum(audw(1:end/2,i)-audw(end/2+1:end,i))

end

%% WEIGHT ANALYSIS 1st hidden 2 nd hidden

hhw=net.LW{2,1}';

figure(5)
clf
for i=1:1:Nhidden
subplot(5,2,i)
% hold off
% plot(eyew(1:end/2,i),'-bx')
% hold on
plot(hhw(:,i),'-ro')
axis([1 4 -10 +10])
end



%% WEIGHT ANALYSIS EYEpos TO 2st HIDDEN

eyew=net.LW{3,2};

figure(6)
clf
for i=1:1:Nhidden
subplot(2,5,i)
hold off
plot(eyew(1:end/2,i),'-bx')
hold on
plot(eyew(end/2+1:end,i),'-ro')
axis([1 10 -10 +10])
end



%%
% DIMENSION_FREQMAP: 30.0;
% DIMENSION_AUDMAP: 4.0;
% DIMENSION_EYEPOS: 10.0;
% DIMENSION_HIDDEN: 10.0;
% DIMENSION_OUTPUT: 20.0;
% 
% SIGMA_FREQMAP: 0.5;
% SIGMA_MOTMAP: 10.0;
% 
% GAIN_FREQMAP: 1;
% EXP_FREQMAP: 40;
% 
% GAIN_EYEPOS: 1;
% GAIN_MOTOR: .7;
% 
% BIAS_HID: 5;
% 
% LEARNING_RATE: 0.5;
% 
% AZIMUTH: 0.0;
% EYEPOSITION: 0.0;
% FREQUENCY: 4950.0;
% INTENSITY: 0.0;
% 
% RANDOM_FLAG: 1;
% CONTINUE_FLAG: 0;
% 
% MOTORERROR_FLAG: 0;
% AZIMUTH_FLAG: 0;
% EYE_FLAG: 0;
% FREQUENCY_FLAG: 0;
% INTENSITY_FLAG: 0;
% 
% DEMO_FLAG: 1000;
% DEMO_WAIT: 100;
% STORE_FLAG: 1;
% STORE: 0;
% 
% WEIGHTS_RANGE: .5;
% 
% AZIMUTH_RANGE: 60.0;
% EYEPOS_RANGE: 40.0;
% MOTORERROR_RANGE: 100.0:
% LOW_FREQUENCY_RANGE: 100.0;
% HIGH_FREQUENCY_RANGE: 10000.0;
% INTENSITY_RANGE: 0.40;
% 
% #V
% 1/1.wht;
% 2/2.wht;
% 3/3.wht;
% 4/4.wht;
% 5/4.wht;
% #E
