%% Bemba 

%% Parameters for subject right dislocation L%, and PRC (figure 5)


Hrel = 30; % High tone F0neut + 30
Lrel = -30; % Low tone F0neut - 30
NeuF0(1) = 150; %initial neutralF0

nmora = 17; %number of moras

LexH= [1 3 10]; %Mora number with lexical H tone
SurfH = [1 2 3 4 8 12 15 17]; % Mora number with surface H tone
DownH = []; % Mora number with downstepped H tone

%Wn =  [6 9 13];
%PPn = [6 9 13];
%IPn = [9 13];

blend_alpha_initial = 0.6; %initial weight for lexical tones
blend_target = 0.3; %target weight for lexical tones


k  = 0.2; %stiffness for blending parameters and  declination

L_1 = 120; % target F0 for declination
%L_compre = 120; % L boundary tone, which blends with lexical tones
L_1mora = 8;  % Mora number where L boundary tone starts to exert influences
%L_compre_mora = 13; % Mora number where PRC starts to exert influences
 %% tonal processes (rule-based approach/ not working at the moment)
% for i = 1:nmora
%    if ismember(i,LexH)  
%        for j = 1:length(Wn)
%            
%       curr_word = find(Wn)
%       Next_LexH = LexH(find(LexH==i)+1);
%       if Next_lexH 
%    bounded spreading
%    if ismember(i, LexH)
%    SurfH = [SurfH i i+1];  % High doubling
%     if  ismember(i+2, LexH)
%     DownH = [DownH i+2];
%     else
%     SurfH = [SurfH i+2];  % Secondary High doubling
%     end
%    end
% end

%% Intonation Figure 5 
figure;


for i = 1:nmora  
    if i == 1
       if ismember(i, SurfH);  
       T(i) = NeuF0(i) + Hrel;
       else 
       T(i) = NeuF0(i) + Lrel;
       end
    elseif 1< i && i < L_1mora
       NeuF0(i) = NeuF0(i-1); % not affected by L% and no declination
       if ismember(i, SurfH) & ~ismember(i,DownH); 
       T(i) = NeuF0(i) + Hrel;
       elseif ismember(i,DownH);
       T(i) = NeuF0(i) + Hrel-50;
       else 
       T(i) = NeuF0(i)  +Lrel; 
       end
    elseif L_1mora<= i 
        dT = -k*NeuF0(i-1) + k*L_1 ; 
        NeuF0(i) = NeuF0(i-1) + dT; %Neutral F0 is attracted towards the boundary tone
         
        blend_alpha(L_1mora-1) =  blend_alpha_initial;
        blend_alpha(i)=  blend_alpha(i-1) -k*blend_alpha(i-1) + k*blend_target; %increasing blending
        
        if ismember(i, SurfH) & ~ismember(i,DownH);
        T(i) = blend_alpha(i)*(NeuF0(i) + Hrel) + (1-blend_alpha(i))*L_1;
        elseif ismember(i,DownH);
        T(i) = blend_alpha(i)*(NeuF0(i) + Hrel-50) + (1-blend_alpha(i))*L_1;
        else 
        T(i) = blend_alpha(i)*(NeuF0(i) + Lrel) + (1-blend_alpha(i))*L_1;
        end
% the following code is not needed if we assume L_1 and L_compre
%   merge
%   elseif  i > L_compre_mora . 
%          dT = -k*NeuF0(i-1) + k*L_1 ; 
%          NeuF0(i) = NeuF0(i-1) + dT 
%           
%          blend_alpha(L_compre_mora) =  blend_alpha_initial;
%          blend_alpha(i)=  blend_alpha(i-1) -k*blend_alpha(i-1) + k*blend_target
%          
%         if ismember(i, SurfH) & ~ismember(i,DownH);            
%         T(i) = blend_alpha(i)*(NeuF0(i) + Hrel) + (1-blend_alpha(i))*L_compre;
%         elseif ismember(i,DownH);
%         T(i) = blend_alpha(i)*(NeuF0(i) + Hrel-50) + (1-blend_alpha(i))*L_compre;
%         else 
%         T(i) = blend_alpha(i)*(NeuF0(i) + Lrel) + (1-blend_alpha(i))*L_compre;
%         end
     end
end

plot (1:nmora,NeuF0, '-o', 'LineWidth' ,2);
ylabel('F0'); xlabel('Syllable');
hold on;
plot (1:nmora,T, '-o', 'LineWidth' ,2);
ylabel('F0'); xlabel('Syllable');
axis ([1 17 75 225]);
title('Figure 5')
legend('Neutral F0+ attractive forces of boundary tones','afterding adding blending',...
    'location','SouthOutside','Orientation','horizontal','NumColumns',2);


%% Parameters for object right dislocation with downstep, L%, and PRC (figure 6)

clear;

Hrel = 50;
Lrel = -30;
NeuF0(1) = 150; %initial neutralF0

nmora = 13;
LexH= [1 3 10];
SurfH = [1 2 3 4 5 6 7 8 9 11 12 13];
DownH = [3 4 5 6 7 8 9 11 12 13];

blend_alpha_initial = 0.6; %initial weight for lexical tones
blend_target = 0.3; %target weight for lexical tones

L_1 = 120;
k  = 0.2; %stiff
L_1mora = 7;  % Mora number where L% boundary tone starts to exert influences
L_Cmora = 10; % Mora number where PRC starts to exert influences

%% Intonation Figure 6
figure;


for i = 1:nmora  
    if i == 1
       if ismember(i, SurfH);  
       T(i) = NeuF0(i) + Hrel;
       else 
       T(i) = NeuF0(i) + Lrel;
       end
    elseif 1< i && i < L_1mora
       NeuF0(i) = NeuF0(i-1); % not affected by L% and no declination
       if ismember(i, SurfH) & ~ismember(i,DownH); 
       T(i) = NeuF0(i) + Hrel;
       elseif ismember(i,DownH);
       T(i) = NeuF0(i) + Hrel-50;
       else 
       T(i) = NeuF0(i)  +Lrel; 
       end
    elseif L_1mora<= i 
        dT = -k*NeuF0(i-1) + k*L_1 ; 
        NeuF0(i) = NeuF0(i-1) + dT; %Neutral F0 is attracted towards the boundary tone
         
        blend_alpha(L_1mora-1) =  blend_alpha_initial;
        blend_alpha(i)=  blend_alpha(i-1) -k*blend_alpha(i-1) + k*blend_target; %increasing blending
        
        if ismember(i, SurfH) & ~ismember(i,DownH);
        T(i) = blend_alpha(i)*(NeuF0(i) + Hrel) + (1-blend_alpha(i))*L_1;
        elseif ismember(i,DownH);
        T(i) = blend_alpha(i)*(NeuF0(i) + Hrel-50) + (1-blend_alpha(i))*L_1;
        else 
        T(i) = blend_alpha(i)*(NeuF0(i) + Lrel) + (1-blend_alpha(i))*L_1;
        end
     end
end

plot (1:nmora,NeuF0, '-o', 'LineWidth' ,2);
ylabel('F0'); xlabel('Syllable');
hold on;
plot (1:nmora,T, '-o', 'LineWidth' ,2);
ylabel('F0'); xlabel('Syllable');
axis ([1 13 75 225])
title('Figure 5')
legend('Neutral F0+ attractive forces of boundary tones','after adding blending',...
    'location','SouthOutside','Orientation','horizontal','NumColumns',2);
