% shift_f0.m

close all

% field and timimg parameters

nsteps = 100;
field_size = 100;
nplots = 10;
doplot = find(~rem([1:nsteps],nsteps/nplots));
ntones = 4;
t.targ = [80 65 50 35];
t.amp = [5 5 5 5];
intend_targ = 35;
nshift = -6;


% create object sim by constructor call
sim = Simulator();

% add fields

sim.addElement(NeuralField('field tone', field_size, 15, -5, 4));


% add interactions
% 
sim.addElement(LateralInteractionsShifted1D('tone > tone', 100, 5, 15, 40, 25, 0, nshift,0), ...
  'field tone', 'output', 'field tone', []);
% sim.addElement(LateralInteractions1D('tone > tone', 100, 5, 15, 40, 30, 0), ...
%  'field tone', 'output', 'field tone', []); %reduce outer two amplitudes
%  sim.addElement(LateralInteractions1D('tone > tone', 100, 5, 15, 20, 30, 0), ...
%   'field tone', 'output', 'field tone', []); reduce inner amplitudes

% add inputs

sim.addElement(GaussStimulus1D('T1', field_size, 5, t.amp(1), t.targ(1)), ...
  [], [], 'field tone');
sim.addElement(GaussStimulus1D('T2', field_size, 5, t.amp(2), t.targ(2)), ...
  [], [], 'field tone');
sim.addElement(GaussStimulus1D('T3', field_size, 5, t.amp(3), t.targ(3)), ...
  [], [], 'field tone');
sim.addElement(GaussStimulus1D('T4', field_size, 5, t.amp(4), t.targ(4)), ...
  [], [], 'field tone'); 
sim.addElement(GaussStimulus1D('Intent', field_size, 5, 0, intend_targ), ...
  [], [], 'field tone');



% try initialization and step to see if the architecture runs
% (this procedure is not necessary, but helpful for debugging)
sim.tryInit();
sim.tryStep();


% initialize the simulator
sim.init();

% run the simulation for nsteps

isyll = 1;
iplot = 1;

for istep = 1 : nsteps
    sim.step();


    if istep == doplot (iplot)
        figure (1)
        subplot (nplots,1,iplot), plot(sim.getComponent('field tone', 'activation'), 'b');
        ylabel ([' t = ', num2str(istep)]);
        figure (2)
        subplot (nplots,1,iplot), plot(sim.getComponent('tone > tone', 'output'), 'r');
        ylabel ([' t = ', num2str(istep)]);
        iplot = iplot +1;
    end
end

figure (1)
subplot (nplots,1,1), title ('Tone field activation', 'FontSize',18 );
figure (2)
subplot (nplots,1,1), title ('Output of Convolution with Kernel', 'FontSize',18 );
figure (3)
plot(sim.getComponent('tone > tone', 'kernel'), 'r');
title ('Interaction Kernel', 'FontSize',18 );



 htone = sim.getElement('Intent'); % get element handle
 htone.amplitude = 20; % change element parameter
 htone.init();

 figure (4)

 nstep2 = 15;
 for i = 1:nstep2
    sim.step();
    subplot (nstep2,1,i), plot(sim.getComponent('field tone', 'activation'), 'b');
    act = sim.getComponent('field tone', 'activation');

    act_plus = act;
    act_plus (act_plus < 0) = 0;
    act = act - min(act);

    act_sum = sum(act);
    act_wgt = sum (act .* [1:field_size]);

    act_plus_sum = sum(act_plus);
    act_plus_wgt = sum (act_plus .* [1:field_size]);

    cen = act_wgt./act_sum;
    cen_plus = act_plus_wgt./act_plus_sum;

    [maxval, maxp] = max (act);
    
    title (['cen = ' num2str(cen) ' cen+ = ' num2str(cen_plus)  ' maxp = ' num2str(maxp)] ,'FontWeight','bold' );
 end

        





