Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Fix pfield amplitude scaling and use the real element positions
Three fixes to `compute_pfield`:

- Element centres were rebuilt from a pitch derived from the first two
  elements, i.e. assumed a flat, equispaced array at z = 0. Read
  `probe_geometry` directly instead, so curved and irregular arrays get
  their real x/z positions. Pitch is now taken from `Probe.get_pitch`.

- `element_width` is now an explicit argument, falling back to the
  pitch-minus-10%-kerf guess only when it is not known. `Parameters`
  passes the probe's `element_width` through when it has one.

- The pulse spectrum used the *pulse-echo* (two-way) bandwidth for a
  one-way transmit response. The one-way response is the square root of
  the two-way one, hence the added factor 1/2 in the exponent.

- The frequency loop accumulated |P(f_k)|^2 as a sum with no df factor,
  so the result scaled with the number of retained samples, i.e. with
  1 / freq_step -- which is derived from max(t0_delays) and therefore
  depends on which transmits are in the stack. Take the mean instead, so
  the field is a true RMS over the retained band and is comparable across
  transmit subsets. No effect when norm=True.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
  • Loading branch information
wesselvannierop and claude committed Aug 7, 2026
commit 575635b8d74ca05811f8ed82cf7c3d0bd635f08c
33 changes: 20 additions & 13 deletions zea/beamform/pfield.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ def compute_pfield(
tx_apodizations,
grid,
t0_delays,
element_width=None,
frequency_step=4,
db_thresh=-1.0,
downsample=10,
Expand Down Expand Up @@ -108,12 +109,7 @@ def compute_pfield(
grid_x = ops.convert_to_tensor(grid[:, :, 0], dtype="float32")
grid_z = ops.convert_to_tensor(grid[:, :, 2], dtype="float32")
t0_delays = ops.convert_to_tensor(t0_delays, dtype="float32")
tx_apodizations = ops.convert_to_tensor(tx_apodizations, dtype="float32")

# formatting
t0_delays = ops.where(ops.isnan(t0_delays), 0, t0_delays)
tx_apodizations = ops.where(ops.isnan(tx_apodizations), 0, tx_apodizations)
tx_apodizations = ops.cast(tx_apodizations, "complex64")
tx_apodizations = ops.convert_to_tensor(tx_apodizations, dtype="complex64")
Comment thread
wesselvannierop marked this conversation as resolved.
Resolved

# probe params
fc_original = center_frequency
Expand All @@ -123,10 +119,12 @@ def compute_pfield(
num_waveforms = 1 # number of waveforms in the pulse

# array params
pitch = ops.abs(probe_geometry[1, 0] - probe_geometry[0, 0]) # element pitch
if element_width is None:
from zea.probes import Probe

kerf = 0.1 * pitch # for now this is hardcoded
element_width = pitch - kerf
pitch = Probe.get_pitch(probe_geometry)
kerf = 0.1 * pitch
element_width = pitch - kerf

# %------------------------------------%
# % POINT LOCATIONS, DISTANCES & GRIDS %
Expand All @@ -148,8 +146,8 @@ def compute_pfield(
grid_z = ops.reshape(grid_z, (-1,))

# Centers of the transducer elements (x- and z-coordinates)
element_x = (ops.arange(0.0, n_el) - (n_el - 1) / 2) * pitch
element_z = ops.zeros(n_el)
element_x = probe_geometry[:, 0]
element_z = probe_geometry[:, 2]
element_theta = ops.zeros(n_el)

# Centroids of the sub-elements
Expand Down Expand Up @@ -205,8 +203,9 @@ def probe_spectrum(f):
denom = (bandwidth / 2) / (ops.log(2) ** (1 / p_shape))
# Raise the normalized difference to the power of p_shape
exponent = (freq_diff / denom) ** p_shape
# Apply the negative sign and exponential
return ops.cast(ops.exp(-exponent), "complex64")
# The bandwidth is a pulse-echo (two-way) bandwidth; the one-way transmit
# response is the square root of the two-way response, hence the factor 1/2.
return ops.cast(ops.exp(-exponent / 2), "complex64")

# The frequency response is a pulse-echo (transmit + receive) response.
# The spectrum of the pulse (pulse_spectrum) will be then multiplied
Expand Down Expand Up @@ -307,6 +306,14 @@ def scan_fn(carry, k):
# Zero out pressure behind the transducer (z < 0)
pressure_squared = ops.where(grid_z[:, None] < 0, 0, pressure_squared)

# Mean over the retained frequency samples, not their sum: the scan accumulates
# |P(f_k)|^2 with no df factor, so a sum scales with the sample count, i.e. with
# 1/freq_step -- which is set above from max(t0_delays) and so depends on which
# transmits are in the stack. The mean keeps the field a true RMS over the
# retained band, and comparable across transmit subsets (~2e-3 apart, from the
# differing grids). No effect when norm=True: a global factor cancels below.
pressure_squared = pressure_squared / ops.cast(ops.shape(freq)[0], "float32")

# RMS acoustic pressure, reshaped to (n_tx, grid_size_z, grid_size_x)
pressure = ops.transpose(ops.sqrt(pressure_squared), (1, 0))
pressure = ops.reshape(pressure, (-1, *size_downsampled))
Expand Down
1 change: 1 addition & 0 deletions zea/parameters.py
Original file line number Diff line number Diff line change
Expand Up @@ -995,6 +995,7 @@ def pfield(self) -> np.ndarray:
grid=self.grid,
t0_delays=self.t0_delays,
probe_bandwidth_percent=self.probe_bandwidth_percent,
element_width=self.element_width if hasattr(self, "element_width") else None,
**self.pfield_kwargs,
)
return ops.convert_to_numpy(pfield)
Expand Down