Esempio n. 1
0
def compute_rescaled_range(sig, win_len):
    """Compute rescaled range of a given time series at a given scale.

    Parameters
    ----------
    sig : 1d array
        Time series.
    win_len : int
        Window length for each rescaled range computation, in samples.

    Returns
    -------
    rs : float
        Average rescaled range over windows.
    """

    # Demean signal
    sig = sig - np.mean(sig)

    # Calculate cumulative sum of the signal & split the signal into segments
    segments = split_signal(sp.cumsum(sig), win_len).T

    # Calculate rescaled range as range divided by standard deviation (of non-cumulative signal)
    rs_win = np.ptp(segments, axis=0) / np.std(split_signal(sig, win_len).T,
                                               axis=0)

    # Take the mean across windows
    rs = np.mean(rs_win)

    return rs
Esempio n. 2
0
def compute_detrended_fluctuation(sig, win_len, deg=1):
    """Compute detrended fluctuation of a time series at the given window length.

    Parameters
    ----------
    sig : 1d array
        Time series.
    win_len : int
        Window length for each detrended fluctuation fit, in samples.
    deg : int, optional, default=1
        Polynomial degree for detrending.

    Returns
    -------
    det_fluc : float
        Measured detrended fluctuation, as the average error fits of the window.
    """

    # Calculate cumulative sum of the signal & split the signal into segments
    segments = split_signal(sp.cumsum(sig - np.mean(sig)), win_len).T

    # Calculate local trend, as the line of best fit within the time window
    _, fluc, _, _, _ = np.polyfit(np.arange(win_len),
                                  segments,
                                  deg=deg,
                                  full=True)

    # Convert to root-mean squared error, from squared error
    det_fluc = np.mean((fluc / win_len))**0.5

    return det_fluc
Esempio n. 3
0
def compute_rescaled_range(sig, win_len):
    """Compute rescaled range of a given time series at a given scale.

    Parameters
    ----------
    sig : 1d array
        Time series.
    win_len : int
        Window length for each rescaled range computation, in samples.

    Returns
    -------
    rs : float
        Average rescaled range over windows.

    Notes
    -----
    - Rescaled range was introduced as a measure of time series variability, by Harold Hurst [1]_.

    References
    ----------
    .. [1] https://en.wikipedia.org/wiki/Rescaled_range
    """

    # Demean signal
    sig = sig - np.mean(sig)

    # Calculate cumulative sum of the signal & split the signal into segments
    segments = split_signal(np.cumsum(sig), win_len).T

    # Calculate rescaled range as range divided by standard deviation (of non-cumulative signal)
    rs_win = np.ptp(segments, axis=0) / np.std(split_signal(sig, win_len).T,
                                               axis=0)

    # Take the mean across windows
    rs = np.mean(rs_win)

    return rs
Esempio n. 4
0
def lagged_coherence_1freq(sig, fs, freq, n_cycles):
    """Compute the lagged coherence of a frequency using the hanning-taper FFT method.

    Parameters
    ----------
    sig : 1d array
        Time series.
    fs : float
        Sampling rate, in Hz.
    freq : float
        The frequency at which to estimate lagged coherence.
    n_cycles : float
        Number of cycles at the examined frequency to use to compute lagged coherence.

    Returns
    -------
    float
        The computed lagged coherence value.
    """

    # Determine number of samples to be used in each window to compute lagged coherence
    n_samps = int(np.ceil(n_cycles * fs / freq))

    # Split the signal into chunks
    chunks = split_signal(sig, n_samps)
    n_chunks = len(chunks)

    # For each chunk, calculate the Fourier coefficients at the frequency of interest
    hann_window = hann(n_samps)
    fft_freqs = np.fft.fftfreq(n_samps, 1 / float(fs))
    fft_freqs_idx = np.argmin(np.abs(fft_freqs - freq))

    fft_coefs = np.zeros(n_chunks, dtype=complex)
    for ind, chunk in enumerate(chunks):
        fourier_coef = np.fft.fft(chunk * hann_window)
        fft_coefs[ind] = fourier_coef[fft_freqs_idx]

    # Compute the lagged coherence value
    lcs_num = 0
    for ind in range(n_chunks - 1):
        lcs_num += fft_coefs[ind] * np.conj(fft_coefs[ind + 1])
    lcs_denom = np.sqrt(
        np.sum(np.abs(fft_coefs[:-1])**2) * np.sum(np.abs(fft_coefs[1:])**2))

    return np.abs(lcs_num / lcs_denom)
Esempio n. 5
0
def compute_detrended_fluctuation(sig, win_len, deg=1):
    """Compute detrended fluctuation of a time series at the given window length.

    Parameters
    ----------
    sig : 1d array
        Time series.
    win_len : int
        Window length for each detrended fluctuation fit, in samples.
    deg : int, optional, default=1
        Polynomial degree for detrending.

    Returns
    -------
    det_fluc : float
        Measured detrended fluctuation, as the average error fits of the window.

    Notes
    -----
    - DFA was originally proposed in [1]_.
    - There is a relationship between DFA measures and 1/f exponent, as detailed in [2]_.

    References
    ----------
    .. [1] Peng, C.-K., Buldyrev, S. V., Havlin, S., Simons, M., Stanley, H. E., &
           Goldberger, A. L. (1994). Mosaic organization of DNA nucleotides.
           Physical Review E, 49(2), 1685–1689.
           DOI: https://doi.org/10.1103/PhysRevE.49.1685
    .. [2] https://en.wikipedia.org/wiki/Detrended_fluctuation_analysis
    """

    # Calculate cumulative sum of the signal & split the signal into segments
    segments = split_signal(np.cumsum(sig - np.mean(sig)), win_len).T

    # Calculate local trend, as the line of best fit within the time window
    _, fluc, _, _, _ = np.polyfit(np.arange(win_len),
                                  segments,
                                  deg=deg,
                                  full=True)

    # Convert to root-mean squared error, from squared error
    det_fluc = np.mean((fluc / win_len))**0.5

    return det_fluc