This tutorial covers the comparison between time series signals and the calculation of metrics. Starting from simple subtraction, metrics like root mean squared error for upper and lower boundaries (envelope) are calculated. Finally, step response data is analyzed. The tutorial also illustrates some helper functions for plotting (e.g. fill between two time series).
1 Addition and Subtraction
First we create a time series with two curves between 0 and 5 seconds. The time samples are randomly varied (sample time is not constant). This could be for example results of simulations with an adaptive (variable) step solver. Note that there is a separate tutorial on signal generation.
We calculate the difference between signals ‘a’/‘b’ and the reference. The reference is automatically resampled according to the sample time of signals ‘a’/‘b’, i.e. the option resample_reference is set to True. The option is available in most of the functions in the following but is set to False by default to avoid unnecessary resampling that deteriorates the performance. There is also the option resample_ts to resample the time series ‘a’/‘b’ to the reference time base. Instead of resampling at every function call, consider using resample from trimes.base to align the sample time only once before the comparison if the sample time remains constant.
A diagram with two y-axes is used for visualization (original signals on the left axis, difference on the right). In addition, the area between ‘b’ and ‘reference’ is filled.
A common use case is to check how well a signal aligns with a reference signal. We first create a periodic signal and a reference (note that there is a separate tutorial on signal generation).
comparison_series and comparison_df calculate any error metric (default is integral_abs_error, i.e. area) for series and dataframes. Some metrics are defined in trimes.metrics. Further metrics from the scikit-learn package can be used.
If you are interested in the time a condition is fullfilled (like a signal being outside an envelope), the get_time_bool function calculates the duration from a boolean array and the sampling time.
from trimes.comparison import get_time_boolout = outside_envelope(wave, ts_envelope)get_time_bool(out, ts_envelope.index.values)
np.float64(2.1029999999999998)
3.3 Calculate Metric
Next we calculate metrics such as the area where the wave exceeds the envelope. comparison_series/comparison_df accept an operator as input argument to consider only time spans where the condition is True.
For convenience there are also methods for envelopes instead of a single boundary.
from trimes.comparison import envelope_comparison_series, envelope_comparison_dfenvelope_comparison_series(wave, ts_envelope)envelope_comparison_df(df_waves, ts_envelope)
array([0.77815754, 1.11895373])
4 Step Response Info
trimes provides an interface to the control package to get the step response info (overshoot etc.) of time series. Let’s first create a step response signal.