File: acfs.py

package info (click to toggle)
python-dynasor 2.1-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 22,008 kB
  • sloc: python: 5,263; sh: 20; makefile: 3
file content (117 lines) | stat: -rw-r--r-- 3,430 bytes parent folder | download
1
2
3
4
5
6
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
"""
A number of utility functions, for example for dealing with
autocorrelation functions, Fourier transforms, and smoothing.
"""

import numpy as np
from scipy.signal import correlate
from numpy.typing import NDArray
import pandas as pd


def compute_acf(Z: NDArray[float], delta_t: float = 1.0, method='scipy'):
    r"""
    Computes the autocorrelation function (ACF) for a one-dimensional signal :math:`Z` in time as

    .. math::

        ACF(\tau) = \frac{\left < Z(t) Z^*(t+\tau) \right >}{\left <  Z(t)  Z^*(t) \right >}

    Here, only the real part of the ACF is returned since if :math:`Z` is complex
    the imaginary part should average out to zero for any stationary signal.

    Parameters
    ----------
    Z
        complex time signal
    delta_t
        spacing in time between two consecutive values in :math:`Z`
    method
        implementation to use; possible values: `numpy` and `scipy` (default and usually faster)
    """

    # keep only real part and normalize
    acf = _compute_correlation_function(Z, Z, method)
    acf = np.real(acf)
    acf /= acf[0]

    time_lags = delta_t * np.arange(0, len(acf), 1)
    return time_lags, acf


def _compute_correlation_function(Z1, Z2, method='scipy'):
    N = len(Z1)
    assert len(Z1) == len(Z2)
    if method == 'scipy':
        cf = correlate(Z1, Z2, mode='full')[N - 1:] / np.arange(N, 0, -1)
    elif method == 'numpy':
        cf = np.correlate(Z1, Z2, mode='full')[N - 1:] / np.arange(N, 0, -1)
    else:
        raise ValueError('method must be either numpy or scipy')
    return cf


# smoothing functions / FFT filters
# -------------------------------------
def gaussian_decay(t: NDArray[float], t_sigma: float):
    r"""
    Evaluates a gaussian distribution in time :math:`f(t)`, which can be applied to an ACF in time
    to artificially damp it, i.e., forcing it to go to zero for long times.

    .. math::

        f(t) = \exp{\left [-\frac{1}{2} \left (\frac{t}{t_\mathrm{sigma}}\right )^2 \right ] }

    Parameters
    ----------
    t
        time array
    t_sigma
        width (standard deviation of the gaussian) of the decay
    """

    return np.exp(- 1 / 2 * (t / t_sigma) ** 2)


def fermi_dirac(t: NDArray[float], t_0: float, t_width: float):
    r"""
    Evaluates a Fermi-Dirac-like function in time :math:`f(t)`, which can be applied to an ACF in
    time to artificially damp it, i.e., forcing it to go to zero for long times without affecting
    the short-time correlations too much.

    .. math::

        f(t) = \frac{1}{\exp{[(t-t_0)/t_\mathrm{width}}] + 1}

    Parameters
    ----------
    t
        time array
    t_0
        starting time for decay
    t_width
        width of the decay

    """
    return 1.0 / (np.exp((t - t_0) / t_width) + 1)


def smoothing_function(data: NDArray[float], window_size: int, window_type: str = 'hamming'):
    """
    Smoothing function for 1D arrays.
    This functions employs pandas rolling window average

    Parameters
    ----------
    data
        1D data array
    window_size
        The size of smoothing/smearing window
    window_type
        What type of window-shape to use, e.g. ``'blackman'``, ``'hamming'``, ``'boxcar'``
        (see pandas and scipy documentaiton for more details)

    """
    series = pd.Series(data)
    new_data = series.rolling(window_size, win_type=window_type, center=True, min_periods=1).mean()
    return np.array(new_data)