clear; close all;

% all_3 - f0, occlusion deletion

%setup
M=3;
type='fft'; %best result - power spectrum, not smoothed
EX='mean';
x=1; %degree of approx

Recordings = uigetdir; %gets directory
Pataka = dir(fullfile(Recordings,'*.wav')); %gets all wav files in struct
Protocols = uigetdir;
Timestamps = dir(fullfile(Protocols,'*.txt'));

data = readtable('data_update.xls','PreserveVariableNames',true);
% data = readtable('data.xls','PreserveVariableNames',true);

data(:,4)=[]; % attribute 'par' deletion
data(19,:)=[]; %missing recording 
data.Gender=categorical(data.Gender);
data=cat(2,data,table(data.Age_m/12,'VariableNames',{'month_decimal'}));

% Cell storage
Moments_PA=cell([length(table2array(data(:,1))),3]);
Moments_PA=cat(2,num2cell(data.Record_nr),Moments_PA);

Moments_TA=cell([length(table2array(data(:,1))),3]);
Moments_TA=cat(2,num2cell(data.Record_nr),Moments_TA);

Moments_KA=cell([length(table2array(data(:,1))),3]);
Moments_KA=cat(2,num2cell(data.Record_nr),Moments_KA);

voicepitch=zeros(length(table2array(data(:,1))),1);

%%
for k = 1:length(table2array(data(:,1)))

RecordingName = Pataka(k).name; 
fullFileName = fullfile(Recordings, RecordingName);
fprintf(1, 'Now reading %s\n', fullFileName);
[signal, fs] = audioread(fullFileName);

ProtocolName = Timestamps(k).name; 
fullFileName2 = fullfile(Protocols, ProtocolName);
timestamp=load(fullFileName2);
if size(signal,2)==2
    signal(:,2)=[];
end

%filtr
fm = [100 4000];
[b,a] = butter(5,2*fm/fs,'bandpass');
signal_filt=filtfilt(b,a,signal);
%downsample
% M=2; % M=3 - fs=14700; M=4 - fs=11025; M=5 - fs=8820;
fs=fs/M;
signal_downsampled=downsample(signal_filt,M);

%Timestamps 
[PA, TA, KA]=timeseg2(signal_downsampled,timestamp,fs);

cycle_nr=size(timestamp, 1)/3; 
Moments_PAsys=zeros([cycle_nr,15]);
Moments_TAsys=zeros([cycle_nr,15]);
Moments_KAsys=zeros([cycle_nr,15]);
f0=zeros([length(timestamp),1]);

for j=1:cycle_nr 
        Moments_PAsys(j,1:5)= spectral_moments(PA{j, 1},fs,type);
        Moments_PAsys(j,6:10)= spectral_moments(PA{j, 2},fs,type);
        
        Moments_TAsys(j,1:5)= spectral_moments(TA{j, 1},fs,type);
        Moments_TAsys(j,6:10)= spectral_moments(TA{j, 2},fs,type);
        
        Moments_KAsys(j,1:5)= spectral_moments(KA{j, 1},fs,type);
        Moments_KAsys(j,6:10)= spectral_moments(KA{j, 2},fs,type);
        
        f0(j)=pitch(PA{j, 2},fs);
        f0(j+cycle_nr)=pitch(TA{j, 2},fs);
        f0(j+2*cycle_nr)=pitch(KA{j, 2},fs);
        
end

clear PA; clear TA; clear KA;
f0(isnan(f0))=[];

switch EX
    case 'mean'
        PAmom=mean(Moments_PAsys);
        TAmom=mean(Moments_TAsys);
        KAmom=mean(Moments_KAsys);
        f0=mean(f0);
    case 'median'
        PAmom=median(Moments_PAsys);
        TAmom=median(Moments_TAsys);
        KAmom=median(Moments_KAsys);
%     case 'trimmean'
%         PAmom=trimmean(Moments_PAsys);
%         TAmom=trimmean(Moments_TAsys);
%         KAmom=trimmean(Moments_KAsys);
end

Moments_PA{k, 2}=PAmom(1:5); Moments_PA{k, 3}=PAmom(6:10); Moments_PA{k, 4}=PAmom(11:15);
Moments_TA{k, 2}=TAmom(1:5); Moments_TA{k, 3}=TAmom(6:10); Moments_TA{k, 4}=TAmom(11:15);
Moments_KA{k, 2}=KAmom(1:5); Moments_KA{k, 3}=KAmom(6:10); Moments_KA{k, 4}=KAmom(11:15);
voicepitch(k)=f0;
end

%% grouping
boys_idxs=find(data.Gender=='M');
girls_idxs=find(data.Gender=='F');

boys=zeros(length(boys_idxs),31); % (:,1) - age, (:,2:13) - moments burst - pataka, (:,14:25) - moments vowel; slope - 6,11,16 (burst); 21,26,31 (vowel)
boys(:,1)=table2array(data(boys_idxs, 'Age_y'))+table2array(data(boys_idxs, 'month_decimal'));
boys(:,2:6)=cell2mat(Moments_PA(boys_idxs,2));
boys(:,7:11)=cell2mat(Moments_TA(boys_idxs,2));
boys(:,12:16)=cell2mat(Moments_KA(boys_idxs,2));
boys(:,17:21)=cell2mat(Moments_PA(boys_idxs,3));
boys(:,22:26)=cell2mat(Moments_TA(boys_idxs,3));
boys(:,27:31)=cell2mat(Moments_KA(boys_idxs,3));
boys(:,32)=voicepitch(boys_idxs);

girls=zeros(length(girls_idxs),25);
girls(:,1)=table2array(data(girls_idxs, 'Age_y'))+table2array(data(girls_idxs, 'month_decimal'));
girls(:,2:6)=cell2mat(Moments_PA(girls_idxs,2));
girls(:,7:11)=cell2mat(Moments_TA(girls_idxs,2));
girls(:,12:16)=cell2mat(Moments_KA(girls_idxs,2));
girls(:,17:21)=cell2mat(Moments_PA(girls_idxs,3));
girls(:,22:26)=cell2mat(Moments_TA(girls_idxs,3));
girls(:,27:31)=cell2mat(Moments_KA(girls_idxs,3));
girls(:,32)=voicepitch(girls_idxs);

% all=zeros(length(Pataka),25);
all(:,1)=table2array(data(:, 'Age_y'))+table2array(data(:, 'month_decimal'));
all(:,2:6)=cell2mat(Moments_PA(:,2));
all(:,7:11)=cell2mat(Moments_TA(:,2));
all(:,12:16)=cell2mat(Moments_KA(:,2));
all(:,17:21)=cell2mat(Moments_PA(:,3));
all(:,22:26)=cell2mat(Moments_TA(:,3));
all(:,27:31)=cell2mat(Moments_KA(:,3));
all(:,32)=voicepitch;

G1M=boys(boys(:,1)<8,:);
G2M=boys(boys(:,1)>=8 & boys(:,1)<12,:);
G3M=boys(boys(:,1)>=12,:);
G1F=girls(girls(:,1)<8,:);
G2F=girls(girls(:,1)>=8 & girls(:,1)<12,:);
G3F=girls(girls(:,1)>=12,:);

figure
subplot 321;
plot(boys(:,1),boys(:,2),'bx'); hold on; plot(girls(:,1),girls(:,2), 'ro'); 
p_pab=polyfit(boys(:,1),boys(:,2),x); y_pab=polyval(p_pab,boys(:,1));
plot(boys(:,1),y_pab,'b');
p_pag=polyfit(girls(:,1),girls(:,2),x); y_pag=polyval(p_pag,girls(:,1));
plot(girls(:,1),y_pag,'r'); hold off;
title('P'); xlabel('Věk [rok]'); ylabel('1. sp. moment [Hz]');

subplot 323;
plot(boys(:,1),boys(:,7),'bx'); hold on; plot(girls(:,1),girls(:,7), 'ro'); 
p_tab=polyfit(boys(:,1),boys(:,7),x); y_tab=polyval(p_tab,boys(:,1));
plot(boys(:,1),y_tab,'b');
p_tag=polyfit(girls(:,1),girls(:,7),x); y_tag=polyval(p_tag,girls(:,1));
plot(girls(:,1),y_tag,'r'); hold off;
title('T'); xlabel('Věk [rok]'); ylabel('1. sp. moment [Hz]');

subplot 325;
plot(boys(:,1),boys(:,12),'bx'); hold on; plot(girls(:,1),girls(:,12), 'ro'); 
p_kab=polyfit(boys(:,1),boys(:,12),x); y_kab=polyval(p_kab,boys(:,1));
plot(boys(:,1),y_kab,'b');
p_kag=polyfit(girls(:,1),girls(:,12),x); y_kag=polyval(p_kag,girls(:,1));
plot(girls(:,1),y_kag,'r'); hold off;
title('K'); xlabel('Věk [rok]'); ylabel('1. sp. moment [Hz]');

subplot 322;
plot(boys(:,1),boys(:,17),'bx'); hold on; plot(girls(:,1),girls(:,17), 'ro'); 
p_pab=polyfit(boys(:,1),boys(:,17),x); y_pab=polyval(p_pab,boys(:,1));
plot(boys(:,1),y_pab,'b');
p_pag=polyfit(girls(:,1),girls(:,17),x); y_pag=polyval(p_pag,girls(:,1));
plot(girls(:,1),y_pag,'r'); hold off;
title('(P)A'); xlabel('Věk [rok]'); ylabel('1. sp. moment [Hz]');  legend('Chlapci','Děvčata');

subplot 324;
plot(boys(:,1),boys(:,22),'bx'); hold on; plot(girls(:,1),girls(:,22), 'ro'); 
p_tab=polyfit(boys(:,1),boys(:,22),x); y_tab=polyval(p_tab,boys(:,1));
plot(boys(:,1),y_tab,'b');
p_tag=polyfit(girls(:,1),girls(:,22),x); y_tag=polyval(p_tag,girls(:,1));
plot(girls(:,1),y_tag,'r'); hold off;
title('(T)A'); xlabel('Věk [rok]'); ylabel('1. sp. moment [Hz]');

subplot 326;
plot(boys(:,1),boys(:,27),'bx'); hold on; plot(girls(:,1),girls(:,27), 'ro'); 
p_kab=polyfit(boys(:,1),boys(:,27),x); y_kab=polyval(p_kab,boys(:,1));
plot(boys(:,1),y_kab,'b');
p_kag=polyfit(girls(:,1),girls(:,27),x); y_kag=polyval(p_kag,girls(:,1));
plot(girls(:,1),y_kag,'r'); hold off;
title('(K)A'); xlabel('Věk [rok]'); ylabel('1. sp. moment [Hz]');

AVG_PA_boys=mean(boys(:,2))
AVG_TA_boys=mean(boys(:,7))
AVG_KA_boys=mean(boys(:,12))

AVG_PA_girls=mean(girls(:,2))
AVG_TA_girls=mean(girls(:,7))
AVG_KA_girls=mean(girls(:,12))

%%
% korelace genders
rp_m=zeros(2,3);
rp_f=zeros(2,3);
count=1;
for k=[2 7 12]
        [r,p]=corr([boys(:,1),boys(:,k)],'Type','Pearson'); %age x pa burst
        rp_m(1,count)=r(1,2);
        rp_m(2,count)=p(1,2);
        [r,p]=corr([girls(:,1),girls(:,k)],'Type','Pearson'); %age x pa burst
        rp_f(1,count)=r(1,2);
        rp_f(2,count)=p(1,2);
        count=count+1;
end

% vowel
rp_m_vowel=zeros(2,3);
rp_f_vowel=zeros(2,3);
count=1;
for k=[17 22 27]
    [r,p]=corr([boys(:,1),boys(:,k)],'Type','Pearson'); %age x pa burst
    rp_m_vowel(1,count)=r(1,2);
    rp_m_vowel(2,count)=p(1,2);
    [r,p]=corr([girls(:,1),girls(:,k)],'Type','Pearson'); %age x pa burst
    rp_f_vowel(1,count)=r(1,2);
    rp_f_vowel(2,count)=p(1,2);
    count=count+1;
end


%burst 2. moment
boys_burst2(:,1)=boys(:,1); boys_burst2(:,2)=boys(:,3);
boys_burst2(:,3)=boys(:,8); boys_burst2(:,4)=boys(:,13);
girls_burst2(:,1)=girls(:,1); girls_burst2(:,2)=girls(:,3);
girls_burst2(:,3)=girls(:,8); girls_burst2(:,4)=girls(:,13);

%burst 3. moment
boys_burst3(:,1)=boys(:,1); boys_burst3(:,2)=boys(:,4);
boys_burst3(:,3)=boys(:,9); boys_burst3(:,4)=boys(:,14);
girls_burst3(:,1)=girls(:,1); girls_burst3(:,2)=girls(:,4);
girls_burst3(:,3)=girls(:,9); girls_burst3(:,4)=girls(:,14);

%burst 4. moment
boys_burst4(:,1)=boys(:,1); boys_burst4(:,2)=boys(:,5);
boys_burst4(:,3)=boys(:,10); boys_burst4(:,4)=boys(:,15);
girls_burst4(:,1)=girls(:,1); girls_burst4(:,2)=girls(:,5);
girls_burst4(:,3)=girls(:,10); girls_burst4(:,4)=girls(:,15);

plots(boys_burst2, girls_burst2, '2. spectral moment');
plots(boys_burst3, girls_burst3, '3. spectral moment');
plots(boys_burst4, girls_burst4, '4. spectral moment');

%vowel 2. moment
boys_vowel2(:,1)=boys(:,1); boys_vowel2(:,2)=boys(:,18);
boys_vowel2(:,3)=boys(:,23); boys_vowel2(:,4)=boys(:,28);
girls_vowel2(:,1)=girls(:,1); girls_vowel2(:,2)=girls(:,18);
girls_vowel2(:,3)=girls(:,23); girls_vowel2(:,4)=girls(:,28);

%vowel 3. moment
boys_vowel3(:,1)=boys(:,1); boys_vowel3(:,2)=boys(:,19);
boys_vowel3(:,3)=boys(:,24); boys_vowel3(:,4)=boys(:,29);
girls_vowel3(:,1)=girls(:,1); girls_vowel3(:,2)=girls(:,19);
girls_vowel3(:,3)=girls(:,24); girls_vowel3(:,4)=girls(:,29);

%vowel 4. moment
boys_vowel4(:,1)=boys(:,1); boys_vowel4(:,2)=boys(:,20);
boys_vowel4(:,3)=boys(:,25); boys_vowel4(:,4)=boys(:,30);
girls_vowel4(:,1)=girls(:,1); girls_vowel4(:,2)=girls(:,20);
girls_vowel4(:,3)=girls(:,25); girls_vowel4(:,4)=girls(:,30);

plots(boys_vowel2, girls_vowel2, '2. spectral moment');
plots(boys_vowel3, girls_vowel3, '3. spectral moment');
plots(boys_vowel4, girls_vowel4, '4. spectral moment');

% slope
boys_burstsl(:,1)=boys(:,1); boys_burstsl(:,2)=boys(:,6);
boys_burstsl(:,3)=boys(:,11); boys_burstsl(:,4)=boys(:,16);
girls_burstsl(:,1)=girls(:,1); girls_burstsl(:,2)=girls(:,6);
girls_burstsl(:,3)=girls(:,11); girls_burstsl(:,4)=girls(:,16);

plots(boys_burstsl, girls_burstsl, 'slope');

boys_vowelsl(:,1)=boys(:,1); boys_vowelsl(:,2)=boys(:,21);
boys_vowelsl(:,3)=boys(:,26); boys_vowelsl(:,4)=boys(:,31);
girls_vowelsl(:,1)=girls(:,1); girls_vowelsl(:,2)=girls(:,21);
girls_vowelsl(:,3)=girls(:,26); girls_vowelsl(:,4)=girls(:,31);

plots(boys_vowelsl, girls_vowelsl, 'slope');

save('DP.mat','boys','girls','Moments_PA','Moments_TA','Moments_KA','data','G1M','G2M','G3M','G1F','G2F','G3F'); 