%% Cycle Counts with Known Time Values
% Generate a signal that resembles a load history, consisting of sinusoid half-periods connecting known, unevenly spaced reversals. The signal is sampled at 10 Hz for 15 seconds. Plot the extrema and the signal.

fs = 10;

X = [0 1 3 4 5 6 8 10 13 15];
Y = [-2 1 -3 5 -1 3 -4 4 -2 6];

Z = [];
for k = 1:length(Y)-1
    x = X(k+1)-X(k);
    z = -(Y(k+1)-Y(k))*cos(pi*(0:1/fs:x-1/fs)/x)+Y(k+1)+Y(k);
    Z = [Z z/2];
end
Z = [Z Y(end)];

t = linspace(X(1),X(end),length(Z));
plot(X,Y,'o',t,Z)

%
% Compute cycle counts for the data. Display the matrix of cycle counts.

[c,hist,edges,rmm,idx] = rainflow(Z,t);

TT = array2table(c,'VariableNames',{'Count','Range','Mean','Start','End'})

% Use rainflow without output arguments to display a histogram of cycles as a function of cycle average and cycle range.

rainflow(Z,t)

% Copyright 2019 The MathWorks, Inc.