% Mambila downdrift
%
% Louis Goldstein
% 14 March 2020

% Model the results in Table 1 of Connell's article

% Idea:
% Over time, the internal target F0 of all four tones show decrease
% in F0, under the control of a 1st order linear declination system
% for each tone.
%
% The tone targets also repel each other with a force that is inversely
% propotional to the current f0 difference beteween the two tones.
%
% Individual speakers differ in the overall scaling of the repulse force
% (r)
% Set r = 20 for S1-S3, r = 10 for S4-S5


clear 

ntones = 4;

% Set the initial f0 value of each tone
T(1,1) = 200;
T(2,1) = 180;
T(3,1) = 160;
T(4,1) = 140;

%Set the goal value for the declination system for each tone
for j = 1:ntones
    Tgoal(j) = T(j,1) - 15;
end


F = zeros(ntones, ntones); % F is the matrix of inter-tone repulsive forces

k  = .1;    % stiffness of the declination systems
r = 20;     % scaling of the repulsive forces

nsyll = 10;

for i = 2:nsyll 
     for j = 1:ntones 
         for l = 1:ntones
            if ~(j==l)
                F(j,l) = r./(T(j,i-1) - T(l,i-1)); % repulsive forces
            end
         end
            push = sum(F,1); % push total amount of force on a given tone from all the others
            dT = -k*T(j, i-1) + k*Tgoal(j) - push(j);  % dynamics       
            T(j,i) = T(j, i-1) + dT;
     end 
    
    end

syll = 1:nsyll;
plot (syll, T, '-o');



    
