clear cycleData
clear thisCycleData
pressure = dataTable{1}.pressureDBar;

% derivFilter = designfilt('differentiator', FilterOrder=10, PassbandFrequency = 0.02, StopbandFrequency=0.025);
% pDeriv=filter(derivFilter, pressure);

hold on
plot(pressure)
yyaxis right
plot(pDeriv)
hold off

absPDeriv = abs(pDeriv);
cycleNum = 1;

lowPHold = true;
pIncreasing = false;
highPHold = false;
pDecreasing = false;

lowPThresh = 30;
pIncrThresh = 2000;
highPHoldThresh = 1950;
pDecrThresh = 30;

cycleIndex = 1;

for i=1:length(pressure)
    if(lowPHold)
        if(pressure(i) > lowPThresh)
            i
            cycleData{cycleNum,:} = thisCycleData;
            clear thisCycleData; 
            cycleNum = cycleNum + 1
            cycleIndex = 1;
            lowPHold = false;
            pIncreasing = true;
        end
    elseif(pIncreasing)
        if(pressure(i) > pIncrThresh)
            i
            cycleData{cycleNum,:} = thisCycleData;
            clear thisCycleData; 
            cycleNum = cycleNum + 1
            cycleIndex = 1;
            pIncreasing = false;
            highPHold = true;
        end
    elseif(highPHold)
        if(pressure(i) < highPHoldThresh)
            i
            cycleData{cycleNum,:} = thisCycleData;
            clear thisCycleData; 
            cycleNum = cycleNum + 1
            cycleIndex = 1;
            highPHold = false;
            pDecreasing = true;
        end
    elseif(pDecreasing)
        if(pressure(i) < pDecrThresh)
            i
            cycleData{cycleNum,:} = thisCycleData;
            clear thisCycleData; 
            cycleNum = cycleNum + 1
            cycleIndex = 1;
            pDecreasing = false;
            lowPHold = true;
        end
    end

    thisCycleData(cycleIndex) = dataTable{1}.vrsVolts(i);
    cycleIndex = cycleIndex + 1;
end

figure
hold on
for i=1:4:length(cycleNum)
plot(cycleData{i,:})
end

figure
hold on
for i=2:4:length(cycleNum)
plot(cycleData{i,:})
end
