Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Docs #168

Open
wants to merge 26 commits into
base: master
Choose a base branch
from
Open

Docs #168

Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions doc/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@
"python": ("https://docs.python.org/3/", None),
"xarray": ("https://xarray.pydata.org/en/stable/", None),
"numpy": ("https://numpy.org/doc/stable", None),
"scipy": ("https://docs.scipy.org/doc/scipy", None),
"matplotlib": ("https://matplotlib.org/stable/", None),
"dask": ("https://docs.dask.org/en/latest", None),
}
Expand Down
64 changes: 40 additions & 24 deletions xrft/xrft.py
Original file line number Diff line number Diff line change
Expand Up @@ -778,7 +778,7 @@ def cross_spectrum(
**kwargs,
):
"""
Calculates the cross spectra of da1 and da2.
Calculates the cross spectra of `da1` and `da2`.

.. math::
da1' = da1 - \overline{da1};\ \ da2' = da2 - \overline{da2}
Expand All @@ -787,26 +787,39 @@ def cross_spectrum(

Parameters
----------
da1 : `xarray.DataArray`
The data to be transformed
da2 : `xarray.DataArray`
The data to be transformed
da1 : xarray.DataArray
The data to be transformed.
da2 : xarray.DataArray
The data to be transformed.
dim : str or sequence of str, optional
The dimensions along which to take the transformation. If `None`, all
The dimensions along which to take the transformation. If ``None``, all
dimensions will be transformed.
real_dim : str, optional
Real Fourier transform will be taken along this dimension.
scaling : str, optional
If 'density', it will normalize the output to power spectral density
If 'spectrum', it will normalize the output to power spectrum
window_correction : boolean
If True, it will correct for the energy reduction resulting from applying a non-uniform window.
This is the default behaviour of many tools for computing power spectrum (e.g scipy.signal.welch and scipy.signal.periodogram).
If scaling = 'spectrum', correct the amplitude of peaks in the spectrum. This ensures, for example, that the peak in the one-sided power spectrum of a 10 Hz sine wave with RMS**2 = 10 has a magnitude of 10.
If scaling = 'density', correct for the energy (integral) of the spectrum. This ensures, for example, that the power spectral density integrates to the square of the RMS of the signal (ie that Parseval's theorem is satisfied). Note that in most cases, Parseval's theorem will only be approximately satisfied with this correction as it assumes that the signal being windowed is independent of the window. The correction becomes more accurate as the width of the window gets large in comparison with any noticeable period in the signal.
If False, the spectrum gives a representation of the power in the windowed signal.
Note that when True, Parseval's theorem may only be approximately satisfied.
kwargs : dict : see xrft.dft for argument list
If ``'density'``, it will normalize the output to power spectral density.
If ``'spectrum'``, it will normalize the output to power spectrum.
window_correction : boolean, optional, default: False
If ``True``, it will correct for the energy reduction resulting from applying a non-uniform window.
This is the default behaviour of many tools for computing power spectrum
(e.g., :func:`scipy.signal.welch` and :func:`scipy.signal.periodogram`).

- If ``scaling='spectrum'``, correct the amplitude of peaks in the spectrum.
This ensures, for example, that the peak in the one-sided power spectrum
of a 10 Hz sine wave with RMS\ :sup:`2` = 10 has a magnitude of 10.
- If ``scaling='density'``, correct for the energy (integral) of the spectrum.
This ensures, for example, that the power spectral density integrates to the square
of the RMS of the signal (i.e., that Parseval's theorem is satisfied).

Note that in most cases, Parseval's theorem will only be approximately satisfied with this correction
as it assumes that the signal being windowed is independent of the window.
The correction becomes more accurate as the width of the window gets large
in comparison with any noticeable period in the signal.

If ``False``, the spectrum gives a representation of the power in the windowed signal.
Note that when ``window_correction=True``, Parseval's theorem may only be approximately satisfied.
kwargs : dict, optional
See :func:`fft` for argument list.
"""

if not true_phase:
Expand Down Expand Up @@ -864,6 +877,7 @@ def cross_spectrum(
"window_correction can only be applied when windowing is turned on."
)
else:
# FIXME: `da` not defined
windows, _ = _apply_window(da, dim, window_type=kwargs.get("window"))
cs = cs / (windows ** 2).mean()
fs = np.prod([float(cs[d].spacing) for d in updated_dims])
Expand All @@ -875,6 +889,7 @@ def cross_spectrum(
"window_correction can only be applied when windowing is turned on."
)
else:
# FIXME: `da` not defined
windows, _ = _apply_window(da, dim, window_type=kwargs.get("window"))
cs = cs / windows.mean() ** 2
fs = np.prod([float(cs[d].spacing) for d in updated_dims])
Expand All @@ -887,10 +902,10 @@ def cross_spectrum(


def cross_phase(da1, da2, dim=None, true_phase=False, **kwargs):
"""
Calculates the cross-phase between da1 and da2.
r"""
roxyboy marked this conversation as resolved.
Show resolved Hide resolved
Calculates the cross-phase between `da1` and `da2`.

Returned values are in [-pi, pi].
Returned values are in :math:`[-\pi, -\pi]`.

.. math::
da1' = da1 - \overline{da1};\ \ da2' = da2 - \overline{da2}
Expand All @@ -899,11 +914,12 @@ def cross_phase(da1, da2, dim=None, true_phase=False, **kwargs):

Parameters
----------
da1 : `xarray.DataArray`
The data to be transformed
da2 : `xarray.DataArray`
The data to be transformed
kwargs : dict : see xrft.dft for argument list
da1 : xarray.DataArray
The data to be transformed.
da2 : xarray.DataArray
The data to be transformed.
kwargs : dict, optional
See :func:`cross_spectrum` for argument list.
"""
if not true_phase:
msg = (
Expand Down