This module contains nodes for spectral analysis with Timeflux.
spectral
- class timeflux_dsp.nodes.spectral.FFT(fs=1.0, nfft=None, return_onesided=True)[source]
Bases:
timeflux.core.node.Node
Compute the one-dimensional discrete Fourier Transform for each column using the Fast Fourier Tranform algorithm.
- Variables
i (Port) – default input, expects DataFrame.
o (Port) – default output, provides DataArray.
Example
In this exemple, we simulate a white noise and we apply FFT:
fs
= 10.0nfft
= 5return_onesided
= False
self.i.data:
A B C 2017-12-31 23:59:59.998745401 0.185133 0.541901 0.872946 2018-01-01 00:00:00.104507143 0.732225 0.806561 0.658783 2018-01-01 00:00:00.202319939 0.692277 0.849196 0.249668 2018-01-01 00:00:00.300986584 0.489425 0.221209 0.987668 2018-01-01 00:00:00.396560186 0.944059 0.039427 0.705575
self.o.data:
xarray.DataArray (times: 1, freqs: 5, space: 3) array([[[ 3.043119+0.j , 2.458294+0.j , 3.47464 +0.j ], [-0.252884+0.082233j, -0.06265 -1.098709j, 0.29353 +0.478287j], [-0.805843+0.317437j, 0.188256+0.146341j, 0.151515-0.674376j], [-0.805843-0.317437j, 0.188256-0.146341j, 0.151515+0.674376j], [-0.252884-0.082233j, -0.06265 +1.098709j, 0.29353 -0.478287j]]]) Coordinates: * times (times) datetime64[ns] 2018-01-01T00:00:00.396560186 * freqs (freqs) float64 0.0 2.0 4.0 -4.0 -2.0 * space (space) object 'A' 'B' 'C'
Notes
This node should be used after a buffer.
References
- Parameters
fs (float) – Nominal sampling rate of the input data.
nfft (int|None) – Length of the Fourier transform. Default: length of the chunk.
return_onesided (bool) – If True, return a one-sided spectrum for real data. If False return a two-sided spectrum. (Note that for complex data, a two-sided spectrum is always returned.) Default: True.
- class timeflux_dsp.nodes.spectral.Welch(rate=None, closed='right', **kwargs)[source]
Bases:
timeflux.core.node.Node
Estimate power spectral density using Welch’s method.
- Variables
i (Port) – default input, expects DataFrame.
o (Port) – default output, provides DataArray with dimensions (time, freq, space).
Example:
In this exemple, we simulate data with noisy sinus on three sensors (columns a, b, c):
fs
= 100.0nfft
= 24
- node.i.data::
s a b c 1970-01-01 00:00:00.000 -0.233920 -0.343296 0.157988 1970-01-01 00:00:00.010 0.460353 0.777296 0.957201 1970-01-01 00:00:00.020 0.768459 1.234923 1.942190 1970-01-01 00:00:00.030 1.255393 1.782445 2.326175 … … … … 1970-01-01 00:00:01.190 1.185759 2.603828 3.315607
node.o.data:
<xarray.DataArray (time: 1, freq: 13, space: 3)> array([[[2.823924e-02, 1.087382e-01, 1.153163e-01], [1.703466e-01, 6.048703e-01, 6.310628e-01], ... ... ... [9.989429e-04, 8.519226e-04, 7.769918e-04], [1.239551e-03, 7.412518e-04, 9.863335e-04], [5.382880e-04, 4.999334e-04, 4.702757e-04]]]) Coordinates: * time (time) datetime64[ns] 1970-01-01T00:00:01.190000 * freq (freq) float64 0.0 4.167 8.333 12.5 16.67 ... 37.5 41.67 45.83 50.0 * space (space) object 'a' 'b' 'c'
Notes
This node should be used after a Window with the appropriate length, with regard to the parameters noverlap, nperseg and nfft. It should be noted that a pipeline such as {LargeWindow-Welch} is in fact equivalent to a pipeline {SmallWindow-FFT-LargeWindow-Average} with SmallWindow ‘s parameters length and step respectively equivalent to nperseg and step and with FFT node with same kwargs.
- Parameters
rate (float|None) – Nominal sampling rate of the input data. If None, the rate will be taken from the input meta/
closed (str) – Make the index closed on the right, left or center.
kwargs – Keyword arguments to pass to scipy.signal.welch function. You can specify: window, nperseg, noverlap, nfft, detrend, return_onesided and scaling.
- class timeflux_dsp.nodes.spectral.Bands(bands=None, relative=False)[source]
Bases:
timeflux.core.node.Node
Averages the XArray values over freq dimension according to the frequencies bands given in arguments.
This node selects a subset of values over the chosen dimensions, averages them along this axis and convert the result into a flat dataframe. This node will output as many ports bands as given bands, with their respective name as suffix.
- Attributes:
i (Port): default output, provides DataArray with 3 dimensions (time, freq, space). o (Port): Default output, provides DataFrame. o_* (Port): Dynamic outputs, provide DataFrame.
- Parameters
bands (dict) – Define the band to extract given its name and its range. An output port will be created with the given names as suffix.