xrscipy.signal.csd

xrscipy.signal.csd(darray: DataArray, other_darray: DataArray, fs: float = None, seglen: float = None, overlap_ratio: float = 0.5, window: str | tuple | _SupportsArray[dtype[Any]] | _NestedSequence[_SupportsArray[dtype[Any]]] | bool | int | float | complex | bytes | _NestedSequence[bool | int | float | complex | str | bytes] = 'hann', nperseg: int = 256, noverlap: int = None, nfft: int = None, detrend: str | Callable | bool = 'constant', return_onesided: bool = True, dim: str = None, scaling: Literal['density', 'spectrum'] = 'density', mode: str = 'psd') DataArray

Estimate the cross power spectral density, Pxy, using Welch’s method.

Parameters:
  • darray (xarray) – Series of measurement values

  • other_darray (xarray) – Series of measurement values fs : float, optional Sampling frequency of the darray and other_darray (time) series. If not specified, will be calculated it from the sampling step of the specified (or only) dimension.

  • window (str or tuple or array_like, optional) – Desired window to use. If window is a string or tuple, it is passed to get_window to generate the window values, which are DFT-even by default. See get_window for a list of windows and required parameters. If window is array_like it will be used directly as the window and its length must be nperseg. Defaults to a Hann window.

  • seglen (float, optional) – Segment length (i.e. nperseg) in units of the used (e.g. time) dimension.

  • nperseg (int, optional) – Length of each segment. Defaults to None, but if window is str or tuple, is set to 256, and if window is array_like, is set to the length of the window.

  • noverlap (int, optional) – Number of points to overlap between segments. If None, noverlap = np.rint(nperseg * overlap_ratio). Defaults to None.

  • overlap_ratio (float, optional) – Used to calculate noverlap, if it is not specified (see above). Defaults to 0.5.

  • nfft (int, optional) – Length of the FFT used, if a zero padded FFT is desired. If None, the FFT length is nperseg. Defaults to None.

  • detrend (str or function or False, optional) – Specifies how to detrend each segment. If detrend is a string, it is passed as the type argument to the detrend function. If it is a function, it takes a segment and returns a detrended segment. If detrend is False, no detrending is done. Defaults to ‘constant’.

  • return_onesided (bool, optional) – If True, return a one-sided spectrum for real data. If False return a two-sided spectrum. Defaults to True, but for complex data, a two-sided spectrum is always returned.

  • dim (str, optional) – Dimension along which the FFT is computed and sampling step calculated. If the signal is 1D, uses the only dimension, otherwise must be specified.

  • scaling ({ 'density', 'spectrum' }, optional) – Selects between computing the cross spectral density (‘density’) where Pxy has units of V**2/Hz and computing the cross spectrum (‘spectrum’) where Pxy has units of V**2, if darray and other_darray are measured in V and fs is measured in Hz. Defaults to ‘density’.

  • mode (str) – Defines what kind of return values are expected. Options are [‘psd’, ‘complex’, ‘magnitude’, ‘angle’, ‘phase’]. ‘complex’ is equivalent to the output of stft with no padding or boundary extension. ‘magnitude’ returns the absolute magnitude of the STFT. ‘angle’ and ‘phase’ return the complex angle of the STFT, with and without unwrapping, respectively.

Returns:

Pxy – Cross spectral density or cross power spectrum with frequency dimension.

Return type:

xarray.DataArray

Notes

By convention, Pxy is computed with the conjugate FFT of darray multiplied by the FFT of other_darray. If the input series differ in length, the shorter series will be zero-padded to match. An appropriate amount of overlap will depend on the choice of window and on your requirements. For the default Hann window an overlap of 50% is a reasonable trade off between accurately estimating the signal power, while not over counting any of the data. Narrower windows may require a larger overlap.

References