pysdic.compute_wave_speckle#

compute_wave_speckle(points, *, wavelength=0.25, n_waves=40, gray_levels=(20.0, 235.0), seed=0, gradient=False)[source]#

Compute the gray levels of a synthetic speckle pattern at given points.

The pattern is a sum of \(M\) plane waves of the same wavelength \(\lambda\), random directions and random phases, saturated by a hyperbolic tangent to give dark and bright spots:

\[S(\vec{x}) = \sum_{j=1}^{M} \cos\bigl(\vec{k}_j \cdot \vec{x} + \varphi_j\bigr), \qquad \|\vec{k}_j\| = \frac{2\pi}{\lambda}\]
\[g(\vec{x}) = \frac{g_{min} + g_{max}}{2} + \frac{g_{max} - g_{min}}{2} \tanh\left(\frac{S(\vec{x})}{\sqrt{M}}\right)\]

The division by \(\sqrt{M}\) keeps the standard deviation of \(S\) close to 1 whatever the number of waves. The gray levels stay strictly between \(g_{min}\) and \(g_{max}\).

The pattern is a smooth function of the coordinates: evaluated at the reference coordinates of material points, it gives a texture attached to the material that can be rendered in any camera (synthetic images for DIC tests).

With gradient=True, the exact gradient of the gray levels is also returned (e.g. as a reference to check the interpolation of an image):

\[\nabla g(\vec{x}) = -\frac{g_{max} - g_{min}}{2 \sqrt{M}} \left(1 - \tanh^2\left(\frac{S(\vec{x})}{\sqrt{M}}\right)\right) \sum_{j=1}^{M} \sin\bigl(\vec{k}_j \cdot \vec{x} + \varphi_j\bigr) \, \vec{k}_j\]

Note

The speckle spots have a size of about \(\lambda / 2\). For DIC, aim at 3 to 5 pixels per spot in the images, i.e. a wavelength of about 6 to 10 pixels once projected: with points in material units seen with a magnification of \(m\) pixels per unit, choose \(\lambda \approx 6 / m\) to \(10 / m\). Smaller spots are aliased by the pixels (see the n_samples of pysdic.generate_synthetic_image()), larger spots reduce the spatial resolution of the measurement.

Note

The waves are drawn from seed at each call: the same seed, n_waves and point dimension \(D\) always give the same pattern. Use seed=None for a different pattern at each call.

Note

The directions of the waves are isotropic in \(\mathbb{R}^D\). For a surface lying in a plane \(z = c\), give only the in-plane coordinates (points[..., :2]): with 3D points, the waves with a component along \(z\) have a longer apparent wavelength in the plane.

Parameters:
  • points (ArrayLike) – Coordinates of the points, an array of shape \((\dots, D)\) with \(D \in \{1, 2, 3\}\). The values must be finite.

  • wavelength (Real, optional (default: 0.25)) – Wavelength \(\lambda\) of the waves, in the unit of points. It sets the size of the speckle spots (about \(\lambda / 2\)). Must be finite and positive.

  • n_waves (Integral, optional (default: 40)) – Number of waves \(M\), at least 1. More waves give a more irregular pattern.

  • gray_levels (Tuple[Real, Real], optional (default: (20.0, 235.0))) – Bounds \((g_{min}, g_{max})\) of the gray levels, finite with \(g_{min} < g_{max}\).

  • seed (Integral, optional (default: 0)) – Seed of the random generator (numpy.random.default_rng). If None, the pattern is different at each call.

  • gradient (bool, optional (default: False)) – Whether to also return the gradient of the gray levels with respect to the coordinates of the points.

Returns:

  • gray_levels (numpy.ndarray) – The gray levels, a float array of shape \((\dots)\).

  • gradient (numpy.ndarray) – Only with gradient=True: the gradient of the gray levels, a float array of shape \((\dots, D)\) (in gray levels per unit of points).

Raises:
  • TypeError – If an input has a wrong type.

  • ValueError – If points has a wrong shape or non-finite values, or if a parameter has a wrong value.

Return type:

ndarray | Tuple[ndarray, ndarray]

Examples

Texture attached to the reference coordinates of the vertices of a plane mesh:

import numpy
from pysdic import compute_wave_speckle

x, y = numpy.meshgrid(numpy.linspace(0, 1, 200), numpy.linspace(0, 1, 200))
image = compute_wave_speckle(numpy.stack((x, y), axis=-1), wavelength=0.05)
image.shape  # (200, 200)

image, image_gradient = compute_wave_speckle(numpy.stack((x, y), axis=-1), wavelength=0.05, gradient=True)
image_gradient.shape  # (200, 200, 2), derivatives along x and y