powerbox.tools.angular_average_nd

powerbox.tools.angular_average_nd(field, coords, bins, n=None, weights=1, average=True, bin_ave=True, get_variance=False)[source]

Take an ND box, and perform a radial average over the first n dimensions.

Parameters:
field : array

An array of arbitrary dimension specifying the field to be angularly averaged.

coords : list of arrays

A list with length equal to the number of dimensions of field. Each entry should be an array specifying the co-ordinates in the corresponding dimension of field. Note this is different from angular_average().

bins : int or array.

Specifies the bins for the averaged dimensions. Can be an int or array specifying bin edges.

n : int, optional

The number of dimensions to be averaged. By default, all dimensions are averaged. Always uses the first n dimensions.

weights : array, optional

An array of the same shape as the first n dimensions of field, giving a weight for each entry.

average : bool, optional

Whether to take the (weighted) average. If False, returns the (unweighted) sum.

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.

Returns:
field_1d : array

The field averaged angularly (finally 1D)

bins : array

The mean co-ordinate in each radial bin (or the bin edges, if bin_ave is False)

Examples

Create a 3D radial function, and average over radial bins. Equivalent to calling angular_average():

>>> import numpy as np
>>> import matplotlib.pyplot as plt
>>> x = np.linspace(-5,5,128)   # Setup a grid
>>> X,Y,Z = np.meshgrid(x,x,x)  # ""
>>> r = np.sqrt(X**2+Y**2+Z**2) # Get the radial co-ordinate of grid
>>> field = np.exp(-r**2)       # Generate a radial field
>>> avgfunc, bins, _ = angular_average_nd(field,[x,x,x],bins=100)   # Call angular_average
>>> plt.plot(bins, np.exp(-bins**2), label="Input Function")   # Plot input function versus ang. avg.
>>> plt.plot(bins, avgfunc, label="Averaged Function")

Create a 2D radial function, extended to 3D, and average over first 2 dimensions:

>>> r = np.sqrt(X**2+Y**2)
>>> field = np.exp(-r**2)    # 2D field
>>> field = np.repeat(field,len(x)).reshape((len(x),)*3)   # Extended to 3D
>>> avgfunc, avbins, coords = angular_average_nd(field, [x,x,x], bins=50, n=2)
>>> plt.plot(avbins, np.exp(-avbins**2), label="Input Function")
>>> plt.plot(avbins, avgfunc[:,0], label="Averaged Function")