Spectra
spectra
Power spectrum calculations for scalar fields on lattices.
This module provides functions to compute power spectra \(P(k)\) from field configurations in 1D, 2D, and 3D. The power spectrum measures the distribution of power across different wavenumbers \(k\), which is fundamental for understanding field fluctuations and correlations.
Physical interpretation:
- \(P(k)\) gives the mean square field amplitude at wavenumber \(k\)
- For Gaussian random fields: \(\langle \phi(k) \phi^*(k') \rangle = (2\pi)^d \delta(k - k') P(k)\)
- In expanding universes: \(P(k,t)\) tracks growth of structure
Usage
power_spectrum_1d(field, L)
Compute 1D power spectrum \(P(k)\) from a field configuration.
The power spectrum is defined as:
where \(\tilde{\phi}(k)\) is the DFT of the field and \(dx = L/N\) is the lattice spacing. This normalization ensures Parseval's theorem: \(\int (dk/2\pi)\,P(k) = \int dx\,|\phi|^2\).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
field
|
Array
|
1D array of field values, shape (N,) |
required |
L
|
float
|
Physical length of the lattice |
required |
Returns:
| Type | Description |
|---|---|
Array
|
Tuple of (k_values, P_k) where: |
Array
|
|
tuple[Array, Array]
|
|
Example
Source code in jaxlatt/observables/spectra.py
power_spectrum_2d(field, L)
Compute 2D power spectrum P(k_x, k_y) from a field configuration.
Returns the full 2D spectrum in k-space. For radially averaged spectrum, use bin_power_spectrum() on the output.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
field
|
Array
|
2D array of field values, shape (N, N) |
required |
L
|
float
|
Physical length of the lattice (assumed square) |
required |
Returns:
| Type | Description |
|---|---|
Array
|
Tuple of (k_x, k_y, P_k) where: |
Array
|
|
Array
|
|
tuple[Array, Array, Array]
|
|
Example
Source code in jaxlatt/observables/spectra.py
power_spectrum_3d(field, L)
Compute 3D power spectrum P(k_x, k_y, k_z) from a field configuration.
Returns the full 3D spectrum in k-space. For spherically averaged spectrum, use bin_power_spectrum() on the output.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
field
|
Array
|
3D array of field values, shape (N, N, N) |
required |
L
|
float
|
Physical length of the lattice (assumed cubic) |
required |
Returns:
| Type | Description |
|---|---|
Array
|
Tuple of (k_x, k_y, k_z, P_k) where: |
Array
|
|
Array
|
|
Example
Source code in jaxlatt/observables/spectra.py
power_spectrum(field, L)
Compute power spectrum automatically based on field dimensionality.
Dispatches to the appropriate 1D/2D/3D function based on field shape.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
field
|
Array
|
Field array (1D, 2D, or 3D) |
required |
L
|
float | tuple[float, ...]
|
Physical length (scalar or tuple for anisotropic boxes) |
required |
Returns:
| Type | Description |
|---|---|
tuple
|
Tuple of (wavenumbers..., P_k) - format depends on dimensionality |
Source code in jaxlatt/observables/spectra.py
make_log_bin_edges(k_min, k_max, num_bins)
Logarithmically spaced bin edges, shape (num_bins + 1,).
Compute once outside a traced region and pass to
:func:bin_power_spectrum_static, which needs the edges to be static so
its output shape is known at trace time.
Source code in jaxlatt/observables/spectra.py
make_linear_bin_edges(k_min, k_max, num_bins)
bin_power_spectrum_static(k_values, P_values, bin_edges)
Radially bin a power spectrum with a static output shape.
Unlike :func:bin_power_spectrum, this is a pure JAX function: it is
jittable, vmappable and differentiable, because it always returns exactly
num_bins values instead of dropping empty bins. Empty bins are reported
with counts == 0 and a value of zero; mask on counts downstream.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
k_values
|
Array
|
Wavenumber magnitudes, any shape. |
required |
P_values
|
Array
|
Power values, same shape as |
required |
bin_edges
|
Array
|
Monotonic edges of shape |
required |
Returns:
| Type | Description |
|---|---|
tuple[Array, Array, Array]
|
Tuple |
Notes
- The
k = 0mode and any non-finite entries are excluded by weight, not by indexing, so shapes stay static. - Modes outside
[bin_edges[0], bin_edges[-1]]are excluded. A mode exactly equal tobin_edges[-1]falls in the last bin (this differs from :func:bin_power_spectrum, whose last bin is half-open and therefore drops the largest mode).
Source code in jaxlatt/observables/spectra.py
bin_power_spectrum(k_values, P_values, num_bins=50, k_min=None, k_max=None, log_bins=True)
Bin power spectrum values into radial/spherical shells.
This function takes the raw k-space power spectrum and averages it into bins of |k| to produce a 1D radially-averaged spectrum P(k).
Eager convenience wrapper around :func:bin_power_spectrum_static: it
picks bin edges from the data and drops empty bins, which makes the output
shape value-dependent and therefore not traceable. Use
:func:bin_power_spectrum_static inside jit/vmap/grad.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
k_values
|
Array
|
Flattened array of wavenumber magnitudes |k| |
required |
P_values
|
Array
|
Flattened array of power spectrum values |
required |
num_bins
|
int
|
Number of bins for averaging |
50
|
k_min
|
float | None
|
Minimum k value (default: smallest non-zero k) |
None
|
k_max
|
float | None
|
Maximum k value (default: maximum k) |
None
|
log_bins
|
bool
|
Use logarithmic binning (default: True, recommended for wide k-range) |
True
|
Returns:
| Type | Description |
|---|---|
ndarray
|
Tuple of (k_binned, P_binned, counts) where: |
ndarray
|
|
ndarray
|
|
tuple[ndarray, ndarray, ndarray]
|
|
Example
Notes
- Excludes k=0 mode (DC component)
- Log binning is recommended for fields with power-law spectra
- Empty bins are removed from output
Source code in jaxlatt/observables/spectra.py
power_spectrum_with_binning(field, L, num_bins=50, log_bins=True)
Convenience function: compute and bin power spectrum in one call.
Automatically handles 1D/2D/3D fields and returns radially/spherically averaged power spectrum.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
field
|
Array
|
Field array (1D, 2D, or 3D) |
required |
L
|
float
|
Physical length of the lattice |
required |
num_bins
|
int
|
Number of bins for averaging |
50
|
log_bins
|
bool
|
Use logarithmic binning |
True
|
Returns:
| Type | Description |
|---|---|
tuple[ndarray, ndarray, ndarray]
|
Tuple of (k, P, counts) - binned power spectrum |