Peak finding

Find and interpolate peaks in 1D, 2D and 3D signals.

machinevisiontoolbox.base.findpeaks.findpeaks(y, x=None, npeaks=None, scale=1, interp=0, return_poly=False)[source]

Find peaks in a 1D signal

Parameters:
  • y (ndarray(N)) – 1D-signal \(y(x)\)

  • x (ndarray(N), optional) – corresponding dependent variable, defaults to the integer sequence \(0 \ldots N-1\).

  • npeaks (int, optional) – number of peaks P to return, defaults to all

  • scale (int, optional) – peak scale, defaults to 1

  • interp (bool or int, optional) – interpolate the peak value, defaults to False

  • return_poly (bool, optional) – return interpolation polynomial, defaults to False

Raises:

ValueErrorinterp must be > 2 if given

Returns:

peak positions and values

Return type:

ndarray(P), ndarray(P)

Find the peak in a 1D signal.

Example:

>>> from machinevisiontoolbox.base import *
>>> import numpy as np
>>> y = np.array([0, 0, 1, 2, 0, 0, 0, 3, 1, 0, 0, 0, 0])
>>> findpeaks(y, scale=3)
(array([7, 3]), array([3., 2.]))
>>> findpeaks(y, scale=3, interp=True)
(array([7.1   , 2.8333]), array([3.025 , 2.0417]))

Note

  • A maxima is defined as an element that is larger than its scale neighbours on either side. This is the largest value in a 2*scale+1 sliding window.

  • The first and last scale elements will never be returned as maxima.

  • To find minima, use findpeak(-y).

  • The interp options fits points in the neighbourhood about the peak with an M’th order polynomial and its peak position is returned. Typically choose M to be even. Setting interp to True uses M=2. Alternatively set interp to M.

  • If return_poly is True then an additional value is returned which is a list of polynomial coefficients for each peak, see numpy.polynomial.

Seealso:

findpeaks2d findpeaks3d

machinevisiontoolbox.base.findpeaks.findpeaks2d(z, npeaks=2, scale=1, interp=False, positive=True)[source]

Find peaks in a 2D signal

Parameters:
  • z (ndarray(H,W)) – 2D-signal \(z(x,y)\)

  • npeaks (int) – number of peaks to return (default all)

  • scale (float) – scale of peaks to consider

  • interp (bool, optional) – interpolate the peak value, defaults to False

  • positive (bool, optional) – peak must be > 0, defaults to False

Returns:

peak positions and magnitudes, one per row

Return type:

ndarray(P,3)

Find the maximum of a 2D signal, typically a greyscale image.

Example:

>>> from machinevisiontoolbox.base import *
>>> import numpy as np
>>> z = np.zeros((10,10))
>>> z[3,4] = 2
>>> z[4,4] = 1
>>> findpeaks2d(z)
array([[4., 3., 2.]])
>>> findpeaks2d(z, interp=True)
array([[4.    , 3.1667, 2.0417, 1.5   ]])

Note

  • A maxima is defined as an element that larger than its neighbours in a 2*scale+1 square window.

  • Elements where the window falls off the edge of the input array will never be returned as maxima.

  • To find minima, use findpeaks2d(-image).

  • The interp options fits points in the neighbourhood about the peak with a paraboloid and its peak position is returned.

Seealso:

findpeaks findpeaks3d

machinevisiontoolbox.base.findpeaks.findpeaks3d(v, npeaks=None)[source]

Find peaks in a 3D signal

Parameters:
  • z (ndarray(H,W,D)) – 3D-data \(v(x,y,z)\)

  • npeaks (int) – number of peaks to return (default all)

Returns:

peak position and magnitude, one per row

Return type:

ndarray(N,4)

Find the maximum of a 3D signal, typically volumetric data such as a scale-space image stack.

Example:

>>> from machinevisiontoolbox.base import *
>>> import numpy as np
>>> z = np.zeros((10,10,10))
>>> z[3,4,5] = 1
>>> findpeaks3d(z)
array([[3., 4., 5., 1.]])

Note

A maxima is defined as an element that larger than its 26 neighbours. Edges elements will never be returned as maxima.

Seealso:

findpeaks findpeaks2d