Skip to content

ntsa.tools.statistics

At a glance

Function One-liner
fun_PSD(dt, X) Power spectral density of one or more signals: (f, PSD).
autocorrelation(x, n_lags) Normalized ACF, acf[0]=1.
signal_stats(x, dt, n_bins=64) Dict of moments, PSD, PDF and ACF.

Full reference

ntsa.tools.statistics

Signal statistics: power spectral density, autocorrelation, summary moments.

fun_PSD(dt, X)

Compute the Power Spectral Density of one or more signals.

Parameters:

Name Type Description Default
dt float

Sampling time.

required
X ndarray

Signal(s), shape (Nq, Nt) (1D signals are promoted to (1, Nt); a 2D array is transposed if its first dimension is larger than its second, i.e. the longer axis is assumed to be time).

required

Returns:

Name Type Description
f ndarray

Frequencies, shape (Nt // 2,).

PSD list of np.ndarray

Power Spectral Density of each row of X.

Source code in ntsa/tools/statistics.py
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
def fun_PSD(dt, X):
    """Compute the Power Spectral Density of one or more signals.

    Parameters
    ----------
    dt : float
        Sampling time.
    X : np.ndarray
        Signal(s), shape ``(Nq, Nt)`` (1D signals are promoted to ``(1, Nt)``; a 2D
        array is transposed if its first dimension is larger than its second, i.e.
        the longer axis is assumed to be time).

    Returns
    -------
    f : np.ndarray
        Frequencies, shape ``(Nt // 2,)``.
    PSD : list of np.ndarray
        Power Spectral Density of each row of `X`.
    """
    if X.ndim == 2:
        if X.shape[0] > X.shape[1]:
            X = X.T
    elif X.ndim == 1:
        X = np.expand_dims(X, axis=0)
    else:
        raise AssertionError('X must be 2 dimensional')

    len_x = X.shape[-1]
    f = np.linspace(0.0, 1.0 / (2.0 * dt), len_x // 2)
    PSD = []
    for x in X:
        yt = np.fft.fft(x)
        PSD.append(2.0 / len_x * np.abs(yt[0:len_x // 2]))

    return f, PSD

autocorrelation(x, n_lags)

Normalized autocorrelation for lags 0..n_lags; acf[0] = 1.

Source code in ntsa/tools/statistics.py
44
45
46
47
48
49
def autocorrelation(x, n_lags):
    """Normalized autocorrelation for lags 0..n_lags; acf[0] = 1."""
    x = np.asarray(x, dtype=float)
    x = x - x.mean()
    c = np.correlate(x, x, mode='full')[len(x) - 1:len(x) + n_lags]
    return c / (c[0] if c[0] > 0 else 1.0)

signal_stats(x, dt, n_bins=64)

Summary statistics of a scalar series.

Returns:

Type Description
dict with keys: mean, std, skew, kurtosis, f, psd, pdf_centers, pdf, acf, acf_lags.
Source code in ntsa/tools/statistics.py
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
def signal_stats(x, dt, n_bins=64):
    """Summary statistics of a scalar series.

    Returns
    -------
    dict with keys: mean, std, skew, kurtosis, f, psd, pdf_centers, pdf, acf, acf_lags.
    """
    x = np.asarray(x, dtype=float)
    f, psd = fun_PSD(dt, x)
    counts, edges = np.histogram(x, bins=n_bins, density=True)
    n_lags = len(x) // 4
    return dict(mean=float(x.mean()), std=float(x.std()),
                skew=float(skew(x)), kurtosis=float(kurtosis(x)),
                f=f, psd=psd[0],
                pdf_centers=0.5 * (edges[:-1] + edges[1:]), pdf=counts,
                acf=autocorrelation(x, n_lags), acf_lags=np.arange(n_lags + 1) * dt)