Skip to content

Cycle counting examples

Cycle counting explanation.

Rainflow cycle counting

import matplotlib.pyplot as plt

from py_fatigue.cycle_count.rainflow import rainflow
from py_fatigue.testing import get_random_data, get_sampled_time

time = get_sampled_time(fs=1, duration=120)
stress = get_random_data(
    time, seed=0, random_type="integers", low=1, high=13, min_=1, range_=11
)
rows, residuals, indices = rainflow(stress)
print(rows[:12, :3])  # Amplitude, mean, count
fig, ax = plt.subplots()
ax.plot(time, stress, label="Full history")
ax.plot(time[indices], residuals, "s--", label="Residuals")
ax.set(xlabel="Time (s)", ylabel="Stress (MPa)")
ax.legend()

The dashed trace joins the residual turning points. In the notebook, the bars show counts by stress-range bin and the right axis shows the cumulative count.

Serialize a cycle count.

Residuals and low-frequency fatigue damage

Draw two consecutive 120-sample windows. The first draws integer stresses from 1 to 12 MPa and the second from 21 to 32 MPa. The different stress levels expose the effect of the cross-window excursion. Use the testing helpers with fixed seeds to reproduce both windows.

from datetime import datetime, timedelta, timezone

import numpy as np
from py_fatigue import CycleCount, SNCurve
from py_fatigue.damage import get_pm
from py_fatigue.testing import get_random_data, get_sampled_time

time = get_sampled_time(fs=1, duration=120)
first = get_random_data(
    time, seed=0, random_type="integers", low=1, high=13, min_=1, range_=11
)
second = get_random_data(
    time, seed=1, random_type="integers", low=1, high=13, min_=21, range_=11
)
start = datetime(2024, 1, 1, tzinfo=timezone.utc)
left = CycleCount.from_timeseries(first, timestamp=start)
right = CycleCount.from_timeseries(second, timestamp=start + timedelta(seconds=len(first)))
whole = CycleCount.from_timeseries(np.concatenate((first, second)), timestamp=start)
added = left + right
curve = SNCurve(3, 12)
for label, count in [("Separate windows", added), ("Whole record", whole)]:
    print(label, f"{get_pm(count, curve).sum():.6e}")

The separate windows give 3.15430 × 10⁻⁸. The whole record gives 4.63305 × 10⁻⁸ and includes a 31 MPa stress range that spans the boundary. The next example processes the residuals from the same windows.

Residual concatenation

from datetime import datetime, timedelta, timezone

import numpy as np
from py_fatigue import CycleCount, SNCurve
from py_fatigue.damage import get_pm
from py_fatigue.testing import get_random_data, get_sampled_time

time = get_sampled_time(fs=1, duration=120)
first = get_random_data(
    time, seed=0, random_type="integers", low=1, high=13, min_=1, range_=11
)
second = get_random_data(
    time, seed=1, random_type="integers", low=1, high=13, min_=21, range_=11
)
start = datetime(2024, 1, 1, tzinfo=timezone.utc)
left = CycleCount.from_timeseries(first, timestamp=start)
right = CycleCount.from_timeseries(second, timestamp=start + timedelta(seconds=len(first)))
whole = CycleCount.from_timeseries(np.concatenate((first, second)), timestamp=start)
added = left + right
recovered = added.solve_lffd()
curve = SNCurve(3, 12)
for name, count in [("Windows", added), ("Recovered", recovered), ("Whole", whole)]:
    print(name, f"{get_pm(count, curve).sum():.6e}")

In this example the window sum is 31.92% below the whole-record result. Residual recovery brings the Miner damage to 4.63305 × 10⁻⁸, matching the whole-record count.

The interactive result shows each original window, its residuals over the full time series, common-bin histograms, and numerical damage differences relative to the whole signal. For real data, inspect gaps and discarded or binned cycles when interpreting the difference.

Combine serialized cycle counts.