power2d¶
- power2d(dx_lon, dx_time, y, /, axis_lon=- 1, axis_time=0, **kwargs)[source]¶
Return the spectral decomposition of a real-valued array along an arbitrary axis.
- Parameters
dx_lon (float or array-like) – Longitude or cyclic dimension step size in physical units. Used to scale
fx_lon
.dx_time (float or array-like) – Time dimension step size in physical units. Used to scale
fx_time
.y (array-like) – The input data.
axis_lon (int, optional) – Location of the cyclic “space” axis, generally longitude.
axis_time (int, optional) – Location of the “time” axis.
wintype (str or (str, float), optional) – The window specification, passed to
window
. The resulting weights are used to window the data before carrying out spectral decompositions. See notes for details.nperseg (int, optional) – The time dimension window or segment length, passed to
window
. IfNone
, windowing is not carried out. See notes for details.detrend ({‘constant’, ‘linear’}, optional) – Passed as the
type
argument toscipy.signal.detrend
.'constant'
removes the mean and'linear'
removes the linear trend.
- Returns
fx_lon (array-like) – Frequencies for longitude or cyclic axis. Units are the inverse of
dx_lon
.fx_time (array-like) – Frequencies for time axis. Units are the inverse of the
dx_time
.P (array-like) – Power spectrum. Units are the input units squared.
Notes
The Fourier coefficients are scaled so that total variance is equal to one half the sum of the right-hand coefficients. This is more natural for the real-valued datasets typically used by physical scientists, and matches the convention from Professor Elizabeth Barnes’s objective analysis course notes. This differs from the numpy convention, which scales the coefficients so that total variance is equal to the sum of squares of all coefficients, or twice the right-hand coefficients.
Windowing is carried out by applying the
wintype
weights to successive time segments of lengthnperseg
(overlapping by one half the window length), taking spectral decompositions of each weighted segment, then taking the average of the result for all segments. Note that non-boxcar windowing reduces the total power amplitude and results in loss of information. It may often be preferable to follow the example of [RH91] and smooth in frequency space with a Gaussian filter after the decomposition has been carried out.The below example shows that the extent of power reduction resulting from non-boxcar windowing depends on the character of the signal.
>>> import numpy as np >>> import climopy as climo >>> state = np.random.RandomState(51423) >>> w = climo.window(200, 'hanning') >>> y1 = np.sin(np.arange(0, 8 * np.pi - 0.01, np.pi / 25)) # basic signal >>> y2 = state.rand(200) # complex signal >>> for y in (y1, y2): ... yvar = y.var() ... Y = (np.abs(np.fft.fft(y)[1:] / y.size) ** 2).sum() ... Yw = (np.abs(np.fft.fft(y * w)[1:] / y.size) ** 2).sum() ... print('Boxcar', Y / yvar) ... print('Hanning', Yw / yvar) Boxcar 1.0 Hanning 0.375 Boxcar 0.9999999999999999 Hanning 0.5728391743988162
References
- RH91
William J. Randel and Isaac M. Held. Phase Speed Spectra of Transient Eddy Fluxes and Critical Layer Absorption. Journal of the Atmospheric Sciences, 48(5):688–697, March 1991. doi:10.1175/1520-0469(1991)048<0688:PSSOTE>2.0.CO;2.