clear all;
clc;

O25P101_rpm = linspace(1, 1, 163840);
O25P102_rpm = linspace(1, 1, 163840);
O25P103_rpm = linspace(1, 1, 163840);
O25P104_rpm = linspace(1, 1, 163840);
O25P105_rpm = linspace(1, 1, 163840);
O25P106_rpm = linspace(1, 1, 163840);
O25P107_rpm = linspace(1, 1, 163840);
O25P108_rpm = linspace(1, 1, 163840);
O25P109_rpm = linspace(1, 1, 163840);
O25P110_rpm = linspace(1, 1, 163840);

O50P101_rpm = linspace(1, 1, 163840);
O50P102_rpm = linspace(1, 1, 163840);
O50P103_rpm = linspace(1, 1, 163840);
O50P104_rpm = linspace(1, 1, 163840);
O50P105_rpm = linspace(1, 1, 163840);
O50P106_rpm = linspace(1, 1, 163840);
O50P107_rpm = linspace(1, 1, 163840);
O50P108_rpm = linspace(1, 1, 163840);
O50P109_rpm = linspace(1, 1, 163840);
O50P110_rpm = linspace(1, 1, 163840);

V25P101_rpm = linspace(1, 1, 163840);
V25P102_rpm = linspace(1, 1, 163840);
V25P103_rpm = linspace(1, 1, 163840);
V25P104_rpm = linspace(1, 1, 163840);
V25P105_rpm = linspace(1, 1, 163840);
V25P106_rpm = linspace(1, 1, 163840);
V25P107_rpm = linspace(1, 1, 163840);
V25P108_rpm = linspace(1, 1, 163840);
V25P109_rpm = linspace(1, 1, 163840);
V25P110_rpm = linspace(1, 1, 163840);

V50P101_rpm = linspace(1, 1, 163840);
V50P102_rpm = linspace(1, 1, 163840);
V50P103_rpm = linspace(1, 1, 163840);
V50P104_rpm = linspace(1, 1, 163840);
V50P105_rpm = linspace(1, 1, 163840);
V50P106_rpm = linspace(1, 1, 163840);
V50P107_rpm = linspace(1, 1, 163840);
V50P108_rpm = linspace(1, 1, 163840);
V50P109_rpm = linspace(1, 1, 163840);
V50P110_rpm = linspace(1, 1, 163840);

load('O25P1-1.mat','Channel_7_Data');
O25P101_rpm=Channel_7_Data;
load('O25P1-2.mat','Channel_7_Data');
O25P102_rpm=Channel_7_Data;
load('O25P1-3.mat','Channel_7_Data');
O25P103_rpm=Channel_7_Data;
load('O25P1-4.mat','Channel_7_Data');
O25P104_rpm=Channel_7_Data;
load('O25P1-5.mat','Channel_7_Data');
O25P105_rpm=Channel_7_Data;
load('O25P1-6.mat','Channel_7_Data');
O25P106_rpm=Channel_7_Data;
load('O25P1-7.mat','Channel_7_Data');
O25P107_rpm=Channel_7_Data;
load('O25P1-8.mat','Channel_7_Data');
O25P108_rpm=Channel_7_Data;
load('O25P1-9.mat','Channel_7_Data');
O25P109_rpm=Channel_7_Data;
load('O25P1-10.mat','Channel_7_Data');
O25P110_rpm=Channel_7_Data;

load('O50P1-1.mat','Channel_7_Data');
O50P101_rpm=Channel_7_Data;
load('O50P1-2.mat','Channel_7_Data');
O50P102_rpm=Channel_7_Data;
load('O50P1-3.mat','Channel_7_Data');
O50P103_rpm=Channel_7_Data;
load('O50P1-4.mat','Channel_7_Data');
O50P104_rpm=Channel_7_Data;
load('O50P1-5.mat','Channel_7_Data');
O50P105_rpm=Channel_7_Data;
load('O50P1-6.mat','Channel_7_Data');
O50P106_rpm=Channel_7_Data;
load('O50P1-7.mat','Channel_7_Data');
O50P107_rpm=Channel_7_Data;
load('O50P1-8.mat','Channel_7_Data');
O50P108_rpm=Channel_7_Data;
load('O50P1-9.mat','Channel_7_Data');
O50P109_rpm=Channel_7_Data;
load('O50P1-10.mat','Channel_7_Data');
O50P110_rpm=Channel_7_Data;

load('V25P1-1.mat','Channel_7_Data');
V25P101_rpm=Channel_7_Data;
load('V25P1-2.mat','Channel_7_Data');
V25P102_rpm=Channel_7_Data;
load('V25P1-3.mat','Channel_7_Data');
V25P103_rpm=Channel_7_Data;
load('V25P1-4.mat','Channel_7_Data');
V25P104_rpm=Channel_7_Data;
load('V25P1-5.mat','Channel_7_Data');
V25P105_rpm=Channel_7_Data;
load('V25P1-6.mat','Channel_7_Data');
V25P106_rpm=Channel_7_Data;
load('V25P1-7.mat','Channel_7_Data');
V25P107_rpm=Channel_7_Data;
load('V25P1-8.mat','Channel_7_Data');
V25P108_rpm=Channel_7_Data;
load('V25P1-9.mat','Channel_7_Data');
V25P109_rpm=Channel_7_Data;
load('V25P1-10.mat','Channel_7_Data');
V25P110_rpm=Channel_7_Data;

load('V50P1-1.mat','Channel_7_Data');
V50P101_rpm=Channel_7_Data;
load('V50P1-2.mat','Channel_7_Data');
V50P102_rpm=Channel_7_Data;
load('V50P1-3.mat','Channel_7_Data');
V50P103_rpm=Channel_7_Data;
load('V50P1-4.mat','Channel_7_Data');
V50P104_rpm=Channel_7_Data;
load('V50P1-5.mat','Channel_7_Data');
V50P105_rpm=Channel_7_Data;
load('V50P1-6.mat','Channel_7_Data');
V50P106_rpm=Channel_7_Data;
load('V50P1-7.mat','Channel_7_Data');
V50P107_rpm=Channel_7_Data;
load('V50P1-8.mat','Channel_7_Data');
V50P108_rpm=Channel_7_Data;
load('V50P1-9.mat','Channel_7_Data');
V50P109_rpm=Channel_7_Data;
load('V50P1-10.mat','Channel_7_Data');
V50P110_rpm=Channel_7_Data;



time01 = linspace(1, 163840, 163840)';
time02 = linspace(1, 163840, 163840)';
time03 = linspace(1, 163840, 163840)';
time04 = linspace(1, 163840, 163840)';
time05 = linspace(1, 163840, 163840)';
time06 = linspace(1, 163840, 163840)';
time07 = linspace(1, 163840, 163840)';
time08 = linspace(1, 163840, 163840)';
time09 = linspace(1, 163840, 163840)';
time10 = linspace(1, 163840, 163840)';

time11 = linspace(1, 163840, 163840)';
time12 = linspace(1, 163840, 163840)';
time13 = linspace(1, 163840, 163840)';
time14 = linspace(1, 163840, 163840)';
time15 = linspace(1, 163840, 163840)';
time16 = linspace(1, 163840, 163840)';
time17 = linspace(1, 163840, 163840)';
time18 = linspace(1, 163840, 163840)';
time19 = linspace(1, 163840, 163840)';
time20 = linspace(1, 163840, 163840)';

time21 = linspace(1, 163840, 163840)';
time22 = linspace(1, 163840, 163840)';
time23 = linspace(1, 163840, 163840)';
time24 = linspace(1, 163840, 163840)';
time25 = linspace(1, 163840, 163840)';
time26 = linspace(1, 163840, 163840)';
time27 = linspace(1, 163840, 163840)';
time28 = linspace(1, 163840, 163840)';
time29 = linspace(1, 163840, 163840)';
time30 = linspace(1, 163840, 163840)';

time31 = linspace(1, 163840, 163840)';
time32 = linspace(1, 163840, 163840)';
time33 = linspace(1, 163840, 163840)';
time34 = linspace(1, 163840, 163840)';
time35 = linspace(1, 163840, 163840)';
time36 = linspace(1, 163840, 163840)';
time37 = linspace(1, 163840, 163840)';
time38 = linspace(1, 163840, 163840)';
time39 = linspace(1, 163840, 163840)';
time40 = linspace(1, 163840, 163840)';

schwellwert = 0.3;

start01 = find(abs(O25P101_rpm) > schwellwert, 1);
start02 = find(abs(O25P102_rpm) > schwellwert, 1);
start03 = find(abs(O25P103_rpm) > schwellwert, 1);
start04 = find(abs(O25P104_rpm) > schwellwert, 1);
start05 = find(abs(O25P105_rpm) > schwellwert, 1);
start06 = find(abs(O25P106_rpm) > schwellwert, 1);
start07 = find(abs(O25P107_rpm) > schwellwert, 1);
start08 = find(abs(O25P108_rpm) > schwellwert, 1);
start09 = find(abs(O25P109_rpm) > schwellwert, 1);
start10 = find(abs(O25P110_rpm) > schwellwert, 1);

start11 = find(abs(O50P101_rpm) > schwellwert, 1);
start12 = find(abs(O50P102_rpm) > schwellwert, 1);
start13 = find(abs(O50P103_rpm) > schwellwert, 1);
start14 = find(abs(O50P104_rpm) > schwellwert, 1);
start15 = find(abs(O50P105_rpm) > schwellwert, 1);
start16 = find(abs(O50P106_rpm) > schwellwert, 1);
start17 = find(abs(O50P107_rpm) > schwellwert, 1);
start18 = find(abs(O50P108_rpm) > schwellwert, 1);
start19 = find(abs(O50P109_rpm) > schwellwert, 1);
start20 = find(abs(O50P110_rpm) > schwellwert, 1);

start21 = find(abs(V25P101_rpm) > schwellwert, 1);
start22 = find(abs(V25P102_rpm) > schwellwert, 1);
start23 = find(abs(V25P103_rpm) > schwellwert, 1);
start24 = find(abs(V25P104_rpm) > schwellwert, 1);
start25 = find(abs(V25P105_rpm) > schwellwert, 1);
start26 = find(abs(V25P106_rpm) > schwellwert, 1);
start27 = find(abs(V25P107_rpm) > schwellwert, 1);
start28 = find(abs(V25P108_rpm) > schwellwert, 1);
start29 = find(abs(V25P109_rpm) > schwellwert, 1);
start30 = find(abs(V25P110_rpm) > schwellwert, 1);

start31 = find(abs(V50P101_rpm) > schwellwert, 1);
start32 = find(abs(V50P102_rpm) > schwellwert, 1);
start33 = find(abs(V50P103_rpm) > schwellwert, 1);
start34 = find(abs(V50P104_rpm) > schwellwert, 1);
start35 = find(abs(V50P105_rpm) > schwellwert, 1);
start36 = find(abs(V50P106_rpm) > schwellwert, 1);
start37 = find(abs(V50P107_rpm) > schwellwert, 1);
start38 = find(abs(V50P108_rpm) > schwellwert, 1);
start39 = find(abs(V50P109_rpm) > schwellwert, 1);
start40 = find(abs(V50P110_rpm) > schwellwert, 1);

time01(1:start01-1) = []; 
O25P101_rpm(1:start01-1) = [];
time02(1:start02-1) = []; 
O25P102_rpm(1:start02-1) = [];
time03(1:start03-1) = []; 
O25P103_rpm(1:start03-1) = [];
time04(1:start04-1) = []; 
O25P104_rpm(1:start04-1) = [];
time05(1:start05-1) = []; 
O25P105_rpm(1:start05-1) = [];
time06(1:start06-1) = []; 
O25P106_rpm(1:start06-1) = [];
time07(1:start07-1) = []; 
O25P107_rpm(1:start07-1) = [];
time08(1:start08-1) = []; 
O25P108_rpm(1:start08-1) = [];
time09(1:start09-1) = []; 
O25P109_rpm(1:start09-1) = [];
time10(1:start10-1) = []; 
O25P110_rpm(1:start10-1) = [];

time11(1:start11-1) = []; 
O50P101_rpm(1:start11-1) = [];
time12(1:start12-1) = []; 
O50P102_rpm(1:start12-1) = [];
time13(1:start13-1) = []; 
O50P103_rpm(1:start13-1) = [];
time14(1:start14-1) = []; 
O50P104_rpm(1:start14-1) = [];
time15(1:start15-1) = []; 
O50P105_rpm(1:start15-1) = [];
time16(1:start16-1) = []; 
O50P106_rpm(1:start16-1) = [];
time17(1:start17-1) = []; 
O50P107_rpm(1:start17-1) = [];
time18(1:start18-1) = []; 
O50P108_rpm(1:start18-1) = [];
time19(1:start19-1) = []; 
O50P109_rpm(1:start19-1) = [];
time20(1:start20-1) = []; 
O50P110_rpm(1:start20-1) = [];

time21(1:start21-1) = []; 
V25P101_rpm(1:start21-1) = [];
time22(1:start22-1) = []; 
V25P102_rpm(1:start22-1) = [];
time23(1:start23-1) = []; 
V25P103_rpm(1:start23-1) = [];
time24(1:start24-1) = []; 
V25P104_rpm(1:start24-1) = [];
time25(1:start25-1) = []; 
V25P105_rpm(1:start25-1) = [];
time26(1:start26-1) = []; 
V25P106_rpm(1:start26-1) = [];
time27(1:start27-1) = []; 
V25P107_rpm(1:start27-1) = [];
time28(1:start28-1) = []; 
V25P108_rpm(1:start28-1) = [];
time29(1:start29-1) = []; 
V25P109_rpm(1:start29-1) = [];
time30(1:start30-1) = []; 
V25P110_rpm(1:start30-1) = [];

time31(1:start31-1) = []; 
V50P101_rpm(1:start31-1) = [];
time32(1:start32-1) = []; 
V50P102_rpm(1:start32-1) = [];
time33(1:start33-1) = []; 
V50P103_rpm(1:start33-1) = [];
time34(1:start34-1) = []; 
V50P104_rpm(1:start34-1) = [];
time35(1:start35-1) = []; 
V50P105_rpm(1:start35-1) = [];
time36(1:start36-1) = []; 
V50P106_rpm(1:start36-1) = [];
time37(1:start37-1) = []; 
V50P107_rpm(1:start37-1) = [];
time38(1:start38-1) = []; 
V50P108_rpm(1:start38-1) = [];
time39(1:start39-1) = []; 
V50P109_rpm(1:start39-1) = [];
time40(1:start40-1) = []; 
V50P110_rpm(1:start40-1) = [];

for k = 1:10
load(['O25P1-', num2str(k), '.mat'],'Channel_6_Data'); 
accO25P1{k}=Channel_6_Data; 
end

for l = 1:10 
load(['O50P1-', num2str(l), '.mat'],'Channel_6_Data'); 
accO50P1{l}=Channel_6_Data; 
end

for m = 1:10 
load(['V25P1-', num2str(m), '.mat'],'Channel_6_Data'); 
accV25P1{m}=Channel_6_Data; 
end

for n = 1:10 
load(['V50P1-', num2str(n), '.mat'],'Channel_6_Data'); 
accV50P1{n}=Channel_6_Data; 
end

%Plot vorher
figure
hold on
plot(accO25P1{1, 1});
plot(accO25P1{1, 2});
plot(accO25P1{1, 3});
plot(accO25P1{1, 4});
plot(accO25P1{1, 5});
plot(accO25P1{1, 6});
plot(accO25P1{1, 7});
plot(accO25P1{1, 8});
plot(accO25P1{1, 9});
plot(accO25P1{1, 10});

plot(accO50P1{1, 1});
plot(accO50P1{1, 2});
plot(accO50P1{1, 3});
plot(accO50P1{1, 4});
plot(accO50P1{1, 5});
plot(accO50P1{1, 6});
plot(accO50P1{1, 7});
plot(accO50P1{1, 8});
plot(accO50P1{1, 9});
plot(accO50P1{1, 10});

plot(accV25P1{1, 1});
plot(accV25P1{1, 2});
plot(accV25P1{1, 3});
plot(accV25P1{1, 4});
plot(accV25P1{1, 5});
plot(accV25P1{1, 6});
plot(accV25P1{1, 7});
plot(accV25P1{1, 8});
plot(accV25P1{1, 9});
plot(accV25P1{1, 10});

plot(accV50P1{1, 1});
plot(accV50P1{1, 2});
plot(accV50P1{1, 3});
plot(accV50P1{1, 4});
plot(accV50P1{1, 5});
plot(accV50P1{1, 6});
plot(accV50P1{1, 7});
plot(accV50P1{1, 8});
plot(accV50P1{1, 9});
plot(accV50P1{1, 10});

accO25P1{1, 1}(1:start01-1) = [];
accO25P1{1, 2}(1:start02-1) = [];
accO25P1{1, 3}(1:start03-1) = [];
accO25P1{1, 4}(1:start04-1) = [];
accO25P1{1, 5}(1:start05-1) = [];
accO25P1{1, 6}(1:start06-1) = [];
accO25P1{1, 7}(1:start07-1) = [];
accO25P1{1, 8}(1:start08-1) = [];
accO25P1{1, 9}(1:start09-1) = [];
accO25P1{1, 10}(1:start10-1) = [];

accO50P1{1, 1}(1:start11-1) = [];
accO50P1{1, 2}(1:start12-1) = [];
accO50P1{1, 3}(1:start13-1) = [];
accO50P1{1, 4}(1:start14-1) = [];
accO50P1{1, 5}(1:start15-1) = [];
accO50P1{1, 6}(1:start16-1) = [];
accO50P1{1, 7}(1:start17-1) = [];
accO50P1{1, 8}(1:start18-1) = [];
accO50P1{1, 9}(1:start19-1) = [];
accO50P1{1, 10}(1:start20-1) = [];

accV25P1{1, 1}(1:start21-1) = [];
accV25P1{1, 2}(1:start22-1) = [];
accV25P1{1, 3}(1:start23-1) = [];
accV25P1{1, 4}(1:start24-1) = [];
accV25P1{1, 5}(1:start25-1) = [];
accV25P1{1, 6}(1:start26-1) = [];
accV25P1{1, 7}(1:start27-1) = [];
accV25P1{1, 8}(1:start28-1) = [];
accV25P1{1, 9}(1:start29-1) = [];
accV25P1{1, 10}(1:start30-1) = [];

accV50P1{1, 1}(1:start31-1) = [];
accV50P1{1, 2}(1:start32-1) = [];
accV50P1{1, 3}(1:start33-1) = [];
accV50P1{1, 4}(1:start34-1) = [];
accV50P1{1, 5}(1:start35-1) = [];
accV50P1{1, 6}(1:start36-1) = [];
accV50P1{1, 7}(1:start37-1) = [];
accV50P1{1, 8}(1:start38-1) = [];
accV50P1{1, 9}(1:start39-1) = [];
accV50P1{1, 10}(1:start40-1) = [];

laengenO25P1 = cellfun(@length, accO25P1); 
minLaengeO25P1 = min(laengenO25P1); 
accO25P1 = cellfun(@(x) x(1:minLaengeO25P1), accO25P1, 'UniformOutput', false);

laengenO50P1 = cellfun(@length, accO50P1); 
minLaengeO50P1 = min(laengenO50P1); 
accO50P1 = cellfun(@(x) x(1:minLaengeO50P1), accO50P1, 'UniformOutput', false);

laengenV25P1 = cellfun(@length, accV25P1); 
minLaengeV25P1 = min(laengenV25P1); 
accV25P1 = cellfun(@(x) x(1:minLaengeV25P1), accV25P1, 'UniformOutput', false);

laengenV50P1 = cellfun(@length, accV50P1); 
minLaengeV50P1 = min(laengenV50P1); 
accV50P1 = cellfun(@(x) x(1:minLaengeV50P1), accV50P1, 'UniformOutput', false);

% Plot hinterher
figure
hold on
plot(accO25P1{1, 1});
plot(accO25P1{1, 2});
plot(accO25P1{1, 3});
plot(accO25P1{1, 4});
plot(accO25P1{1, 5});
plot(accO25P1{1, 6});
plot(accO25P1{1, 7});
plot(accO25P1{1, 8});
plot(accO25P1{1, 9});
plot(accO25P1{1, 10});

plot(accO50P1{1, 1});
plot(accO50P1{1, 2});
plot(accO50P1{1, 3});
plot(accO50P1{1, 4});
plot(accO50P1{1, 5});
plot(accO50P1{1, 6});
plot(accO50P1{1, 7});
plot(accO50P1{1, 8});
plot(accO50P1{1, 9});
plot(accO50P1{1, 10});

plot(accV25P1{1, 1});
plot(accV25P1{1, 2});
plot(accV25P1{1, 3});
plot(accV25P1{1, 4});
plot(accV25P1{1, 5});
plot(accV25P1{1, 6});
plot(accV25P1{1, 7});
plot(accV25P1{1, 8});
plot(accV25P1{1, 9});
plot(accV25P1{1, 10});

plot(accV50P1{1, 1});
plot(accV50P1{1, 2});
plot(accV50P1{1, 3});
plot(accV50P1{1, 4});
plot(accV50P1{1, 5});
plot(accV50P1{1, 6});
plot(accV50P1{1, 7});
plot(accV50P1{1, 8});
plot(accV50P1{1, 9});
plot(accV50P1{1, 10});

accO25P1_=cell2mat(accO25P1);
accO50P1_=cell2mat(accO50P1);
accV25P1_=cell2mat(accV25P1);
accV50P1_=cell2mat(accV50P1);

O25P1_ave = mean(accO25P1_,2);
O50P1_ave = mean(accO50P1_,2);
V25P1_ave = mean(accV25P1_,2);
V50P1_ave = mean(accV50P1_,2);

all{1, 1}=O25P1_ave;
all{1, 2}=O50P1_ave;
all{1, 3}=V25P1_ave;
all{1, 4}=V50P1_ave;

laengen_all = cellfun(@length, all); 
minLaenge_all = min(laengen_all); 
P1_all = cellfun(@(x) x(1:minLaenge_all), all, 'UniformOutput', false);

P1_all_ = cell2mat(P1_all);

figure;
hold on;
plot(P1_all{1, 1});
plot(P1_all{1, 2});
plot(P1_all{1, 3});
plot(P1_all{1, 4});

P1_ave = mean(P1_all_,2);

figure;
hold on;
plot(P1_ave);
hold off;



%%%%%%%%%%%%%%%%%%%%%%%%%1.Mglk.%%%%%%%%%%%%%%%%%%%%%%%%%  
x_P1 = length(P1_ave); 
time = linspace(1, 15, x_P1)';
pointsPerSecond = round(x_P1/20); 
coeffPpS = ones(1, pointsPerSecond)/pointsPerSecond;
avg = filter(coeffPpS, 0.7, P1_ave);
fDelay = (length(coeffPpS)-1)/2;
%plot(time,[P1_ave avg])        %Plot mit Delay
figure;
plot(time,P1_ave, ...
     time-fDelay/pointsPerSecond,avg);
grid;
axis tight;
legend('Input Data','Filtered Data','location','best');
ylabel('Acceleration (m/sē)');
xlabel('Elapsed Time (s)');
title('Acceleration respectively 40 Attempts');
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%


%%%%%%%%%%%%%%%%%%%%%%Schwenkwinkel%%%%%%%%%%%%%%%%%%%%%%
phi = atan((avg/9.81))*(360/(2*pi));
figure;
plot(time, phi);
grid;
axis tight;
legend('Swivel Angle PHI in degree','location','best');
ylabel('$\varphi$','Interpreter','Latex');
xlabel('Elapsed Time (s)')
title('Swivel Angle PHI in degree');
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%


%%%%%%%%%%%%%%%%%%Ergebnisse_speichern%%%%%%%%%%%%%%%%%%%
%save ('P1_phi.mat', 'phi', 'time');
%save ('P1_acc_avg.mat', 'avg', 'time');
save ('P1_ave.mat', 'P1_ave', 'time');
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
