uwacan.spectral.spectrum#
- spectrum(time_data, window=None, scaling='density', nfft=None, detrend=True, samplerate=None, axis=None)[source]#
Compute the power spectrum of time-domain data.
The
spectrumfunction calculates the power spectrum of input time-series data. It supports various input types, includingTimeData,xarray.DataArray, and NumPy arrays. The function applies windowing, detrending, and scaling as specified by the parameters to produce the frequency-domain representation of the data.- Parameters:
- time_data_core.TimeData or xr.DataArray or numpy.ndarray
The input time-domain data to compute the spectrum for. The data can be one of the following:
TimeData: Wrapped time data fromuwacan.xarray.DataArray: An xarray DataArray with a ‘time’ dimension.numpy.ndarray: A NumPy array containing time-series data.
- windowstr or array_like, optional
The window function to apply to the data before computing the FFT. This can be:
A string specifying the type of window to use (e.g.,
"hann","kaiser","blackman").An array-like sequence of window coefficients.
If
None, no window is applied. Default isNone.
- scaling{‘density’, ‘spectrum’, ‘dc-nyquist’} or numeric, optional
Specifies the scaling of the power spectrum. Options include:
'density': Computes the power spectral density.'spectrum': Computes the power spectrum.'dc-nyquist': Halves the output at DC and Nyquist frequencies. Use with a pre-scaled window that takes care to scale the remainder of the single-sided spectrum.any numeric value: The output of the fft will be scaled by this value.
Default is
"density".- nfftint, optional
The number of points to use in the FFT computation. If
None, it defaults to the length of the input data along the specified axis.- detrendbool, default=True
If
True, removes the mean from the data before computing the FFT to reduce spectral leakage. IfFalse, no detrending is performed.- sampleratefloat, optional
The sampling rate of the input data in Hz. Required if
time_datais an numpy array. If not provided, it defaults to 1. This parameter is used to compute the frequency axis and proper density scaling.- axisint, optional
The axis along which to compute the FFT. If
None, the last axis is used. Only used for numpy inputs. This parameter allows flexibility in handling multi-dimensional data.
- Returns:
- _core.FrequencyData or xr.DataArray or numpy.ndarray
The computed power spectrum of the input data. The return type matches the input type:
If
time_datais aTimeData, returns aFrequencyDataobject.If
time_datais anxarray.DataArray, returns anxarray.DataArraywith a ‘frequency’ dimension.If
time_datais anumpy.ndarray, returns a NumPy array containing the power spectrum.