function [Values_R_conc] = R_conc(weight,height,time_R_conc,ch4_R_conc, ch7_R_conc, ch8_R_conc, ch12_R_conc) 
%funkcia na spracovanie nameraných signálov koncentrického merania pravej končatiny
%vstupmi je hmotnosť a výška subjekta a ďalej sú na vstupe výstupy z funkcie imp2vect
%výstupom funkcie je tabuľka parametrov slúžiacich na vyhodnotenie výsledkov
%spustenie funkcie: [Values_R_conc] = R_conc(60,1.69,time_R_conc,ch4_R_conc, ch7_R_conc, ch8_R_conc, ch12_R_conc)

% weight = _ v kg (subjekt D --> 60)
% height = _ v m (subjekt D --> 1.69)

%vzorkovacia frekvencia
fvz_R_conc = 4000; 

%kolko sekund zo zaznamu nas zaujima
s_R_conc = 23; 
time_R_conc = time_R_conc(1:(s_R_conc*fvz_R_conc)); 
ch4_R_conc = ch4_R_conc(1:(s_R_conc*fvz_R_conc));
ch7_R_conc = ch7_R_conc(1:(s_R_conc*fvz_R_conc));
ch8_R_conc = ch8_R_conc(1:(s_R_conc*fvz_R_conc));
ch12_R_conc = ch12_R_conc(1:(s_R_conc*fvz_R_conc));

%prepočty signálov
angle_R_conc = ch12_R_conc*2*1000/10; %uhol v °
torque_R_conc = ch4_R_conc*2*1000/5; %krútiaci moment v Nm
EMG_VM_R_conc = ch7_R_conc./1000; %EMG vastus medialis v mV
EMG_VL_R_conc = ch8_R_conc./1000; %EMG vastus lateralis v mV

%% izokineticky dynamometer

% filtracia signalov z izokinetickeho dynamometra
[b_R_conc,a_R_conc]=butter(3,20/fvz_R_conc,'low');
torque_filtered_R_conc=filtfilt(b_R_conc,a_R_conc,torque_R_conc);
angle_filtered_R_conc = filtfilt(b_R_conc,a_R_conc,angle_R_conc);

%detekcia maximálnych hodnôt sily
[yPKS1_R_conc, xPKS1_R_conc]=findpeaks(torque_filtered_R_conc, 'MinPeakHeight', 92, 'MinPeakDistance', 6000); %extenzia
[yPKS2_R_conc, xPKS2_R_conc]=findpeaks(-torque_filtered_R_conc, 'MinPeakHeight', 35, 'MinPeakDistance', 6000); %flexia

%uhly kedy došlo k maximám
yANG1_R_conc = angle_filtered_R_conc(xPKS1_R_conc); %extenzia
yANG2_R_conc = angle_filtered_R_conc(xPKS2_R_conc); %flexia

%priemery sily
EXTENSION_R_conc = mean(yPKS1_R_conc); %extenzia...koncentricky kvadriceps
FLEXION_R_conc = mean(yPKS2_R_conc); %flexia...koncentricky hamstring
HAMQUAD_R_conc = FLEXION_R_conc/EXTENSION_R_conc; %pomer H/Q

%priemery uhlov
ANG_EXTENSION_R_conc = mean(yANG1_R_conc); %extenzia
ANG_FLEXION_R_conc = mean(yANG2_R_conc); %flexia

%data na normalizáciu - body mass index
BMI_R_conc = weight/(height^2);

%% EMG

%filtracia signalov EMG
[e_R_conc,f_R_conc]=butter(4,20/fvz_R_conc,'high');
EMG_VL_filtered_R_conc =filtfilt(e_R_conc,f_R_conc,EMG_VL_R_conc); %vastus lateralis
EMG_VM_filtered_R_conc =filtfilt(e_R_conc,f_R_conc,EMG_VM_R_conc); %vastus medialis

%RMS                                                                
rmsv_EMG_VL_R_conc = sqrt(movmean(EMG_VL_filtered_R_conc.^2, 10)); 
rmsv_EMG_VM_R_conc = sqrt(movmean(EMG_VM_filtered_R_conc.^2, 10));

%detekcia maximálnych hodnôt aktivacie
[EMG_VL_yPKS1_R_conc, EMG_VL_xPKS1_R_conc]=findpeaks(rmsv_EMG_VL_R_conc, 'MinPeakHeight', 0.5, 'MinPeakDistance', 6000);
[EMG_VM_yPKS1_R_conc, EMG_VM_xPKS1_R_conc]=findpeaks(rmsv_EMG_VM_R_conc, 'MinPeakHeight', 0.45, 'MinPeakDistance', 6000);

%uhly kedy k maximám došlo
EMG_VL_yPKS1_ANG_R_conc = angle_filtered_R_conc(EMG_VL_xPKS1_R_conc);
EMG_VM_yPKS1_ANG_R_conc = angle_filtered_R_conc(EMG_VM_xPKS1_R_conc);

%data na normalizáciu EMG
rectified_EMG_VL_R_conc =abs(EMG_VL_R_conc);
max_rectified_EMG_VL_R_conc = max(rectified_EMG_VL_R_conc);
rectified_EMG_VM_R_conc =abs(EMG_VM_R_conc);
max_rectified_EMG_VM_R_conc = max(rectified_EMG_VM_R_conc);

%priemery EMG
VL_R_conc = mean(EMG_VL_yPKS1_R_conc);
VM_R_conc = mean(EMG_VM_yPKS1_R_conc);
EMG_R_conc = VM_R_conc/VL_R_conc;

%priemery uhlov EMG
ANG_VL_R_conc = mean(EMG_VL_yPKS1_ANG_R_conc);
ANG_VM_R_conc = mean(EMG_VM_yPKS1_ANG_R_conc);

%% surove signaly
figure
subplot 411
plot(time_R_conc, ch12_R_conc)
title('Pozícia - uhol končatiny', 'FontSize', 16)
xlabel('Čas [s]', 'FontSize', 16)
ylabel('Napätie [V]', 'FontSize', 16)
subplot 412
plot(time_R_conc, ch4_R_conc)
title('Krútiaci moment', 'FontSize', 16)
xlabel('Čas [s]', 'FontSize', 16)
ylabel('Napätie [V]', 'FontSize', 16)
subplot 413
plot (time_R_conc, ch7_R_conc)
title('EMG vastus medialis', 'FontSize', 16)
xlabel('Čas [s]', 'FontSize', 16')
ylabel('Napätie [mikroV]', 'FontSize', 16)
subplot 414
plot(time_R_conc, ch8_R_conc) 
title('EMG vastus lateralis', 'FontSize', 16)
xlabel('Čas [s]', 'FontSize', 16)
ylabel('Napätie [mikroV]', 'FontSize', 16)

sgtitle('Pravá končatina/koncentrické meranie - surové signály', 'FontSize', 18, 'FontWeight','bold')

%% torque spracovanie
figure
subplot 311
plot(time_R_conc,ch4_R_conc)
title('Krútiaci moment')
xlabel('Čas [s]')
ylabel('Napätie [V]')
subplot 312
plot(time_R_conc,torque_filtered_R_conc)
title('Krútiaci moment: prepočty a filtrácia')
xlabel('Čas [s]')
ylabel('Krútiaci moment [Nm]')
subplot 313
hold on
plot(time_R_conc,abs(torque_filtered_R_conc))
scatter(xPKS1_R_conc./fvz_R_conc, abs(yPKS1_R_conc), 'magenta')
scatter(xPKS2_R_conc./fvz_R_conc, abs(yPKS2_R_conc), 'black')
title('Krútiaci moment: detekcia max. hodnôt a prevod do absolútnych hodnôt')
xlabel('Čas[s]')
ylabel('Krútiaci moment [Nm]')

sgtitle('Pravá končatina/koncentrické meranie - spracovanie krútiaceho momentu') 

%% angle spracovanie
figure
subplot 311
plot(time_R_conc, ch12_R_conc)
title('Pozícia - uhol končatiny')
xlabel('Čas [s]')
ylabel('Napätie [V]')
subplot 312
plot(time_R_conc, angle_filtered_R_conc)
title('Pozícia - uhol končatiny: prepočty a filtrácia')
xlabel('Čas [s]')
ylabel('Uhol [°]')
subplot 313
hold on
plot(time_R_conc,angle_filtered_R_conc)
scatter(xPKS1_R_conc./fvz_R_conc,yANG1_R_conc,'magenta')
scatter(xPKS2_R_conc./fvz_R_conc,yANG2_R_conc, 'black')
scatter(EMG_VL_xPKS1_R_conc./fvz_R_conc,EMG_VL_yPKS1_ANG_R_conc, 'red')
scatter(EMG_VM_xPKS1_R_conc./fvz_R_conc,EMG_VM_yPKS1_ANG_R_conc, 'green')
title('Pozícia - uhol končatiny')
xlabel('Čas[s]')
ylabel('Uhol [°]')
legend('uhol v čase', 'krútiaci moment - extenzia', 'krútiaci moment - flexia','EMG VL', 'EMG VM')

sgtitle('Pravá končatina/koncentrické meranie - spracovanie pozície = uhlu končatiny') 

%% emg spracovanie - rovnaké pre VL aj VM
figure
subplot 311
plot(time_R_conc, ch8_R_conc) 
title('EMG vastus lateralis')
xlabel('Čas [s]')
ylabel('Napätie [mikroV]')
subplot 312
plot(time_R_conc, EMG_VL_filtered_R_conc)
title('EMG vastus lateralis: prepočet a filtrácia')
xlabel('Čas [s]')
ylabel('Napätie [mV]')
subplot 313
plot(time_R_conc, rmsv_EMG_VL_R_conc)
hold on
scatter(EMG_VL_xPKS1_R_conc./fvz_R_conc, EMG_VL_yPKS1_R_conc, 'black')
hold off
title('EMG vastus lateralis: RMS a detekcia max. hodnôt')
xlabel('Čas [s]')
ylabel('Napätie [mV]')

sgtitle('Pravá končatina/koncentrické meranie - spracovanie EMG signálu') 

%% spracované data
figure
subplot 411
hold on
plot(time_R_conc,angle_filtered_R_conc)
scatter(xPKS2_R_conc./fvz_R_conc,yANG2_R_conc, 'black')
scatter(xPKS1_R_conc./fvz_R_conc,yANG1_R_conc,'magenta')
scatter(EMG_VL_xPKS1_R_conc./fvz_R_conc,EMG_VL_yPKS1_ANG_R_conc, 'red')
scatter(EMG_VM_xPKS1_R_conc./fvz_R_conc,EMG_VM_yPKS1_ANG_R_conc, 'green')
title('Pozícia - uhol končatiny', 'FontSize', 16)
xlabel('Čas [s]', 'FontSize', 16)
ylabel('Uhol [°]', 'FontSize', 16)
legend('10° -> 90° = flexia končatiny, 90° -> 10° = extenzia končatiny', 'krútiaci moment - flexia', 'krútiaci moment - extenzia','EMG vastus lateralis', 'EMG vastus medialis', 'FontSize', 14)
subplot 412
hold on
plot(time_R_conc,abs(torque_filtered_R_conc))
scatter(xPKS2_R_conc./fvz_R_conc, abs(yPKS2_R_conc), 'black')
scatter(xPKS1_R_conc./fvz_R_conc, abs(yPKS1_R_conc), 'magenta')
title('Krútiaci moment', 'FontSize', 16)
xlabel('Čas [s]', 'FontSize', 16)
ylabel('Krútiaci moment [Nm]', 'FontSize', 16)
legend('Krútiaci moment v čase', 'maximá počas flexie', 'maximá počas extenzie', 'FontSize', 14);
subplot 413
plot(time_R_conc, rmsv_EMG_VM_R_conc)
hold on
scatter(EMG_VM_xPKS1_R_conc./fvz_R_conc, EMG_VM_yPKS1_R_conc, 'green')
hold off
title('EMG vastus medialis - RMS', 'FontSize', 16)
xlabel('Čas [s]', 'FontSize', 16)
ylabel('Napätie [mV]','FontSize', 16)
legend('EMG vastus medialis v čase' , 'maximálne aktivácie', 'FontSize', 14)
subplot 414
plot(time_R_conc, rmsv_EMG_VL_R_conc)
hold on
scatter(EMG_VL_xPKS1_R_conc./fvz_R_conc, EMG_VL_yPKS1_R_conc, 'red')
title('EMG vastus lateralis - RMS', 'FontSize', 16)
xlabel('Čas [s]', 'FontSize', 16)
ylabel('Napätie [mV]', 'FontSize', 16)
legend('EMG vastus leteralis v čase' , 'maximálne aktivácie','FontSize', 14)

sgtitle('Pravá končatina/koncentrické meranie - spracované signály', 'FontSize', 18) 

%% hodnoty
values_R_conc = zeros(11,1);
values_R_conc(1,1) = BMI_R_conc;
values_R_conc(2,1) = EXTENSION_R_conc;
values_R_conc(3,1) = ANG_EXTENSION_R_conc;
values_R_conc(4,1) = FLEXION_R_conc;
values_R_conc(5,1) = ANG_FLEXION_R_conc;
values_R_conc(6,1) = VM_R_conc;
values_R_conc(7,1) = ANG_VM_R_conc;
values_R_conc(8,1) = VL_R_conc;
values_R_conc(9,1) = ANG_VL_R_conc;
values_R_conc(10,1)= max_rectified_EMG_VM_R_conc; 
values_R_conc(11,1)= max_rectified_EMG_VL_R_conc;

values_R_conc = values_R_conc.';
column_R_conc = {'BMI','Kvadricep_Konc','Uhol_KK','Hamstring_Konc','Uhol_HK','VM_Konc','Uhol_VMK ','VL_Konc','Uhol_VLK','Max_VMK','Max_VLK'};
row_R_conc = {'Subjekt'};
Values_R_conc = array2table(values_R_conc, 'RowNames', row_R_conc, 'VariableNames', column_R_conc);


