process module

Other time series processing.

interpolate(x, y, new_x, method='linear', **kwargs)

Interpolate function over new set of points.

Supports linear, cubic and spline interpolation.

Parameters:
  • x (array-like) – The x-coordinates of the data points, must be increasing.

  • y (array-like) – The y-coordinates of the data points, same length as x.

  • new_x (array-like) – New x-coordinates at which to evaluate the interpolated values.

  • method (str, default='linear') – Interpolation method. Supported methods are ‘linear’, ‘constant’, ‘cubic’ and ‘spline’.

  • kwargs (dict) – Additional keyword arguments passed to the interpolation function. For more details, see kwargs of numpy and scipy interpolation functions.

repeat(x, y, repeats: int) tuple[ndarray, ndarray]

Extend time series.

Independent variable is appended with the same spacing, dependent variable is copied.

Parameters:
  • x (1-D array-like of size n) – Independent variable in strictly increasing order.

  • y (1-D array-like of size n) – Dependent variable.

  • repeats (int) – How many times repeat time series.

Returns:

  • ndarray – x, repeated independent variable.

  • ndarray – y, repeated dependent variable.

trend(x, y, fun: Callable[[ndarray], ndarray], normalized=False) Tuple[ndarray, ndarray]

Apply long-term trend to time series data using provided function.

Parameters:
  • x (1-D array-like of size n) – Independent variable in strictly increasing order.

  • y (1-D array-like of size n) – Dependent variable.

  • fun (Callable) – Long term trend applied to the data in form of a function. Callable signature is (x) -> y_shift where x independent variable axis and y_shift is independent variable shift for that x.

  • normalized (bool, default: False) – If true, x variable in fun is normalized to the range of [0, 1].

Returns:

  • ndarray – x, independent variable.

  • ndarray – y, shifted dependent variable.

linear_trend(x, y, a, normalized=False)

Adding linear trend to time series.

Parameters:
  • x (1-D array-like of size n) – Independent variable in strictly increasing order.

  • y (1-D array-like of size n) – Dependent variable.

  • a (float) – Linear coefficient.

  • normalized (bool, default: False) – If true, x variable in fun is normalized to the range of [0, 1].

Returns:

  • ndarray – x, independent variable.

  • ndarray – y, shifted dependent variable.

spline_smooth(x: ndarray, y: ndarray, s=None)

Smooth a function y=x using smoothing spline

Value of smoothing s needs to be set empirically by trial and error for specific data.

https://docs.scipy.org/doc/scipy/tutorial/interpolate/smoothing_splines.html

Parameters:
  • x (array_like) – The data points defining a curve y = f(x)

  • y (array_like) – The data points defining a curve y = f(x)

  • s (float, optional) – A smoothing condition. s can be used to control the tradeoff between closeness and smoothness of fit. Larger s means more smoothing while smaller values of s indicate less smoothing. If s is None, it’s ‘good’ value is calculated based on number of samples and standard deviation.

Return type:

BSpline

Notes

If s is not provided, it is calculated as:

\[s = m \sigma^2\]

where \(m\) is the number of samples and \(\sigma\) is the estimated standard deviation.

noise_gauss(a: ndarray | List, snr=None, snr_in_db=True, std=1.0)

Add gaussian noise to the signal.

Add noise targeting provided snr value. If snr is not specified, standard deviation std value is used.

Parameters:
  • a (np.ndarray) – Signal for which noise is inserted.

  • snr (float | list[float] | ndarray[float], optional) – Signal-to-noise ratio; if snr_in_db is True, either is treated in decibels or linear values. It can be provided as scalar or list of floats. If list of floats provided, for each input element of a, corresponding value of snr is considered.

  • snr_in_db (bool, default: True) – Determines whether treat snr in decibels or linear.

  • std (float, default=1.0) – Standard deviation of the noise. Used if snr is not provided.

Returns:

Noised signal.

Return type:

np.ndarray

Notes

Signal-to-noise ratio is defined as:

\[SNR = 10*log_{10}(S/N)\]

where S is signal power and N is noise power.

Gaussian noise has flat power specturm

\[N = var(n) = std(n) ^ 2\]

Signal power is calculated as:

\[E[S^2] = mean(s^2)\]

If snr is in decibels:

\[std(n) = sqrt(mean(s^2) / (10^{SNR_{db}/10}))\]

Else if snr is in linear scale:

\[std(n) = sqrt(mean(s^2) / SNR)\]
average(x, y, interval)

Average time series over n samples

Parameters:
  • x (1-D array-like of size n) – Independent variable in strictly increasing order.

  • y (1-D array-like of size n) – Dependent variable.

  • interval (int) – Interval for which calculate the average value.

Returns:

  • ndarray – x, independent variable.

  • ndarray – y, dependent variable.

truncate(x, y, x_left, x_right, x_left_as_ratio=False, x_right_as_ratio=False)

Truncate time series to specific value range or ratio of the range.

Parameters:
  • x (1-D array-like of size n) – Independent variable in strictly increasing order.

  • y (1-D array-like of size n) – Dependent variable

  • x_left (float) – Value in x array to which truncate arrays from the left. If value is not present in x array, closest lower value is selected.

  • x_right (float) – Value in x array to which truncate arrays from the right. If value is not present in x array, closest higher value is selected.

  • x_left_as_ratio (bool, default: False) – If true, x_left is treated as ratio of the x array to truncate from the left, where 0.0 is the start and 1.0 is the end of the array.

  • x_right_as_ratio (bool, default: False) – Ratio of the x array to truncate from the left, where 0.0 is the start and 1.0 is the end of the array. Ignored, if x_left is not None.

Returns:

  • ndarray – x, truncated independent variable.

  • ndarray – y, truncated dependent variable.

normalize(a, min_val=0, max_val=1)

Normalize array to specific range.

Parameters:
  • a (array-like) – Array of values to normalize.

  • min_val (float, default: 0) – Min value to which normalize array values.

  • max_val (float, default: 1) – Max value to which normalize array values.

Returns:

Normalized array.

Return type:

ndarray