The timesignal module

A module for time signal handling

Warning

This module is not considered finalized even though it is part of pylife-2.0. Breaking changes might occur in upcoming minor releases.

class pylife.stress.timesignal.TimeSignalGenerator(sample_rate, sine_set, gauss_set, log_gauss_set)[source]

Generates mixed time signals

The generated time signal is a mixture of random sets of sinus signals

For each set the user supplys a dict describing the set:

sinus_set = {
    'number': number of signals
    'amplitude_median':
    'amplitude_std_dev':
    'frequency_median':
    'frequency_std_dev':
    'offset_median':
    'offset_std_dev':
}

The amplitudes (\(A\)), fequencies (\(\omega\)) and offsets (\(c\)) are then norm distributed. Each sinus signal looks like

\(s = A \sin(\omega t + \phi) + c\)

where \(phi\) is a random value between 0 and \(2\pi\).

So the whole sinus \(S\) set is given by the following expression:

\(S = \sum^n_i A_i \sin(\omega_i t + \phi_i) + c_i\).

query(sample_num)[source]

Gets a sample chunk of the time signal

Parameters:

sample_num (int) – number of the samples requested

Returns:

samples – the requested samples

Return type:

1D numpy.ndarray

You can query multiple times, the newly delivered samples will smoothly attach to the previously queried ones.

reset()[source]

Resets the generator

A resetted generator behaves like a new generator.

pylife.stress.timesignal.butter_bandpass(df, lowcut, highcut, order=5)[source]

Use the functonality of scipy

Parameters:
  • df (DataFrame) –

  • lowcut (float) – low frequency

  • highcut (float) – high freqency.

  • order (int, optional) – Butterworth filter order. The default is 5.

Returns:

TSout

Return type:

DataFrame

pylife.stress.timesignal.clean_timeseries(df, comparison_column, window_size=1000, overlap=800, feature='abs_energy', method='keep', n_gridpoints=3, percentage_max=0.05, order=3)[source]

Removes segments of the data in which the extracted feature value is lower as percentage_max and fills the gaps with polynomial regression

Parameters:
  • df (input pandas DataFrame that shall be cleaned) –

  • comparison_column (str, column that is used for the feature) – comparison with percentage max

  • window_size (int, optional) – window size of the rolled segments - The default is 1000.

  • overlap (int, optional) – overlap between 2 adjecent windows -The default is 200.

  • feature (string, optional) – extracted feature - only supports one at a time - and only features form tsfresh that dont need extra parameters. The default is “maximum”.

  • method (string, optional) –

    • ‘keep’: keeps the windows which are extracted,

    • ’remove’: removes the windows which are extracted

  • n_gridpoints (TYPE, optional) – number of gridpoints. The default is 3.

  • percentage_max (float, optional) – min percentage of the maximum to keep the window. The default is 0.05.

  • order (int, optional) – order of polynom The default is 3.

Returns:

df_poly – cleaned DataFrame

Return type:

pandas DataFrame

pylife.stress.timesignal.fs_calc(df)[source]

Calculates the sample frequency of a DataFrame time series

Parameters:

df (DataFrame) – time series.

Returns:

fs – sample freqency

Return type:

int, float

pylife.stress.timesignal.psd_df(df_ts, NFFT=512)[source]

calculates the psd using Welch algorithm from matplotlib functionality

Parameters:
  • df_ts (DataFram) – time series dataframe

  • NFFT (int, optional) – BufferSize. The default is 512.

Returns:

df_psd – PSD.

Return type:

DataFrame

pylife.stress.timesignal.resample_acc(df, fs=1)[source]

Resamples a pandas time series DataFrame

Parameters:
  • df (DataFrame) –

  • time_col (str) – column name of the time column

  • fs (float) – sample rate of the resampled time series

Return type:

DataFrame