%%
%%%% 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=10000
	 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)	



%%
%%%%NEURALNET INPUT

% coch_input=reshape(cochlea', numel(cochlea), 1);
% eyepos_input=reshape(eyepos', numel(eyepos), 1);
% motmap_ouput=motmap';
% 
% p=randi([1, 10],80,10)
% 
% t=randi([1, 10],10,10)
clc

% net = newff(p,t,[10 5],{'purelin'},'trainlm');
%      net.trainParam.show = 1;
%      net.trainParam.epochs = 200;
%      %net.trainParam.goal = 10/10^20;
%      net.trainParam.lr= .1;
%      net.trainParam.mem_reduc= 200;
%       randn('seed',192736547);
%             net = init(net);
%      pack




net = newff(p,t,[20])
net.trainParam.goal = 10/10^20;
      randn('seed',192736547);
            net = init(net);
     pack



net.trainParam.epochs = 500;
net = train(net,p,t);




%%

net.trainParam.epochs = 50;
net = train(net,P,T);
Y = sim(net,P);
figure(3)
plot(P,T,P,Y,'o');


%%


net = newff(p,t,[5 5],{'purelin','purelin'},'trainlm')
     net.trainParam.show = 1;
     net.trainParam.epochs = 1;
     %net.trainParam.goal = 10/10^20;
     net.trainParam.lr= .05;
     net.trainParam.mem_reduc= 60;
      randn('seed',192736547);
            net = init(net);
     pack
net = train(net,p,t)




	 
%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%--   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_netv2

    %-- 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

%%
% 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
