%% Parameters (figure 13 declarative)

clear;

Hrel = 20;
Lrel = -20;
NeuF0(1) = 150; %initial neutral F0

nmora = 15;
LexH= [1 3 10]; % not needed
SurfH = [3 5 6 7 8 9 10 11 12 13 14];
DownH = [8 9 10 11 12 13 14];

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

L_final = 110;
k  = 0.4; %stiff
L_subj_mora = 5;  % Mora number where L% boundary tone starts to exert influences
L_final_mora = 8;  % Mora number where L% boundary tone starts to exert influences



%% Intonation Figure 13 declarative
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_final_mora
        NeuF0(i) = NeuF0(i-1);
        if i == L_subj_mora  %L% for the subject affects only one syllable here. Replace H
        T(i) = NeuF0(i)  +Lrel;
        elseif ismember(i, SurfH) & ~ismember(i,DownH); 
        T(i) = NeuF0(i) + Hrel;
        elseif ismember(i,DownH);
        T(i) = NeuF0(i) + Hrel-10;
        else 
        T(i) = NeuF0(i)  +Lrel; 
        end
    elseif L_final_mora<= i 
        dT = -k*NeuF0(i-1) + k*L_final ; 
        NeuF0(i) = NeuF0(i-1) + dT; % Neutral F0 is attracted towards the boundary tone
         
        blend_alpha(L_final_mora-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_final;
        elseif ismember(i,DownH);
        T(i) = blend_alpha(i)*(NeuF0(i) + Hrel- 10) + (1-blend_alpha(i))*L_final;
        else 
        T(i) = blend_alpha(i)*(NeuF0(i) + Lrel) + (1-blend_alpha(i))*L_final;
        end
     end
end

plot (1:nmora,NeuF0, '-o', 'LineWidth' ,2);
hold on;
plot (1:nmora,T, '-o', 'LineWidth' ,2);
ylabel('F0'); xlabel('Mora');
axis ([1 15 75 225])
title('Declarative')
legend('Neutral F0+ attractive forces of boundary tones','after adding lexical tone target and L% blending',...
    'location','SouthOutside','Orientation','horizontal','NumColumns',2);

T_dec = T;
%% Intonation Figure 13 polar question
H_utt = 170;
SurfH = [3 5 6 7 8 9 10 11 12 13 14];
DownH = [8 9 10 11 12 13 14]; % Hs after !H are still downstepped

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_final_mora)
         dT = -k*NeuF0(i-1) + k*H_utt ; % global pitch raising H-
         NeuF0(i) = NeuF0(i-1) + dT; 
         if i == L_subj_mora
         T(i) = NeuF0(i)  +Lrel;
         elseif ismember(i, SurfH) & ~ismember(i,DownH);
         T(i) = NeuF0(i) + Hrel;
         elseif ismember(i,DownH);
         T(i) = NeuF0(i) + Hrel- 20;
         else 
         T(i) = NeuF0(i) + Lrel;
         end
    elseif L_final_mora<= i 
         dT = -k*NeuF0(i-1) + k*(L_final+H_utt)/2 ; % global pitch raising target -H blends with L%
         NeuF0(i) = NeuF0(i-1) + dT; 
         if ismember(i, SurfH) & ~ismember(i,DownH);
         T(i) = NeuF0(i) + Hrel;
         elseif ismember(i,DownH);
         T(i) = NeuF0(i) + Hrel- 20;
         else 
         T(i) = NeuF0(i) + Lrel;
         end
     end
end

figure;
plot (1:nmora,NeuF0, '-o', 'LineWidth' ,2);
ylabel('F0'); xlabel('Mora');
hold on;
plot (1:nmora,T, '-o', 'LineWidth' ,2);
axis ([1 15 75 225]);
title('Polar question');
legend('Neutral F0+ attractive forces of L% and H-','after adding lexical tone target and L% blending',...
    'location','SouthOutside','Orientation','horizontal','NumColumns',2);

T_polar = T;

figure;
plot (1:nmora,T_polar, '-o', 'LineWidth' ,2);
ylabel('F0'); xlabel('Syllable');
hold on;
plot (1:nmora,T_dec, '-o', 'LineWidth' ,2);
axis ([1 15 75 225])
title('Figure 13')
legend('polar question','declarative',...
    'location','SouthOutside','Orientation','horizontal','NumColumns',2);
