function rev = extract_reversal_points(x)
%EXTRACT_REVERSAL_POINTS  Identify reversal points for rainflow counting
%
%   rev = extract_reversal_points(x)
%
%   Inputs
%   ------
%   x   : Nx1 or 1xN real-valued time history (stress, strain, load, etc.)
%
%   Outputs
%   -------
%   rev : vector of reversal (turning) points including endpoints
%
%   Notes
%   -----
%   - Flat segments are handled consistently by forcing zero slopes
%     to positive sign.
%   - Endpoints are always retained.
%   - Intended as a preprocessing step prior to rainflow cycle counting.

    % Ensure column vector
    x = x(:);

    % First difference
    dx = diff(x);

    % Sign of slope
    sg = sign(dx);
    sg(sg == 0) = 1;  % treat flat segments consistently

    % Identify sign changes and keep endpoints
    k = [true; diff(sg) ~= 0; true];

    % Extract reversal points
    rev = x(k);
end
