This module contains implementations of algorithms for time series analysis. These algorithms include:
1. Spectral estimation: calculate the spectra of time-series and cross-spectra between time-series.
get_spectra(), get_spectra_bi(), periodogram(), periodogram_csd(), DPSS_windows(), multi_taper_psd(), multi_taper_csd(), mtm_cross_spectrum()
2. Coherency: calculate the pairwise correlation between time-series in the frequency domain and related quantities.
coherency(), coherence(), coherence_regularized(), coherency_regularized(), coherency_bavg(), coherence_bavg(), coherence_partial(), coherence_partial_bavg(), coherency_phase_spectrum(), coherency_phase_delay(), coherency_phase_delay_bavg(), correlation_spectrum()
3. Event-related analysis: calculate the correlation between time-series and external events.
freq_domain_xcorr(), freq_domain_xcorr_zscored(), fir()
4. Cached coherency: A set of special functions for quickly calculating coherency in large data-sets, where the calculation is done over only a subset of the adjacency matrix edges and intermediate calculations are cached, in order to save calculation time.
cache_fft(), cache_to_psd(), cache_to_phase(), cache_to_relative_phase(), cache_to_coherency().
wmorlet(), wfmorlet_fft(), wlogmorlet(), wlogmorlet_fft()
The algorithms in this library are the functional form of the algorithms, which accept as inputs numpy array and produce numpy array outputs. Therfore, they can be used on any type of data which can be represented in numpy arrays. See also nitime.analysis for simplified analysis interfaces, using the data containers implemented in nitime.timeseries
Returns the Discrete Prolate Spheroidal Sequences of orders [0,Kmax-1] for a given frequency-spacing multiple NW and sequence length N.
| Returns : | v,e : tuple,
|
|---|
Notes
Tridiagonal form of DPSS calculation from:
Slepian, D. Prolate spheroidal wave functions, Fourier analysis, and uncertainty V: The discrete case. Bell System Technical Journal, Volume 57 (1978), 1371430
Finds the parameters for an autoregressive model of order norder of the process s. Using these parameters, an estimate of the PSD is calculated from [-PI,PI) in Nfreqs, or [0,PI] in {N/2+1}freqs. Uses the Levinson-Durbin recursion method, and a baised estimate of sxx (unless sxx is provided).
The model for the autoregressive process takes this convention: s[n] = a1*s[n-1] + a2*s[n-2] + ... aP*s[n-P] + v[n]
where v[n] is a zero-mean white noise process with variance=sigma_v
| Parameters : | s : ndarray
order : int
Nfreqs : int
sxx : ndarray (optional)
sides : str (optional)
system : bool (optional)
|
|---|---|
| Returns : | (w, ar_psd) : w : Array of normalized frequences from [-.5, .5) or [0,.5] ar_psd : A PSD estimate computed by sigma_v / |1-a(f)|**2 , where
|
Filters data into a frequency range.
For each of the two bounds, a low-passed version is created by convolving with a box-car and then the low-passed version for the upper bound is added to the low-passed version for the lower bound subtracted from the signal, resulting in a band-passed version
| Parameters : | time_series: float array :
ub : float, optional
lb : float, optional
n_iterations: int, optional :
|
|---|---|
| Returns : | float array: :
|
compute and cache the windowed FFTs of the time_series, in such a way that computing the psd and csd of any combination of them can be done quickly.
| Parameters : | time_series : float array
ij: list of tuples :
lb,ub: float :
method: dict, optional :
|
|---|---|
| Returns : | freqs, cache :
|
Notes
From a set of cached spectra, calculate the coherency relationships
| Parameters : | cache: dict :
ij: list :
|
|---|---|
| Returns : | Cxy: dict :
|
From a set of cached set of windowed fft’s, calculate the frequency-band dependent phase for each of the channels in ij. Note that this returns the absolute phases of the time-series, not the relative phases between them. In order to get relative phases, use cache_to_relative_phase
| Parameters : | cache : dict
ij: list :
|
|---|---|
| Returns : | Phase : dict
|
From a set of cached windowed fft, calculate the psd
| Parameters : | cache : dict
ij : list
|
|---|---|
| Returns : | Pxx : dict
|
From a set of cached set of windowed fft’s, calculate the frequency-band dependent relative phase for the combinations ij.
| Parameters : | cache: dict :
ij: list :
|
|---|---|
| Returns : | Phi_xy : dict
|
Compute the coherence between the spectra of an n-tuple of time_series.
Parameters of this function are in the time domain.
| Parameters : | time_series: float array :
csd_method: dict, optional :
|
|---|---|
| Returns : | f : float array
c : float array
|
See also
Notes
This is an implementation of equation (2) of [Sun2005]:
Coh_{xy}(\lambda) = |{R_{xy}(\lambda)}|^2 = \frac{|{f_{xy}(\lambda)}|^2}{f_{xx}(\lambda) \cdot f_{yy}(\lambda)}
| [Sun2005] | F.T. Sun and L.M. Miller and M. D’Esposito(2005). Measuring temporal dynamics of functional networks using phase spectrum of fMRI data. Neuroimage, 28: 227-37. |
Compute the band-averaged coherence between the spectra of two time series.
Input to this function is in the time domain.
| Parameters : | time_series : float array
lb, ub: float, optional :
csd_method: dict, optional. :
|
|---|---|
| Returns : | c : float
|
Compute the band-averaged coherency between the spectra of two time series. input to this function is in the frequency domain
| Parameters : | fxy : float array
fyy,fxx : float array
|
|---|---|
| Returns : | float : :
|
Compute the coherence between the spectra of two time series.
Parameters of this function are in the frequency domain.
| Parameters : | fxy : array
fyy,fxx : array
|
|---|---|
| Returns : | float : a frequency-band-dependent measure of the linear association
|
See also
Compute the band-specific partial coherence between the spectra of two time series.
The partial coherence is the part of the coherence between x and y, which cannot be attributed to a common cause, r.
Input to this function is in the time domain.
| Parameters : | time_series: float array :
r: float array :
csd_method: dict, optional :
|
|---|---|
| Returns : | f: array, :
c: float array :
|
Notes
This is an implementation of equation (2) of [Sun2004]:
Coh_{xy|r} = \frac{|{R_{xy}(\lambda) - R_{xr}(\lambda) R_{ry}(\lambda)}|^2}{(1-|{R_{xr}}|^2)(1-|{R_{ry}}|^2)}
| [Sun2004] | F.T. Sun and L.M. Miller and M. D’Esposito(2004). Measuring |
interregional functional connectivity using coherence and partial coherence analyses of fMRI data Neuroimage, 21: 647-58.
Compute the band-averaged partial coherence between the spectra of two time series. See coherence_partial().
Input to this function is in the time domain.
| Parameters : | time_series : float array
r : float array
csd_method: dict, optional :
lb: float, optional :
ub: float, optional :
|
|---|---|
| Returns : | c: float :
|
Compute the band-averaged partial coherence between the spectra of two time series.
Input to this function is in the frequency domain.
| Parameters : | f: the frequencies : fxy : float array
fyy,fxx : float array
fxr,fry : float array
|
|---|---|
| Returns : | float :
|
See also
Compute the band-specific partial coherence between the spectra of two time series. See partial_coherence().
Input to this function is in the frequency domain.
| Parameters : | fxy : float array
fyy,fxx : float array
fxr,fry : float array
|
|---|---|
| Returns : | float :
|
Same as coherence, except regularized in order to overcome numerical imprecisions
| Parameters : | time_series: n-d float array :
epsilon: float :
alpha: float :
csd_method: dict, optional. :
|
|---|---|
| Returns : | frequencies, coherence : |
Notes
The regularization scheme is as follows:
C_{x,y} = \frac{(\alpha f_{xx} + \epsilon)^2}{\alpha^{2}((f_{xx}+\epsilon)(f_{yy}+\epsilon))}
A regularized version of the calculation of coherence, which is more robust to numerical noise than the standard calculation.
Input to this function is in the frequency domain
| Parameters : | fxy, fxx, fyy: float arrays :
epsilon: float :
alpha: float :
|
|---|---|
| Returns : | float array :
|
Compute the coherency between the spectra of n-tuple of time series. Input to this function is in the time domain
| Parameters : | time_series: n*t float array :
csd_method: dict, optional. :
|
|---|---|
| Returns : | f : float array
c : float array
|
Notes
This is an implementation of equation (1) of [Sun2005]:
R_{xy} (\lambda) = \frac{f_{xy}(\lambda)} {\sqrt{f_{xx} (\lambda) \cdot f_{yy}(\lambda)}}
| [Sun2005] | F.T. Sun and L.M. Miller and M. D’Esposito(2005). Measuring temporal dynamics of functional networks using phase spectrum of fMRI data. Neuroimage, 28: 227-37. |
Compute the band-averaged coherency between the spectra of two time series.
Input to this function is in the time domain.
| Parameters : | time_series: n*t float array :
lb, ub: float, optional :
csd_method: dict, optional. :
|
|---|---|
| Returns : | c: float array :
|
Notes
This is an implementation of equation (A4) of [Sun2005]:
\bar{Coh_{xy}} (\bar{\lambda}) = \frac{\left|{\sum_\lambda{\hat{f_{xy}}}}\right|^2} {\sum_\lambda{\hat{f_{xx}}}\cdot sum_\lambda{\hat{f_{yy}}}}
| [Sun2005] | F.T. Sun and L.M. Miller and M. D’Esposito(2005). Measuring temporal dynamics of functional networks using phase spectrum of fMRI data. Neuroimage, 28: 227-37. |
Compute the band-averaged coherency between the spectra of two time series.
Input to this function is in the frequency domain.
| Parameters : | fxy : float array
fyy,fxx : float array
|
|---|---|
| Returns : | float :
|
Notes
This is an implementation of equation (A4) of [Sun2005]:
\bar{Coh_{xy}} (\bar{\lambda}) = \frac{\left|{\sum_\lambda{\hat{f_{xy}}}}\right|^2} {\sum_\lambda{\hat{f_{xx}}}\cdot sum_\lambda{\hat{f_{yy}}}}
| [Sun2005] | F.T. Sun and L.M. Miller and M. D’Esposito(2005). Measuring temporal dynamics of functional networks using phase spectrum of fMRI data. Neuroimage, 28: 227-37. |
Compute the coherency between the spectra of two time series.
Input to this function is in the frequency domain.
| Parameters : | fxy : float array
fyy,fxx : float array
|
|---|---|
| Returns : | complex array :
|
See also
The temporal delay calculated from the coherency phase spectrum.
| Parameters : | time_series: float array :
lb, ub: float :
csd_method : dict, optional.
|
|---|---|
| Returns : | f : float array
p : float array
|
Band-averaged phase delay between time-series
| Parameters : | time_series: float array :
lb,ub : float, optional
|
|---|---|
| Returns : | p : float array
|
Compute the band-averaged phase delay between the spectra of two signals
| Parameters : | f: float array :
fxy : float array
|
|---|---|
| Returns : | float :
|
Compute the phase delay between the spectra of two signals. The input to this function is in the frequency domain.
| Parameters : | f: float array :
fxy : float array
|
|---|---|
| Returns : | float array :
|
Compute the phase spectrum of the cross-spectrum between two time series.
The parameters of this function are in the time domain.
| Parameters : | time_series: n*t float array : The time series, with t, time, as the last dimension : |
|---|---|
| Returns : | f: mid frequencies of the bands : p: an array with the pairwise phase spectrum between the time : series, where p[i][j] is the phase spectrum between time series[i] and : time_series[j] : |
Notes
This is an implementation of equation (3) of Sun et al. (2005) [Sun2005]:
\phi(\lambda) = arg [R_{xy} (\lambda)] = arg [f_{xy} (\lambda)]
| [Sun2005] | F.T. Sun and L.M. Miller and M. D’Esposito(2005). Measuring temporal dynamics of functional networks using phase spectrum of fMRI data. Neuroimage, 28: 227-37. |
Compute the phase spectrum of the cross-spectrum between two time series.
The parameters of this function are in the frequency domain.
| Parameters : | fxy : float array
|
|---|---|
| Returns : | float :
|
Notes
This is an implementation of equation (3) of Sun et al. (2005) [Sun2005]:
\phi(\lambda) = arg [R_{xy} (\lambda)] = arg [f_{xy} (\lambda)]
| [Sun2005] | F.T. Sun and L.M. Miller and M. D’Esposito(2005). Measuring temporal dynamics of functional networks using phase spectrum of fMRI data. Neuroimage, 28: 227-37. |
Compute a regularized measure of the coherence.
Regularization may be needed in order to overcome numerical imprecisions
| Parameters : | time_series: float array :
epsilon: float :
alpha: float :
csd_method: dict, optional. :
|
|---|---|
| Returns : | f: float array :
c: float array :
|
Notes
The regularization scheme is as follows:
Coh_{xy}^R = \frac{(\alpha f_{xx} + \epsilon) ^2}{\alpha^{2}(f_{xx}+\epsilon)(f_{yy}+\epsilon)}
A regularized version of the calculation of coherency, which is more robust to numerical noise than the standard calculation
Input to this function is in the frequency domain.
| Parameters : | fxy, fxx, fyy: float arrays :
epsilon: float :
alpha: float :
|
|---|---|
| Returns : | float array :
|
Calculate the spectral decomposition of the correlation.
| Parameters : | x1,x2: ndarray :
Fs: float, optional :
norm: bool, optional :
|
|---|---|
| Returns : | ccn: ndarray :
f: ndarray :
|
Notes
This method is described in full in [Cordes2000]
| [Cordes2000] | D Cordes, V M Haughton, K Arfanakis, G J Wendt, P A Turski, |
C H Moritz, M A Quigley, M E Meyerand (2000). Mapping functionally related regions of brain with functional connectivity MR imaging. AJNR American journal of neuroradiology 21:1636-44
Calculate the FIR (finite impulse response) HRF, according to [Burock2000]
| Parameters : | timeseries : float array
design : int array
|
|---|---|
| Returns : | HRF: float array :
|
Notes
Implements equation 4 in[Burock2000]_:
\hat{h} = (X^T X)^{-1} X^T y
| [Burock2000] | M.A. Burock and A.M.Dale (2000). Estimation and Detection of Event-Related fMRI Signals with Temporally Correlated Noise: A Statistically Efficient and Unbiased Approach. Human Brain Mapping, 11:249-260 |
Calculates the event related timeseries, using a cross-correlation in the frequency domain.
| Parameters : | tseries: float array :
events: float array :
t_before: float :
t_after: float :
Fs: float :
|
|---|---|
| Returns : | xcorr: float array :
|
Calculates the z-scored event related timeseries, using a cross-correlation in the frequency domain.
| Parameters : | tseries: float array :
events: float array :
t_before: float :
t_after: float :
Fs: float :
|
|---|---|
| Returns : | xcorr: float array : The correlation function between the tseries and the events. Can be : interperted as a linear filter from events to responses (the time-series) : of an LTI. Because it is normalized to its own mean and variance, it can be : interperted as measuring statistical significance relative to all : time-shifted versions of the events. : |
Gaussian white noise.
XXX - incomplete.
Compute the spectra of an n-tuple of time series and all of the pairwise cross-spectra.
| Parameters : | time_series: float array :
method: dict, optional :
|
|---|---|
| Returns : | f: float array :
fxy: float array :
|
Computes the spectra of two timeseries and the cross-spectrum between them
| Parameters : | x,y : float arrays
method: dict, optional :
|
|---|---|
| Returns : | f: float array :
fxx: float array :
fyy: float array :
fxy: float array :
|
The cross-spectrum between two tapered time-series, derived from a multi-taper spectral estimation.
| Parameters : | tx, ty: ndarray (K, ..., N) :
weights: ndarray, or 2-tuple or list :
sides: str in {‘onesided’, ‘twosided’} :
|
|---|
Notes
spectral densities are always computed as S_{xy}^{mt}(f) = \frac{\sum_k [d_k^x(f)y_k^x(f)][d_k^y(f)(y_k^y(f))^{*}]}{[\sum_k d_k^x(f)^2]^{\frac{1}{2}}[\sum_k d_k^y(f)^2]^{\frac{1}{2}}}
Returns an estimate of the Cross Spectral Density (CSD) function between all (N choose 2) pairs of timeseries in s, using the multitaper method. If the NW product, or the BW and Fs in Hz are not specified by the user, a bandwidth of 4 times the fundamental frequency, corresponding to NW = 4 will be used.
| Parameters : | s : ndarray
Fs: float, Sampling rate of the signal : BW: float, The bandwidth of the windowing function will determine the number :
adaptive : {True, False}
low_bias : {True, False}
sides : str (optional) [ ‘default’ | ‘onesided’ | ‘twosided’ ]
|
|---|---|
| Returns : | (freqs, csd_est) : ndarrays
|
Returns an estimate of the PSD function of s using the multitaper method. If the NW product, or the BW and Fs in Hz are not specified by the user, a bandwidth of 4 times the fundamental frequency, corresponding to NW = 4 will be used.
| Parameters : | s : ndarray
Fs: float, Sampling rate of the signal : BW: float, The bandwidth of the windowing function will determine the number :
adaptive : {True/False}
jackknife : {True/False}
low_bias : {True/False}
sides : str (optional) [ ‘default’ | ‘onesided’ | ‘twosided’ ]
|
|---|---|
| Returns : | (freqs, psd_est, ssigma_or_nu) : ndarrays
|
Returns the frequency response of the IIR or FIR filter described by beta and alpha coefficients.
| Parameters : | b : beta sequence (moving average component) a : alpha sequence (autoregressive component) Nfreqs : size of frequency grid sides : {‘onesided’, ‘twosided’}
|
|---|---|
| Returns : | fgrid, H(e^jw) : |
Notes
For a description of the linear constant-coefficient difference equation, see http://en.wikipedia.org/wiki/Z-transform#Linear_constant-coefficient_difference_equation
Takes an N-point periodogram estimate of the PSD function. The number of points N, or a precomputed FFT Sk may be provided. By default, the PSD function returned is normalized so that the integral of the PSD is equal to the mean squared amplitude (mean energy) of s (see Notes).
| Parameters : | s : ndarray
Fs: float (optional) :
Sk : ndarray (optional)
N : int (optional)
sides : str (optional) [ ‘default’ | ‘onesided’ | ‘twosided’ ]
PSD normalize : boolean (optional, default=True) Normalizes the PSD |
|---|---|
| Returns : | (f, psd): tuple :
|
Notes
setting dw = 2*PI/N, then the integral from -PI, PI (or 0,PI) of PSD/(2PI) will be nearly equal to sxx(0), where sxx is the autocovariance function of s(n). By definition, sxx(0) = E{s(n)s*(n)} ~ (s*s.conj()).mean()
Takes an N-point periodogram estimate of all the cross spectral density functions between rows of s.
The number of points N, or a precomputed FFT Sk may be provided. By default, the CSD function returned is normalized so that the integral of the PSD is equal to the mean squared amplitude (mean energy) of s (see Notes).
| Returns : | (freqs, csd_est) : ndarrays
|
|---|
Notes
setting dw = 2*PI/N, then the integral from -PI, PI (or 0,PI) of PSD/(2PI) will be nearly equal to sxy(0), where sxx is the crosscovariance function of s1(n), s2(n). By definition, sxy(0) = E{s1(n)s2*(n)} ~ (s1*s2.conj()).mean()
returns a complex morlet wavelet in the frequency domain
| Parameters : | f0 : center frequency
|
|---|
returns a complex log morlet wavelet in the time domain
| Parameters : | f0 : center frequency
|
|---|
returns a complex log morlet wavelet in the frequency domain
| Parameters : | f0 : center frequency
|
|---|
returns a complex morlet wavelet in the time domain
| Parameters : | f0 : center frequency
|
|---|
Finds the parameters for an autoregressive model of order norder of the process s. Using these parameters, an estimate of the PSD is calculated from [-PI,PI) in Nfreqs, or [0,PI] in {N/2+1}freqs. Uses the basic Yule Walker system of equations, and a baised estimate of sxx (unless sxx is provided).
The model for the autoregressive process takes this convention: s[n] = a1*s[n-1] + a2*s[n-2] + ... aP*s[n-P] + v[n]
where v[n] is a zero-mean white noise process with variance=sigma_v
| Parameters : | s : ndarray
order : int
Nfreqs : int
sxx : ndarray (optional)
sides : str (optional)
system : bool (optional)
|
|---|---|
| Returns : | (w, ar_psd) : w : Array of normalized frequences from [-.5, .5) or [0,.5] ar_psd : A PSD estimate computed by sigma_v / |1-a(f)|**2 , where
|