crashmetrics.correlation¶
correlation
¶
Test-versus-CAE correlation: ISO/TS 18571, CC rating and error measures.
Rate how well a computed signal (usually CAE) matches a reference signal (usually a physical test) with one number between 0 and 1, and its sub-scores.
Functions:
iso18571: ISO/TS 18571:2024 objective rating (corridor, phase, magnitude, slope) and its grade (excellent, good, fair, poor).cc_rating: Corridor and cross-correlation (CC) rating: a corridor score plus phase, size and shape from the cross-correlation.error_measures: RMSE, normalised RMSE, peak and peak-time errors, correlation coefficient, Sprague & Geers and Russell errors.compare: All three at once;flattenturns the result into columns for a feature table.resample_pair: Put two signals on one uniform time grid over their common interval.
Conventions:
referenceis the signal you trust (the test),comparisonthe one being rated (CAE). The ratings are not symmetric: corridors are sized from the reference.- Signals may have different time bases; they are linearly resampled to
dt(default 0.1 ms, the 10 kHz that ISO/TS 18571 assumes) over the common interval, or overt_starttot_end. - Filter both signals the same way (same CFC) before rating them.
- A positive time shift means the comparison signal is late.
ISO18571Parameters
dataclass
¶
ISO18571Parameters(
a0: float = 0.05,
b0: float = 0.5,
k_z: float = 2.0,
max_shift: float = 0.2,
k_p: float = 1.0,
max_magnitude_error: float = 0.5,
k_m: float = 1.0,
max_slope_error: float = 2.0,
k_s: float = 1.0,
dtw_window: float = 0.1,
w_z: float = 0.4,
w_p: float = 0.2,
w_m: float = 0.2,
w_s: float = 0.2,
)
Parameters of ISO/TS 18571; the defaults are the standard's values. Change them only for studies of the metric itself.
a0
class-attribute
instance-attribute
¶
Inner corridor half width, fraction of the reference's absolute maximum.
b0
class-attribute
instance-attribute
¶
Outer corridor half width, fraction of the absolute maximum.
max_shift
class-attribute
instance-attribute
¶
Maximum allowable time shift, fraction of the interval (eps_P*).
max_magnitude_error
class-attribute
instance-attribute
¶
Maximum allowable magnitude error (eps_M*).
max_slope_error
class-attribute
instance-attribute
¶
Maximum allowable slope error (eps_S*).
dtw_window
class-attribute
instance-attribute
¶
Dynamic time warping window, fraction of the signal length (ISO/TS 18571:2024).
ISO18571Result
dataclass
¶
ISO18571Result(
rating: float,
grade: str,
corridor: float,
phase: float,
magnitude: float,
slope: float,
time_shift_ms: float,
correlation: float,
magnitude_error: float,
slope_error: float,
t_start: float,
t_end: float,
n_points: int,
)
ISO/TS 18571 rating of a comparison signal against a reference.
CCParameters
dataclass
¶
CCParameters(
a0: float = 0.05,
b0: float = 0.5,
k_z: float = 2.0,
int_min: float = 0.8,
d_min: float = 0.01,
d_max: float = 0.12,
k_v: float = 1.0,
k_g: float = 1.0,
k_p: float = 1.0,
w_phase: float = 1 / 3,
w_size: float = 1 / 3,
w_shape: float = 1 / 3,
w_corridor: float = 0.5,
w_cross: float = 0.5,
norm: float | None = None,
)
Parameters of the corridor and cross-correlation rating.
The defaults are the values commonly cited for the published method (Gehre, Gades and Wernicke, ESV 2009). If your project compares results with another implementation, check its settings and set these to match. Names in parentheses are the parameter names commonly used for this method.
a0
class-attribute
instance-attribute
¶
Inner corridor half width, fraction of the norm (A_0).
int_min
class-attribute
instance-attribute
¶
Minimum overlap of the shifted signals, fraction of the interval
(INT_MIN); the largest shift is 1 - int_min.
d_min
class-attribute
instance-attribute
¶
Time shift with full phase points, fraction of the interval (D_MIN).
d_max
class-attribute
instance-attribute
¶
Time shift with zero phase points (D_MAX).
w_phase
class-attribute
instance-attribute
¶
Weight of the phase rating in the cross-correlation rating (G_V).
w_shape
class-attribute
instance-attribute
¶
Weight of the shape rating (G_P).
w_corridor
class-attribute
instance-attribute
¶
Weight of the corridor rating in the total (G_1).
w_cross
class-attribute
instance-attribute
¶
Weight of the cross-correlation rating in the total (G_2).
norm
class-attribute
instance-attribute
¶
Corridor norm; default the reference's absolute maximum. Set it for small signals (for example a minimum displacement) so tiny absolute differences are not penalised.
CCResult
dataclass
¶
CCResult(
rating: float,
corridor: float,
cross_correlation: float,
phase: float,
size: float,
shape: float,
time_shift_ms: float,
correlation: float,
t_start: float,
t_end: float,
)
Corridor and cross-correlation rating of a comparison signal against a reference.
cross_correlation
instance-attribute
¶
Cross-correlation rating (weighted phase, size and shape).
size
instance-attribute
¶
Size rating: ratio of the smaller to the larger signal energy.
shape
instance-attribute
¶
Shape (progression) rating: the cross-correlation at the best shift.
time_shift_ms
instance-attribute
¶
Time shift with the highest cross-correlation, ms (positive: the comparison is late).
ErrorMeasures
dataclass
¶
ErrorMeasures(
rmse: float,
nrmse: float,
mae: float,
max_error: float,
peak_reference: float,
peak_comparison: float,
peak_error: float,
peak_time_error_ms: float,
correlation: float,
r_squared: float,
sprague_geers_magnitude: float,
sprague_geers_phase: float,
sprague_geers_comprehensive: float,
russell_magnitude: float,
russell_phase: float,
russell_comprehensive: float,
)
Classic error measures between a reference and a comparison.
peak_reference
instance-attribute
¶
Reference value at its largest magnitude (signed).
peak_comparison
instance-attribute
¶
Comparison value at its largest magnitude (signed).
peak_error
instance-attribute
¶
(|peak comparison| - |peak reference|) / |peak reference|:
+0.10 means the comparison peaks 10 % higher.
peak_time_error_ms
instance-attribute
¶
Time of the comparison's peak minus the reference's, ms.
r_squared
instance-attribute
¶
Coefficient of determination of the comparison as a prediction
of the reference, 1 - SS_res / SS_tot (can be negative).
sprague_geers_magnitude
instance-attribute
¶
Sprague & Geers magnitude error M (0 for equal energy).
sprague_geers_phase
instance-attribute
¶
Sprague & Geers phase error P, 0 to 1.
sprague_geers_comprehensive
instance-attribute
¶
Sprague & Geers comprehensive error C = sqrt(M^2 + P^2).
russell_phase
instance-attribute
¶
Russell phase error RP (the same as Sprague & Geers P).
russell_comprehensive
instance-attribute
¶
Russell comprehensive error RC = sqrt(pi/4 (RM^2 + RP^2)).
Comparison
dataclass
¶
Comparison(
iso: ISO18571Result,
cc: CCResult,
errors: ErrorMeasures,
channel: str = "",
)
resample_pair
¶
resample_pair(
time: ArrayLike,
reference: ArrayLike,
comparison: ArrayLike,
comparison_time: ArrayLike | None = None,
t_start: float | None = None,
t_end: float | None = None,
dt: float = 0.0001,
) -> tuple[Array, Array, Array]
Return both signals on one uniform time grid.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
time
|
ArrayLike
|
Time of the reference signal, s. |
required |
reference
|
ArrayLike
|
The two signals. |
required |
comparison
|
ArrayLike
|
The two signals. |
required |
comparison_time
|
ArrayLike | None
|
Time of the comparison signal, s, if it differs from |
None
|
t_start
|
float | None
|
Interval of evaluation, s. Default: the interval both signals cover. |
None
|
t_end
|
float | None
|
Interval of evaluation, s. Default: the interval both signals cover. |
None
|
dt
|
float
|
Time step of the common grid, s (default 0.1 ms = 10 kHz). |
0.0001
|
Returns:
| Type | Description |
|---|---|
(t, reference, comparison)
|
The grid from |
iso18571_grade
¶
Return the ISO/TS 18571 grade of an overall rating: excellent (R > 0.94), good (0.80 < R <= 0.94), fair (0.58 < R <= 0.80) or poor (R <= 0.58).
iso18571
¶
iso18571(
time: ArrayLike,
reference: ArrayLike,
comparison: ArrayLike,
comparison_time: ArrayLike | None = None,
t_start: float | None = None,
t_end: float | None = None,
dt: float = 0.0001,
parameters: ISO18571Parameters | None = None,
) -> ISO18571Result
Return the ISO/TS 18571 objective rating of two signals.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
time
|
ArrayLike
|
Time of the reference signal, s. |
required |
reference
|
ArrayLike
|
Reference signal (usually the test), any unit. |
required |
comparison
|
ArrayLike
|
Signal to rate (usually CAE), same unit. |
required |
comparison_time
|
ArrayLike | None
|
Time of the comparison signal if it differs from |
None
|
t_start
|
float | None
|
Interval of evaluation, s. Default: the interval both signals cover. Choose it to contain the event and little else: long quiet tails raise the corridor score. |
None
|
t_end
|
float | None
|
Interval of evaluation, s. Default: the interval both signals cover. Choose it to contain the event and little else: long quiet tails raise the corridor score. |
None
|
dt
|
float
|
Time step the signals are resampled to (default 0.1 ms, the 10 kHz the standard assumes). |
0.0001
|
parameters
|
ISO18571Parameters | None
|
|
None
|
Returns:
| Type | Description |
|---|---|
ISO18571Result
|
|
Notes
The procedure follows ISO/TS 18571:2024:
- Corridor Z: inner and outer corridors of half width
a0andb0times the reference's absolute maximum; each sample scores 1 inside the inner corridor, 0 outside the outer one and((outer - d) / (outer - inner))^k_zbetween; Z is the average. - Phase E_P: the comparison is shifted sample by sample, up to
max_shiftof the interval, and the shift with the highest Pearson correlation is kept (the smallest one on a tie). E_P falls linearly from 1 at no shift to 0 atmax_shift. - Magnitude E_M: the shifted signals are aligned with dynamic
time warping (squared-difference cost, window 10 % of the
length); the error is
sum|C - T| / sum|T|and E_M falls linearly from 1 at no error to 0 at 0.5. - Slope E_S: central-difference derivatives of the shifted
signals, smoothed with a nine-point moving average; the error is
sum|C' - T'| / sum|T'|and E_S falls linearly to 0 at 2.0. - Rating R = 0.4 Z + 0.2 E_P + 0.2 E_M + 0.2 E_S.
The 2014 edition did not specify the warping window and computed the slope differently; ratings can differ slightly from 2014-based tools.
cc_rating
¶
cc_rating(
time: ArrayLike,
reference: ArrayLike,
comparison: ArrayLike,
comparison_time: ArrayLike | None = None,
t_start: float | None = None,
t_end: float | None = None,
dt: float = 0.0001,
parameters: CCParameters | None = None,
) -> CCResult
Return the corridor and cross-correlation (CC) rating of two signals.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
time
|
ArrayLike
|
As for |
required |
reference
|
ArrayLike
|
As for |
required |
comparison
|
ArrayLike
|
As for |
required |
comparison_time
|
ArrayLike
|
As for |
required |
t_start
|
ArrayLike
|
As for |
required |
t_end
|
ArrayLike
|
As for |
required |
dt
|
ArrayLike
|
As for |
required |
parameters
|
CCParameters | None
|
|
None
|
Returns:
| Type | Description |
|---|---|
CCResult
|
|
Notes
- Corridor: the same corridor method as ISO/TS 18571, sized
from
norm(default the reference's absolute maximum). -
Cross-correlation: the comparison is shifted by up to
1 - int_minof the interval; the shift with the highest normalised cross-correlationK = sum(T C) / sqrt(sum T^2 sum C^2)over the overlap is kept. -
Phase: 1 for a shift up to
d_minof the interval, 0 fromd_max,((d_max - d) / (d_max - d_min))^k_vbetween. - Size:
(smaller / larger)^k_gof the two signal energiessum x^2over the shifted overlap. - Shape:
max(K, 0)^k_p. - Cross-correlation rating: their weighted sum.
- Total:
w_corridor * corridor + w_cross * cross.
An automatic interval of evaluation and standard-deviation corridors
(offered by some tools) are not implemented: give t_start and
t_end.
error_measures
¶
error_measures(
time: ArrayLike,
reference: ArrayLike,
comparison: ArrayLike,
comparison_time: ArrayLike | None = None,
t_start: float | None = None,
t_end: float | None = None,
dt: float = 0.0001,
) -> ErrorMeasures
Return classic error measures between two signals.
Arguments as for iso18571. The Sprague & Geers (2004) and Russell
(1997) errors are computed from the integrals of m^2, c^2
and m c over the interval (m: reference, c: comparison); 0 is a
perfect match.
compare
¶
compare(
time: ArrayLike,
reference: ArrayLike,
comparison: ArrayLike,
comparison_time: ArrayLike | None = None,
t_start: float | None = None,
t_end: float | None = None,
dt: float = 0.0001,
channel: str = "",
iso_parameters: ISO18571Parameters | None = None,
cc_parameters: CCParameters | None = None,
) -> Comparison
Return the ISO/TS 18571 rating, the CC rating and the error measures of one signal pair, over the same interval.
flatten(compare(...), prefix="head_") gives columns such as
head_iso.rating and head_errors.peak_error for a feature
table.