get_power¶
-
powerbox.tools.
get_power
(deltax, boxlength, deltax2=None, N=None, a=1.0, b=1.0, remove_shotnoise=True, vol_normalised_power=True, bins=None, res_ndim=None, weights=None, weights2=None, dimensionless=True, bin_ave=True, get_variance=False, log_bins=False, ignore_zero_mode=False, k_weights=1)[source]¶ Calculate the isotropic power spectrum of a given field, or cross-power of two similar fields.
This function, by default, conforms to typical cosmological power spectrum conventions – normalising by the volume of the box and removing shot noise if applicable. These options are configurable.
Parameters: - deltax : array-like
The field on which to calculate the power spectrum . Can either be arbitrarily n-dimensional, or 2-dimensional with the first being the number of spatial dimensions, and the second the positions of discrete particles in the field. The former should represent a density field, while the latter is a discrete sampling of a field. This function chooses which to use by checking the value of N (see below). Note that if a discrete sampling is used, the power spectrum calculated is the “overdensity” power spectrum, i.e. the field re-centered about zero and rescaled by the mean.
- boxlength : float or list of floats
The length of the box side(s) in real-space.
- deltax2 : array-like
If given, a box of the same shape as deltax, against which deltax will be cross correlated.
- N : int, optional
The number of grid cells per side in the box. Only required if deltax is a discrete sample. If given, the function will assume a discrete sample.
- a,b : float, optional
These define the Fourier convention used. See
powerbox.dft
for details. The defaults define the standard usage in cosmology (for example, as defined in Cosmological Physics, Peacock, 1999, pg. 496.). Standard numerical usage (eg. numpy) is (a,b) = (0,2pi).- remove_shotnoise : bool, optional
Whether to subtract a shot-noise term after determining the isotropic power. This only affects discrete samples.
- vol_weighted_power : bool, optional
Whether the input power spectrum,
pk
, is volume-weighted. Default True because of standard cosmological usage.- bins : int or array, optional
Defines the final k-bins output. If None, chooses a number based on the input resolution of the box. Otherwise, if int, this defines the number of kbins, or if an array, it defines the exact bin edges.
- res_ndim : int, optional
Only perform angular averaging over first res_ndim dimensions. By default, uses all dimensions.
- weights, weights2 : array-like, optional
If deltax is a discrete sample, these are weights for each point.
- dimensionless: bool, optional
Whether to normalise the cube by its mean prior to taking the power.
- bin_ave : bool, optional
Whether to return the bin co-ordinates as the (weighted) average of cells within the bin (if True), or the linearly spaced edges of the bins
- get_variance : bool, optional
Whether to also return an estimate of the variance of the power in each bin.
- log_bins : bool, optional
Whether to create bins in log-space.
- ignore_zero_mode : bool, optional
Whether to ignore the k=0 mode (or DC term).
- k_weights : nd-array, optional
The weights of the n-dimensional k modes. This can be used to filter out some modes completely.
Returns: - p_k : array
The power spectrum averaged over bins of equal \(|k|\).
- meank : array
The bin-centres for the p_k array (in k). This is the mean k-value for cells in that bin.
- var : array
The variance of the power spectrum, estimated from the mean standard error. Only returned if get_variance is True.
Examples
One can use this function to check whether a box created with
PowerBox
has the correct power spectrum:>>> from powerbox import PowerBox >>> import matplotlib.pyplot as plt >>> pb = PowerBox(250,lambda k : k**-2.) >>> p,k = get_power(pb.delta_x,pb.boxlength) >>> plt.plot(k,p) >>> plt.plot(k,k**-2.) >>> plt.xscale('log') >>> plt.yscale('log')